【问题标题】:Grabbing last frame from cv::VideoCapture从 cv::VideoCapture 抓取最后一帧
【发布时间】:2013-03-07 19:48:57
【问题描述】:

如何使用 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 frame为空。

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

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

【问题讨论】:

  • 出于好奇,您是否尝试过使用retrieve() 而不是grab()?
  • 我没有尝试过 retrive,但我认为这有效!非常感谢!
  • 虽然它抓住了它看到的第一件事,而不是点击按钮时看到的框架

标签: ios opencv video-capture


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 2019-12-03
    • 2011-09-17
    • 2018-02-02
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多