为什么我的cv :: Mat变成灰色?

时间:2013-07-04 11:01:44

标签: ios opencv avcapturesession

以下是我实施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];

1 个答案:

答案 0 :(得分:1)

问题发生的原因是NULL作为第三个参数传递。对于4通道图像应为CV_8UC4

cv::Mat my_mat = cv::Mat(videoRect.size.height, videoRect.size.width, CV_8UC4, baseaddress);