我正在尝试使用iPhone 6以240fps进行实时图像处理。问题是当我以这种速度捕获视频时,由于无法对每个像素进行采样以获得平均值,因此无法足够快地处理图像。降低图像分辨率可以轻松解决此问题,但是我不知道该怎么做。可用的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];
}

最佳答案

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

通过这种方法,我在CPU上获得了60fps的过程镜像。

08-27 19:11