1

如何使用 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 稍后处理图像?

提前致谢!使用带有 opencv2 框架的 iOS 6

4

1 回答 1

0

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

另外,我认为 read() 和 retrieve() 都返回对 VideoCapture 缓冲区的引用,因此您需要使用 cvCloneImage() 来操作帧。

于 2013-03-08T21:55:45.500 回答