OpenCV + AVCapture 踩坑

背景介绍

通过AVCaptureSession提供的AVCaptureVideoDataOutputSampleBufferDelegate方法,期望在

- (void)    captureOutput:(AVCapturePhotoOutput *)output
    didOutputSampleBuffer:(nonnull CMSampleBufferRef)sampleBuffer
           fromConnection:(nonnull AVCaptureConnection *)connection

回调方法中利用 OpenCV 处理传入的图像。
由于传入的图像为CMSampleBufferRef,需要转换为cv::Mat
故网上搜寻多个方法均无果,表现为显示四列灰度图像。
以为是转换方法的实现问题,找到官方库#include "opencv2/imgcodecs/ios.h"
cvMatFromUIImage,还是不行。
尝试打印OSType:

OSType type = CVPixelBufferGetPixelFormatType(pixelBuffer);

发现值为kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
kCVPixelFormatType_420YpCbCr8BiPlanarFullRange

解决方法

[videoDataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容