2015-03-12 22 views
6

240 fps'de bir iPhone 6 ile gerçek zamanlı görüntü işleme yapmaya çalışıyorum. Sorun şu ki, bu hızda video yakaladığımda, görüntüyü yeterince hızlı işleyemiyorum çünkü ortalamayı almak için her bir pikseli örneklemem gerekiyor. Görüntü çözünürlüğünü azaltmak bu sorunu kolayca çözebilir, ancak bunu nasıl yapacağımı anlayamıyorum. Kullanılabilir AVCaptureDeviceFormat'ların seçenekleri 192x144 px, ancak 30 fps'dir. Tüm 240 fps seçeneklerinin hepsi daha büyük boyutlara sahiptir.Düşük çözünürlükle iPhone 6'da 240fps nasıl elde edilir

- (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

Sana bir cihazda 240FPS zorlayabilir sanmıyorum, işleme ağır ise, saniyede çerçeve aşağı düşecek, bu maksimum ve eş zamanlı olarak görüntüler işlenir, sana Apple'ın örnekten 'imageFromSampleBuffer' işlevini kullanın önermek işlenmeden önce daha sonra yeniden boyutlandırılan bir UIImage almak için kod, bu görüntü işleme hızlandırmak gerekir. – gabbler

+0

İlginç bir fikir. Görüntüyü yeniden alındıktan sonra yeniden boyutlandırmayı düşünmemiştim, ama analizimi yapmadan önce. Bunu deneyeceğim ve hangi performans etkisinin olduğunu göreceğim. Umarım görüntü yeniden boyutlandırılırsa grafik kartı kullanımı olur, bu yüzden CPU çok fazla etkilenmez. – lehn0058

+1

İşte yaptığım şey, piksel arabelleğinden aldığımda CG ile yeniden boyutlandırıyorum çünkü 640x480 @ 60fps istiyorum ve Apple bunu desteklemiyor. Merak etme, neden bu insanlık dışı hızda görüntü işleme yapmak istiyorsun? – aledalgrande

cevap

0

GPUImage kütüphane deneyin: İşte verileri örnekleme ediyorum nasıl. Her filtre, forceProcessingAtSize: yöntemine sahiptir. GPU'da yeniden boyutlandırıldıktan sonra GPUImageRawDataOutput ile veri alabilirsiniz.

Bu yöntemle CPU üzerinde işlem görüntüsüyle 60 fps var.

İlgili konular