cdu*_*uck 3 iphone opencv real-time avfoundation
我正在编写一个iPhone应用程序,可以使用OpenCV进行某种实时图像检测.什么是一个转换的最佳途径CMSampleBufferRef图像从相机(我使用AVCaptureVideoDataOutputSampleBufferDelegateAVFoundation的)到IplImage该OpenCV的理解?转换需要足够快,以便可以实时运行.
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Convert CMSampleBufferRef into IplImage
IplImage *openCVImage = ???(sampleBuffer);
// Do OpenCV computations realtime
// ...
[pool release];
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
Sté*_*ard 11
此示例代码基于Apple的样本来管理CMSampleBuffer的指针:
- (IplImage *)createIplImageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer {
IplImage *iplimage = 0;
if (sampleBuffer) {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
// get information of the image in the buffer
uint8_t *bufferBaseAddress = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
size_t bufferWidth = CVPixelBufferGetWidth(imageBuffer);
size_t bufferHeight = CVPixelBufferGetHeight(imageBuffer);
// create IplImage
if (bufferBaseAddress) {
iplimage = cvCreateImage(cvSize(bufferWidth, bufferHeight), IPL_DEPTH_8U, 4);
iplimage->imageData = (char*)bufferBaseAddress;
}
// release memory
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
}
else
DLog(@"No sampleBuffer!!");
return iplimage;
}
Run Code Online (Sandbox Code Playgroud)
您需要创建一个4通道IplImage,因为Phone的相机缓冲区位于BGRA中.
根据我的经验,这种转换足够快,可以在实时应用程序中完成,但当然,任何添加到它的内容都会花费时间,特别是对于OpenCV.
| 归档时间: |
|
| 查看次数: |
8733 次 |
| 最近记录: |