Rob*_*ert 9 c++ opencv objective-c ios avcapturesession
我想对a执行一些操作CVPixelBufferRef并提出一个cv::Mat
CV_8UC1)我不确定最有效的顺序是什么,但是,我知道所有操作都可以在open:CV矩阵上使用,所以我想知道如何转换它.
- (void) captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
cv::Mat frame = f(pixelBuffer); // how do I implement f()?
Run Code Online (Sandbox Code Playgroud)
Rob*_*ert 12
我在一些优秀的GitHub源代码中找到了答案.为简单起见,我在这里改编 它也为我做了灰度转换.
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
OSType format = CVPixelBufferGetPixelFormatType(pixelBuffer);
// Set the following dict on AVCaptureVideoDataOutput's videoSettings to get YUV output
// @{ kCVPixelBufferPixelFormatTypeKey : kCVPixelFormatType_420YpCbCr8BiPlanarFullRange }
NSAssert(format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, @"Only YUV is supported");
// The first plane / channel (at index 0) is the grayscale plane
// See more infomation about the YUV format
// http://en.wikipedia.org/wiki/YUV
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
void *baseaddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
CGFloat width = CVPixelBufferGetWidth(pixelBuffer);
CGFloat height = CVPixelBufferGetHeight(pixelBuffer);
cv::Mat mat(height, width, CV_8UC1, baseaddress, 0);
// Use the mat here
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
Run Code Online (Sandbox Code Playgroud)
我在想最好的订单是:
我正在用这个。我的cv:Mat配置是 BGR(8UC3) colorFormat。
CVImageBufferRef -> cv::Mat
- (cv::Mat) matFromImageBuffer: (CVImageBufferRef) buffer {
cv::Mat mat ;
CVPixelBufferLockBaseAddress(buffer, 0);
void *address = CVPixelBufferGetBaseAddress(buffer);
int width = (int) CVPixelBufferGetWidth(buffer);
int height = (int) CVPixelBufferGetHeight(buffer);
mat = cv::Mat(height, width, CV_8UC4, address, 0);
//cv::cvtColor(mat, _mat, CV_BGRA2BGR);
CVPixelBufferUnlockBaseAddress(buffer, 0);
return mat;
}
Run Code Online (Sandbox Code Playgroud)
cv::Mat -> CVImageBufferRef (CVPixelBufferRef)
- (CVImageBufferRef) getImageBufferFromMat: (cv::Mat) mat {
cv::cvtColor(mat, mat, CV_BGR2BGRA);
int width = mat.cols;
int height = mat.rows;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
// [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
// [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
[NSNumber numberWithInt:width], kCVPixelBufferWidthKey,
[NSNumber numberWithInt:height], kCVPixelBufferHeightKey,
nil];
CVPixelBufferRef imageBuffer;
CVReturn status = CVPixelBufferCreate(kCFAllocatorMalloc, width, height, kCVPixelFormatType_32BGRA, (CFDictionaryRef) CFBridgingRetain(options), &imageBuffer) ;
NSParameterAssert(status == kCVReturnSuccess && imageBuffer != NULL);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
void *base = CVPixelBufferGetBaseAddress(imageBuffer) ;
memcpy(base, mat.data, _mat.total()*4);
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
return imageBuffer;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6115 次 |
| 最近记录: |