打开cv iosvideo处理
我正在尝试使用openCv框架来做这里的iosvideo处理教程。
我已经成功地将ios openCv框架加载到我的项目中, 但似乎在我的框架和教程中提供的框架之间不匹配,我希望有人能帮助我。
OpenCv使用cv::Mat
types来表示图像。 当使用AVfoundation委托来处理来自相机的图像时 – 我将需要将所有的CMSampleBufferRef
转换为该types。
看来,本教程中介绍的openCV框架提供了一个名为using的库
#import <opencv2/highgui/cap_ios.h>
用新的委托命令:
任何人都可以指出我在哪里可以find这个框架或CMSampleBufferRef
和cv::Mat
之间的快速转换
编辑
在opencv框架中有很多细分(至less对ios来说)。 我已经通过各种“官方”网站下载它,并使用THEIR指令使用fink和brew等工具。 我甚至比较了安装到/ usr / local / include / opencv /中的头文件。 每次都不一样 下载openCV项目时 – 在同一个项目中有各种cmake文件和冲突的自述文件。 我认为我成功地为IOS构build了一个好的版本,通过这个链接内置了框架的avcapturefunction(通过这个头文件<opencv2/highgui/cap_ios.h>
),然后使用ios目录中的python脚本构build库- 使用命令python opencv/ios/build_framework.py ios
。 我会尝试更新
这是我使用的转换。 你locking像素缓冲区,用cv :: Mat创build一个cv :: Mat进程,然后解锁像素缓冲区。
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); CVPixelBufferLockBaseAddress( pixelBuffer, 0 ); int bufferWidth = CVPixelBufferGetWidth(pixelBuffer); int bufferHeight = CVPixelBufferGetHeight(pixelBuffer); int bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer); unsigned char *pixel = (unsigned char *)CVPixelBufferGetBaseAddress(pixelBuffer); cv::Mat image = cv::Mat(bufferHeight,bufferWidth,CV_8UC4,pixel, bytesPerRow); //put buffer in open cv, no memory copied //Processing here //End processing CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 ); }
上面的方法不会复制任何内存,因此您不拥有内存,pixelBuffer将为您释放它。 如果你想要你自己的缓冲区的副本,就这样做
cv::Mat copied_image = image.clone();
这是以前接受的答案中的代码的更新版本,应该可以在任何iOS设备上使用。
由于至less在iPhone 6和iPhone 6+上, bufferWidth
不等于bytePerRow
,因此我们需要将每行中的字节数指定为cv :: Mat构造函数的最后一个参数。
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); CVPixelBufferLockBaseAddress(pixelBuffer, 0); int bufferWidth = CVPixelBufferGetWidth(pixelBuffer); int bufferHeight = CVPixelBufferGetHeight(pixelBuffer); int bytePerRow = CVPixelBufferGetBytesPerRow(pixelBuffer); unsigned char *pixel = (unsigned char *) CVPixelBufferGetBaseAddress(pixelBuffer); cv::Mat image = cv::Mat(bufferHeight, bufferWidth, CV_8UC4, pixel, bytePerRow); // Process you cv::Mat here CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
代码已经在运行iOS 10的iPhone5,iPhone6和iPhone6 +上进行了testing。