美文网首页
OpenCV + AVCapture 踩坑

OpenCV + AVCapture 踩坑

作者: umiiiii | 来源:发表于2019-03-21 17:34 被阅读0次

背景介绍

通过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]];

相关文章

网友评论

      本文标题:OpenCV + AVCapture 踩坑

      本文链接:https://www.haomeiwen.com/subject/mlmcvqtx.html