从cv::VideoCapture抓取最后一帧



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

在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");
    }
}

当捕获按钮被按下时,我想要捕获videoccapture中的最后一帧并将数据保存到_lastFrame中。使用上面所示的方法,在IBAction中,_last frame为空。

是否有另一种方法来抓取帧并使用_lastFrame稍后处理图像?

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

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

另外,我认为read()和retrieve()都返回对videoccapture Buffer的引用,因此您需要使用cvCloneImage()来操纵帧。

相关内容

  • 没有找到相关文章

最新更新