我正在尝试在 iPhone 上进行一些图像处理。我正在使用http://developer.apple.com/library/ios/#qa/qa2010/qa1702.html来捕获相机帧。
我的问题是,当我尝试访问捕获的缓冲区时,相机 FPS 从 30 下降到大约 20。有人知道我该如何解决吗?
我使用了我能找到的最低捕获质量(AVCaptureSessionPresetLow = 192x144),采用 kCVPixelFormatType_32BGRA 格式。如果有人知道我可以使用的较低质量,我愿意尝试。
当我在其他平台(如 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 图像。有没有一种简单的方法来缩小相机输出图像,一种使用硬件加速的方法,所以我可以使用小的方法?