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

标签 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];

最佳答案

问题的发生是因为 NULL 作为第三个参数传递的。对于 4 channel 图像,它应该是 CV_8UC4:

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

关于ios - 为什么我的 cv::Mat 变成灰色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17468603/

相关文章:

python - 如何获取图像的红色 channel 颜色空间?

swift - 如何自定义大小 AVcapture

ios - 使用 AVCaptureSession 录制视频

ios - UIButton touchUpInside 与 UITableViewCell 滑动事件冲突

ios - 不能用 `UIPageViewController` 刷 `UINavigationController`

ios - 如何在 iOS 中以 1% 的精度获取电池电量

c++ - 在 C++ 中使用 opencv 捕获实时视频?

opencv - OpenCV 中的 getRectSubPix 和 borderInterpolate

iphone - 在 iOS 中将单元格添加到 UITableView 的底部

ios - 使用 AVCaptureSession 和 AVAssetWriter 在翻转相机的同时进行无缝录音