2015-03-12 13 views
6

Tôi đang cố gắng xử lý ảnh thời gian thực với iPhone 6 ở tốc độ 240 khung hình/giây. Vấn đề là khi tôi quay video ở tốc độ đó, tôi không thể xử lý hình ảnh đủ nhanh vì tôi cần lấy mẫu mỗi pixel để đạt được mức trung bình. Giảm độ phân giải hình ảnh sẽ dễ dàng giải quyết vấn đề này, nhưng tôi không thể tìm ra cách để làm điều này. Các AVCaptureDeviceFormat có sẵn tùy chọn với 192x144 px, nhưng ở tốc độ 30 khung hình/giây. Tất cả các tùy chọn 240fps đều có kích thước lớn hơn. Sau đây là cách tôi lấy mẫu dữ liệu:Cách nhận 240fps trên iPhone 6 với độ phân giải thấp

- (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]; 
} 
+2

Tôi không nghĩ rằng bạn có thể ép 240fps trên thiết bị, đó là hình ảnh tối đa và đồng thời được xử lý, nếu xử lý quá nặng, khung hình trên giây sẽ thả xuống, tôi đề nghị bạn sử dụng chức năng 'imageFromSampleBuffer' từ mẫu của Apple để có UIImage, được thay đổi kích thước sau này trước khi xử lý, điều này sẽ tăng tốc độ xử lý hình ảnh. – gabbler

+0

Ý tưởng thú vị. Tôi đã không nghĩ đến việc định cỡ lại hình ảnh sau khi nó đã được lấy ra, nhưng trước khi tôi phân tích. Tôi sẽ thử và xem tác động của hiệu suất. Hy vọng rằng thay đổi kích thước hình ảnh sẽ là hoạt động của card đồ họa, vì vậy CPU sẽ không bị ảnh hưởng nhiều. – lehn0058

+1

Đó là những gì tôi làm, tôi thay đổi kích thước với CG khi tôi lấy từ bộ đệm pixel, bởi vì tôi muốn 640x480 @ 60fps và Apple không hỗ trợ nó. Tò mò, tại sao bạn muốn xử lý hình ảnh với tốc độ vô nhân đạo đó? – aledalgrande

Trả lời

0

Hãy thử GPUImage thư viện. Mỗi bộ lọc có phương thức forceProcessingAtSize:. Sau khi thay đổi kích cỡ trên GPU, bạn có thể truy xuất dữ liệu với GPUImageRawDataOutput.

Tôi nhận được 60 hình/giây với hình ảnh xử lý trên CPU bằng phương pháp này.

Các vấn đề liên quan