保存高质量的图像,进行实时处理 - 最好的方法是什么?

时间:2013-05-20 11:21:46

标签: ios opencv avfoundation

我还在学习AVFoundation,所以我不确定如何最好地解决需要捕获高质量静态图像的问题,但提供低质量的预览视频流。

我有一个需要拍摄高质量图像的应用程序(AVCaptureSessionPresetPhoto),但是使用OpenCV处理预览视频流 - 可以接受低得多的分辨率。简单地使用基础OpenCV Video Camera class并不好,因为将defaultAVCaptureSessionPreset设置为AVCaptureSessionPresetPhoto会导致将全分辨率框架传递给processImage - 这确实非常慢。

如何与可用于捕获静止图像的设备建立高质量连接,以及可以处理和显示的低质量连接?我需要如何设置会话/连接的描述将非常有用。是否有这样一个应用程序的开源示例?

2 个答案:

答案 0 :(得分:1)

我做了类似的事情 - 我抓住了委托方法中的像素,制作了CGImageRef,然后将其调度到正常的优先级队列,在那里进行了修改。由于AVFoundation必须使用CADisplayLink作为回调方法,因此它具有最高优先级。在我的特殊情况下,我没有抓住所有像素,因此它以30fps的速度在iPhone 4上运行。根据您要运行的设备,您有像素数,fps等折衷。

另一个想法是获取2个像素子集的幂 - 例如每行和每第4行每4个。我再次在20-30fps的应用程序中做了类似的事情。然后,您可以在调度的块中进一步操作此较小的图像。

如果这看起来令人生畏,可以为工作代码提供赏金。

代码:

// Image is oriented with bottle neck to the left and the bottle bottom on the right
- (void)captureOutput:(AVCaptureVideoDataOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
#if 1   
    AVCaptureDevice *camera = [(AVCaptureDeviceInput *)[captureSession.inputs lastObject] device];
    if(camera.adjustingWhiteBalance || camera.adjustingExposure) NSLog(@"GOTCHA: %d %d", camera.adjustingWhiteBalance, camera.adjustingExposure);
    printf("foo\n");
#endif

    if(saveState != saveOne && saveState != saveAll) return;


    @autoreleasepool {
        CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
        //NSLog(@"PE: value=%lld timeScale=%d flags=%x", prStamp.value, prStamp.timescale, prStamp.flags);

        /*Lock the image buffer*/
        CVPixelBufferLockBaseAddress(imageBuffer,0); 

        NSRange captureRange;
        if(saveState == saveOne) {
#if 0 // B G R A MODE ! 
NSLog(@"PIXEL_TYPE: 0x%lx", CVPixelBufferGetPixelFormatType(imageBuffer));
uint8_t *newPtr = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
NSLog(@"ONE VAL %x %x %x %x", newPtr[0], newPtr[1], newPtr[2], newPtr[3]);
}
exit(0);
#endif
            [edgeFinder setupImageBuffer:imageBuffer];

            BOOL success = [edgeFinder delineate:1];

            if(!success) {
                dispatch_async(dispatch_get_main_queue(), ^{ edgeFinder = nil; [delegate error]; });
                saveState = saveNone;
            } else 
                bottleRange = edgeFinder.sides;
                xRange.location = edgeFinder.shoulder;
                xRange.length = edgeFinder.bottom - xRange.location;

                NSLog(@"bottleRange 1: %@ neck=%d bottom=%d", NSStringFromRange(bottleRange), edgeFinder.shoulder, edgeFinder.bottom );
                //searchRows = [edgeFinder expandRange:bottleRange];

                rowsPerSwath = lrintf((bottleRange.length*NUM_DEGREES_TO_GRAB)*(float)M_PI/360.0f);
NSLog(@"rowsPerSwath = %d", rowsPerSwath);
                saveState = saveIdling;

                captureRange = NSMakeRange(0, [WLIPBase numRows]);
                dispatch_async(dispatch_get_main_queue(), ^
                    {
                        [delegate focusDone];
                        edgeFinder = nil;
                        captureOutput.alwaysDiscardsLateVideoFrames = YES;
                    });
        } else {        
            NSInteger rows = rowsPerSwath;
            NSInteger newOffset = bottleRange.length - rows;
            if(newOffset & 1) {
                --newOffset;
                ++rows;
            }
            captureRange = NSMakeRange(bottleRange.location + newOffset/2, rows);
        }
        //NSLog(@"captureRange=%u %u", captureRange.location, captureRange.length);

        /*Get information about the image*/
        uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); 
        size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
        size_t width = CVPixelBufferGetWidth(imageBuffer); 

        // Note Apple sample code cheats big time - the phone is big endian so this reverses the "apparent" order of bytes
        CGContextRef newContext = CGBitmapContextCreate(NULL, width, captureRange.length, 8, bytesPerRow, colorSpace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little); // Video in ARGB format

assert(newContext);

        uint8_t *newPtr = (uint8_t *)CGBitmapContextGetData(newContext);
        size_t offset   = captureRange.location * bytesPerRow;

        memcpy(newPtr, baseAddress + offset, captureRange.length * bytesPerRow);

        CVPixelBufferUnlockBaseAddress(imageBuffer, 0);

        OSAtomicIncrement32(&totalImages);
        int32_t curDepth = OSAtomicIncrement32(&queueDepth);
        if(curDepth > maxDepth) maxDepth = curDepth;

#define kImageContext   @"kImageContext"
#define kState          @"kState"
#define kPresTime       @"kPresTime"

        CMTime prStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);      // when it was taken?
        //CMTime deStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer);          // now?

        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
            [NSValue valueWithBytes:&saveState objCType:@encode(saveImages)], kState,
            [NSValue valueWithNonretainedObject:(__bridge id)newContext], kImageContext,
            [NSValue valueWithBytes:&prStamp objCType:@encode(CMTime)], kPresTime,
            nil ];
        dispatch_async(imageQueue, ^
            {
                // could be on any thread now
                OSAtomicDecrement32(&queueDepth);

                if(!isCancelled) {
                    saveImages state; [(NSValue *)[dict objectForKey:kState] getValue:&state];
                    CGContextRef context; [(NSValue *)[dict objectForKey:kImageContext] getValue:&context];
                    CMTime stamp; [(NSValue *)[dict objectForKey:kPresTime] getValue:&stamp];

                    CGImageRef newImageRef = CGBitmapContextCreateImage(context); 
                    CGContextRelease(context);
                    UIImageOrientation orient = state == saveOne ? UIImageOrientationLeft : UIImageOrientationUp;
                    UIImage *image = [UIImage imageWithCGImage:newImageRef scale:1.0 orientation:orient]; // imageWithCGImage:  UIImageOrientationUp  UIImageOrientationLeft
                    CGImageRelease(newImageRef);
                    NSData *data = UIImagePNGRepresentation(image);

                    // NSLog(@"STATE:[%d]: value=%lld timeScale=%d flags=%x", state, stamp.value, stamp.timescale, stamp.flags);

                    {
                        NSString *name = [NSString stringWithFormat:@"%d.png", num];
                        NSString *path = [[wlAppDelegate snippetsDirectory] stringByAppendingPathComponent:name];
                        BOOL ret = [data writeToFile:path atomically:NO];
//NSLog(@"WROTE %d err=%d w/time %f path:%@", num, ret, (double)stamp.value/(double)stamp.timescale, path);
                        if(!ret) {
                            ++errors;
                        } else {
                            dispatch_async(dispatch_get_main_queue(), ^
                                {
                                    if(num) [delegate progress:(CGFloat)num/(CGFloat)(MORE_THAN_ONE_REV * SNAPS_PER_SEC) file:path];
                                } );
                        }
                        ++num;
                    }
                } else NSLog(@"CANCELLED");

            } );
    }
}

答案 1 :(得分:0)

在AVCaptureSessionPresetPhoto中,它使用小视频预览(iPhone6约为1000x700)和高分辨率照片(约3000x2000)。

所以我使用修改过的' CvPhotoCamera'用于处理小型预览并拍摄全尺寸照片的类。我在此处发布此代码:https://stackoverflow.com/a/31478505/1994445