如何在低分辨率的iPhone 6上获得240fps

时间:2015-03-12 21:05:30

标签: ios objective-c avcapturedevice

我正在尝试用240fps的iPhone 6进行实时图像处理。问题是当我以该速度捕获视频时,我无法足够快地处理图像,因为我需要对每个像素进行采样以获得平均值。降低图像分辨率可以轻松解决这个问题,但我无法弄清楚如何做到这一点。可用的AVCaptureDeviceFormat有192x144像素的选项,但是30fps。所有240fps选项都具有更大的尺寸。以下是我对数据进行采样的方法:

- (void)startDetection
{
    const int FRAMES_PER_SECOND = 240;
    self.session = [[AVCaptureSession alloc] init];
    self.session.sessionPreset = AVCaptureSessionPresetLow;

    // Retrieve the back camera
    NSArray *devices = [AVCaptureDevice devices];
    AVCaptureDevice *captureDevice;
    for (AVCaptureDevice *device in devices)
    {
        if ([device hasMediaType:AVMediaTypeVideo])
        {
            if (device.position == AVCaptureDevicePositionBack)
            {
                captureDevice = device;
                break;
            }
        }
    }

    NSError *error;
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error];
    [self.session addInput:input];

    if (error)
    {
        NSLog(@"%@", error);
    }

    // Find the max frame rate we can get from the given device
    AVCaptureDeviceFormat *currentFormat;
    for (AVCaptureDeviceFormat *format in captureDevice.formats)
    {
        NSArray *ranges = format.videoSupportedFrameRateRanges;
        AVFrameRateRange *frameRates = ranges[0];

        // Find the lowest resolution format at the frame rate we want.
        if (frameRates.maxFrameRate == FRAMES_PER_SECOND && (!currentFormat || (CMVideoFormatDescriptionGetDimensions(format.formatDescription).width < CMVideoFormatDescriptionGetDimensions(currentFormat.formatDescription).width && CMVideoFormatDescriptionGetDimensions(format.formatDescription).height < CMVideoFormatDescriptionGetDimensions(currentFormat.formatDescription).height)))
        {
            currentFormat = format;
        }
    }

    // Tell the device to use the max frame rate.
    [captureDevice lockForConfiguration:nil];
    captureDevice.torchMode=AVCaptureTorchModeOn;
    captureDevice.activeFormat = currentFormat;
    captureDevice.activeVideoMinFrameDuration = CMTimeMake(1, FRAMES_PER_SECOND);
    captureDevice.activeVideoMaxFrameDuration = CMTimeMake(1, FRAMES_PER_SECOND);
    [captureDevice setVideoZoomFactor:4];
    [captureDevice unlockForConfiguration];

    // Set the output
    AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];

    // create a queue to run the capture on
    dispatch_queue_t captureQueue=dispatch_queue_create("catpureQueue", NULL);

    // setup our delegate
    [videoOutput setSampleBufferDelegate:self queue:captureQueue];

    // configure the pixel format
    videoOutput.videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey,
                                 nil];
    videoOutput.alwaysDiscardsLateVideoFrames = NO;
    [self.session addOutput:videoOutput];

    // Start the video session
    [self.session startRunning];
}

1 个答案:

答案 0 :(得分:0)

试试GPUImage库。每个过滤器都有方法forceProcessingAtSize:。强制在GPU上调整大小后,您可以使用GPUImageRawDataOutput检索数据。

我用这种方法在CPU上获得了60fps的过程映像。