从cv :: VideoCapture抓取最后一帧

时间:2013-03-07 19:48:58

标签: ios opencv video-capture

如何使用cv :: VideoCapture访问视频的最后一帧?

在OpenCVViewController.h中:

@interface OpenCVViewController : UIViewController {

    cv::VideoCapture *_videoCapture;
    cv::Mat _lastFrame;

    AVCaptureVideoPreviewLayer *_previewLayer;
    UIView *_videoPreviewView;
}

@property(nonatomic, retain) IBOutlet UIButton *captureButton;
@property(nonatomic, retain) IBOutlet UIView *videoPreviewView;
@property(nonatomic, retain) AVCaptureVideoPreviewLayer *previewLayer;

- (IBAction)capture:(id)sender;

@end

在OpenCVViewController.mm中:

- (void)viewDidLoad {
    [super viewDidLoad];

    _videoCapture = new cv::VideoCapture;
    if (!_videoCapture->open(CV_CAP_AVFOUNDATION)) {
        NSLog(@"Open video camera failed");
    }

    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetHigh;

    CALayer *viewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    _previewLayer.frame = _videoPreviewView.bounds;
    [_videoPreviewView.layer addSublayer:_previewLayer];
    AVCaptureDevice *device = [AVCaptureDeviceInput defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        NSLog(@"Error opening camera: %@", error);
    }

    [session addInput:input];
    [session startRunning];

}


- (IBAction)capture:(id)sender {

    _captureButton.enabled = NO;

    if (_videoCapture && _videoCapture->grab()) {
        (*_videoCapture) >> _lastFrame;          //Line is hit
    }
    else {
        NSLog("Failed to grab frame");
    }
}

按下捕获按钮时,我想抓取videoCapture中的最后一帧并将数据保存到_lastFrame中。使用上面显示的方法,在IBAction中,_last框架为空。

是否有其他方法可以抓取一个帧并使用_lastFrame稍后处理图像?

提前致谢!使用iOS 6和opencv2框架

1 个答案:

答案 0 :(得分:0)

您应该尝试使用VideoCapture::read,它结合了grab()和retrieve()。

另外,我认为read()和retrieve()都返回对VideoCapture Buffer的引用,因此你需要使用cvCloneImage()来操作框架。