【发布时间】: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