3

我有一个 iOS 应用程序,它使用手机的前置摄像头并设置 AVCaptureSession 来读取传入的摄像头数据。我设置了一个简单的帧计数器来检查数据传入的速度,令我惊讶的是,当相机处于低光照条件时,帧速率(使用代码中的 imagecount 变量测量)非常慢,但只要我移动手机进入明亮的区域,帧速率几乎会增加三倍。我想始终保持图像处理的高帧速率并将 minFrameDuration 变量设置为 30 fps,但这没有帮助。关于为什么这种随机行为的任何想法?

创建捕获会话的代码如下:

#pragma mark Create and configure a capture session and start it running

- (void)setupCaptureSession
{

NSError *error = nil;

// Create the session
session = [[AVCaptureSession alloc] init];

// Configure the session to produce lower resolution video frames, if your
// processing algorithm can cope. We'll specify medium quality for the
// chosen device.
session.sessionPreset = AVCaptureSessionPresetLow;

// Find a suitable AVCaptureDevice
//AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSArray *devices = [AVCaptureDevice devices];
AVCaptureDevice *frontCamera;
AVCaptureDevice *backCamera;

for (AVCaptureDevice *device in devices) {



    if ([device hasMediaType:AVMediaTypeVideo]) {

        if ([device position] == AVCaptureDevicePositionFront) {

            backCamera = device;
        }
        else {

            frontCamera = device;
        }
    }
}


//Create a device input with the device and add it to the session.
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:backCamera
                                                                    error:&error];

if (!input) {
    //Handling the error appropriately.
}
[session addInput:input];

// Create a VideoDataOutput and add it to the session
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];

// Configure your output.
dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
[output setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);

// Specify the pixel format
output.videoSettings =
[NSDictionary dictionaryWithObject:
 [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
                            forKey:(id)kCVPixelBufferPixelFormatTypeKey];

// If you wish to cap the frame rate to a known value, such as 30 fps, set
// minFrameDuration.
output.minFrameDuration = CMTimeMake(1,30);

//Start the session running to start the flow of data

[session startRunning];

}

#pragma mark Delegate routine that is called when a sample buffer was written

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
   fromConnection:(AVCaptureConnection *)connection
{
  //counter to track frame rate
  imagecount++;

  //display to help see speed of images being processed on ios app
  NSString *recognized = [[NSString alloc] initWithFormat:@"IMG COUNT - %d",imagecount];
  [self performSelectorOnMainThread:@selector(debuggingText:) withObject:recognized waitUntilDone:YES];

}
4

1 回答 1

4

当光线较少时,相机需要更长的曝光时间才能在每个像素中获得相同的信噪比。这就是为什么您可能期望在低光照条件下帧速率下降的原因。

您将 minFrameDuration 设置为 1/30 秒,以防止长时间曝光帧降低帧速率。但是,您应该改为设置 maxFrameDuration:您的代码原样表示帧速率不超过 30 FPS,但它可能是 10 FPS 或 1 FPS....

此外,文档说用 lockForConfiguration: 和 unlockForConfiguration: 括起对这些参数的任何更改,因此可能是您的更改没有进行。

于 2014-01-04T23:50:50.817 回答