我试图将CMSampleBufferRef (作为AVCaptureVideoDataOutputSampleBufferDelegate in iOS的一部分)转换为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的构造函数中转换它们,同样的问题。
发布于 2016-01-19 14:33:23
也许这会奏效:
- (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); copies only 25% of the image
memcpy(image.data, imgBufAddr, w * h* 4); // copies all pixels
// unlock again
CVPixelBufferUnlockBaseAddress(imgBuf, 0);
dispatch_async(dispatch_get_main_queue(), ^{
[previewMat setImage:[UIImage imageWithCVMat:image]];
});
}发布于 2018-04-11 08:15:45
改进的解决方案,解决了“只占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;
}发布于 2016-11-17 22:51:49
图像类型不相等,您需要尝试做某种类型的
cvtColor(image, image_copy, CV_BGRA2BGR);尝试使用其他类型的CV_BGRA2BGR
希望能帮上忙。
https://stackoverflow.com/questions/34677890
复制相似问题