我正試圖在iPhone上做一些圖像處理。我使用http://developer.apple.com/library/ios/#qa/qa2010/qa1702.html來捕捉相機幀。訪問iPhone視頻輸出圖像緩衝區時的低FPS
我的問題是,當我試圖訪問捕獲的緩衝區時,相機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圖像。有沒有一種簡單的方法來縮小相機輸出圖像,這是一種使用硬件加速的方式,所以我可以使用小圖像?
我同意這很可能是拖慢整個捕獲速度的緩慢處理,但我不確定早期簡單釋放鎖定會有很大幫助。爲了處理30 FPS的幀,每幀的回調需要在不到1/30秒的時間內完成。如果沒有,處理操作會阻塞它們正在運行的線程。 – 2010-11-23 19:29:07
如果它作爲生產者 - >消費者模式工作 - 回調會被排隊 - 這一切都取決於他對修改後的圖像所做的操作。如果只是保存它然後嘿,讓它在某處緩衝。如果它被顯示,那麼他有一個問題:) – deanWombourne 2010-11-24 11:32:20