访问iPhone视频输出图像缓冲区时FPS低

Asa*_*ssi 8 iphone image-processing video-processing avfoundation

我正在尝试在iPhone上进行一些图像处理.我正在使用http://developer.apple.com/library/ios/#qa/qa2010/qa1702.html来捕捉相机帧.

我的问题是,当我试图访问捕获的缓冲区时,相机FPS从30下降到大约20.有人知道我该如何解决它?

我使用kCVPixelFormatType_32BGRA格式中我能找到的最低捕获质量(AVCaptureSessionPresetLow = 192x144).如果有人知道我可以使用的质量较低,我愿意尝试一下.

当我在其他平台上进行相同的图像访问时,比如Symbian,它可以正常工作.

这是我的代码:

#pragma mark -
#pragma mark AVCaptureSession delegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
    fromConnection:(AVCaptureConnection *)connection 
{ 
 /*We create an autorelease pool because as we are not in the main_queue our code is
  not executed in the main thread. So we have to create an autorelease pool for the thread we are in*/
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
 //Lock the image buffer
    if (CVPixelBufferLockBaseAddress(imageBuffer, 0) == kCVReturnSuccess)
    {  

  // calculate FPS and display it using main thread
  [self performSelectorOnMainThread:@selector(updateFps:) withObject: (id) nil waitUntilDone:NO];


  UInt8 *base = (UInt8 *)CVPixelBufferGetBaseAddress(imageBuffer); //image buffer start address

  size_t width  = CVPixelBufferGetWidth(imageBuffer); 
  size_t height = CVPixelBufferGetHeight(imageBuffer);

  int size = (height*width);
  UInt8* pRGBtmp = m_pRGBimage;

        /*
        Here is the problem; m_pRGBimage is RGB image I want to process.
        In the 'for' loop I convert the image from BGRA to RGB. As a resault, the FPS drops to 20.
        */ 
  for (int i=0;i<size;i++)
  {   
    pRGBtmp[0] = base[2];
    pRGBtmp[1] = base[1];
    pRGBtmp[2] = base[0];
    base = base+4;
    pRGBtmp = pRGBtmp+3;     
  }


  // Display received action
  [self performSelectorOnMainThread:@selector(displayAction:) withObject: (id) nil waitUntilDone:NO];
  //[self displayAction:&eyePlayOutput];
  //saveFrame( imageBuffer );

  //unlock the  image buffer
  CVPixelBufferUnlockBaseAddress(imageBuffer,0);

 }


 [pool drain];
} 
Run Code Online (Sandbox Code Playgroud)

作为答案的后续,我需要实时处理图像,它正在显示.

我注意到当我使用AVCaptureSessionPresetHigh时,我做的最简单的事情就像:

 for (int i=0;i<size;i++)
    x = base[0];
Run Code Online (Sandbox Code Playgroud)

导致帧速率降至4-5 FPS.我想是因为那个大小的图像没有缓存.

基本上我需要96x48图像.是否有一种简单的方法来缩小相机输出图像,这是一种使用硬件加速的方式,所以我可以使用小型的?

Bra*_*son 8

除了最快的iOS设备外,任何迭代图像中每个像素的内容都会相当慢.例如,我通过简单的逐像素颜色测试对640 x 480视频帧(307,200像素)中的每个像素进行基准测试,发现这仅在iPhone 4上以大约4 FPS运行.

您正在寻找处理27,648像素的情况,它应该足够快地运行到iPhone 4上达到30 FPS,但这是一个比原始iPhone和iPhone 3G更快的处理器.iPhone 3G可能仍然会遇到这种处理负担.您也没有说明Symbian设备中处理器的速度有多快.

我建议重新处理你的处理算法以避免颜色空间转换.应该不需要重新排序颜色组件以便处理它们.

此外,您可以通过在图像的行和列内以特定间隔采样来选择性地仅处理几个像素.

最后,如果您的目标是支持OpenGL ES 2.0(iPhone 3G S及更新版本)的较新iOS设备,您可能需要使用GLSL片段着色器来完全在GPU上处理视频帧.我在这里描述了这个过程,以及用于实时基于颜色的对象跟踪的示例代码.在我的基准测试中,GPU可以比CPU快14到28倍处理这种处理.