1

这是我实现的方式AVCaptureVideoDataOutputSampleBufferDelegate

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    OSType format = CVPixelBufferGetPixelFormatType(pixelBuffer);
    CGRect videoRect = CGRectMake(0.0f, 0.0f, CVPixelBufferGetWidth(pixelBuffer),     CVPixelBufferGetHeight(pixelBuffer));
    AVCaptureVideoOrientation videoOrientation = [[[_captureOutput connections] objectAtIndex:0] videoOrientation];

    CVPixelBufferLockBaseAddress(pixelBuffer, 0);
    void *baseaddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);

    cv::Mat my_mat = cv::Mat(videoRect.size.height, videoRect.size.width, NULL, baseaddress, 0); //<<<<----HERE
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

这是我如何设置捕获格式的格式:

    OSType format = kCVPixelFormatType_32BGRA;

// Check YUV format is available before selecting it (iPhone 3 does not support it)
if ([_captureOutput.availableVideoCVPixelFormatTypes containsObject:
     [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange]]) {
    format = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
}

_captureOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:format]
                                                           forKey:(id)kCVPixelBufferPixelFormatTypeKey];
4

1 回答 1

1

NULL问题是由于作为第三个参数传递而发生的。它应该CV_8UC4用于 4 通道图像:

cv::Mat my_mat = cv::Mat(videoRect.size.height, videoRect.size.width, CV_8UC4, baseaddress);
于 2013-07-04T14:32:04.540 回答