我正在尝试将CMSampleBufferRef(作为iOS中AVCaptureVideoDataOutputSampleBufferDelegate的一部分)转换为OpenCV Mat,以尝试在半实时状态下稳定输出。目前我正在运行一个测试应用程序,遵循this,但在创建和使用Mat时一直遇到问题。
Swift控制器
let wrapper : OpenCVWrapper = OpenCVWrapper()
...
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
self.wrapper.processBuffer(sampleBuffer, self.previewMat)
}
OpenCVWrapper
- (void)processBuffer:(CMSampleBufferRef)buffer :(UIImageView*)previewMat {
// Convert current buffer to Mat
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(buffer);
CVPixelBufferLockBaseAddress( pixelBuffer, 0);
CGFloat bufferWidth = CVPixelBufferGetWidth(pixelBuffer);
CGFloat bufferHeight = CVPixelBufferGetHeight(pixelBuffer);
unsigned char *pixel = (unsigned char *)CVPixelBufferGetBaseAddress(pixelBuffer);
Mat tmp(bufferWidth, bufferHeight, CV_8UC4, pixel);
Mat cur = tmp.clone();
dispatch_async(dispatch_get_main_queue(), ^{
[previewMat setImage:[UIImage imageWithCVMat:cur]];
});
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
}
在Mat cur = tmp.clone()
中我遇到了EXC_BAD_ACCESS
错误。
你有什么想法,我在这里做错了什么吗?
我已经尝试过bufferWidth、CGFloat和int,并在Mat的构造函数中进行了切换,但问题仍然存在。