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

标签 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];
} 

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

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

 for (int i=0;i<size;i++)
    x = base[0];

导致帧速率下降至 4-5 FPS。我猜这是因为该尺寸的图像没有被缓存。

基本上我需要 96x48 图像。有没有一种简单的方法来缩小相机输出图像的比例,一种使用硬件加速的方法,这样我就可以使用小图像?

最佳答案

在图像中的每个像素上进行迭代的任何操作在除了最快的 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 的较新 iOS 设备(iPhone 3G S 及更高版本),您可能需要考虑使用 GLSL 片段着色器完全在 GPU 上处理视频帧。我描述一下这个过程here ,以及基于颜色的实时对象跟踪的示例代码。在我的基准测试中,GPU 处理此类处理的速度比 CPU 快 14 - 28 倍。

关于iphone - 访问 iPhone 视频输出图像缓冲区时 FPS 较低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4255550/

相关文章:

python - 从 hDf5 文件中读取巨大的 numpy 数组(带有图像数据)的最快方法

Python CV2 视频编写器不保存视频

c - 如何屏蔽视频中的某个窗口或区域

image - OpenCV:从文件夹中读取图像系列

iphone - 从通讯录中获取记录时,应用程序崩溃

image - 在其他图像之上添加覆盖图像

iphone - UIScrollView添加了 subview 但无法滑动到它们

java - 如何锐化android中的图像?

iphone - 我得到 : Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException' , 原因: 'Could not instantiate class named MKMapView'

java - 如何在 iOS 中的线程之间传输数据?