我尝试将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的构造函数中切换它们,同样的问题。
答案 0 :(得分:2)
也许这会奏效:
- (void)processBuffer:(CMSampleBufferRef)buffer :(UIImageView*)previewMat {
{
CVImageBufferRef imgBuf = CMSampleBufferGetImageBuffer(buffer);
// lock the buffer
CVPixelBufferLockBaseAddress(imgBuf, 0);
// get the address to the image data
void *imgBufAddr = CVPixelBufferGetBaseAddressOfPlane(imgBuf, 0);
// get image properties
int w = (int)CVPixelBufferGetWidth(imgBuf);
int h = (int)CVPixelBufferGetHeight(imgBuf);
// create the cv mat
cv::Mat image;
image.create(h, w, CV_8UC4);
memcpy(image.data, imgBufAddr, w * h);
// unlock again
CVPixelBufferUnlockBaseAddress(imgBuf, 0);
dispatch_async(dispatch_get_main_queue(), ^{
[previewMat setImage:[UIImage imageWithCVMat:image]];
});
}
答案 1 :(得分:1)
图像类型不相等,您需要尝试做某种
cvtColor(image, image_copy, CV_BGRA2BGR);
尝试使用其他类型的CV_BGRA2BGR
希望它有所帮助。
答案 2 :(得分:0)
改进解决方案,修复问题“只有前30%”:
- (cv::Mat)matFromBuffer:(CMSampleBufferRef)buffer {
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(buffer);
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
//Processing here
int bufferWidth = (int)CVPixelBufferGetWidth(pixelBuffer);
int bufferHeight = (int)CVPixelBufferGetHeight(pixelBuffer);
unsigned char *pixel = (unsigned char *)CVPixelBufferGetBaseAddress(pixelBuffer);
//put buffer in open cv, no memory copied
cv::Mat mat = cv::Mat(bufferHeight,bufferWidth,CV_8UC4,pixel,CVPixelBufferGetBytesPerRow(pixelBuffer));
//End processing
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );
cv::Mat matGray;
cvtColor(mat, matGray, CV_BGR2GRAY);
return matGray;
}