以下内容翻译自OpenCV官方文档,仅翻译了C++相关内容。
VideoCapture是一个用来从视频文件、图片序列或摄像头来获取图像的类。以下是一个样例用法:
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
opencv_source_code/samples/cpp/starter_video.cpp是一个基础的VideoCapture接口用法样例。
opencv_source_code/samples/cpp/video_dmtx.cpp是一个基础的视频处理样例
VideoCapture::VideoCapture
类的构造函数。
C++:
VideoCapture::
VideoCapture
()
C++:
VideoCapture::
VideoCapture
(const string& filename)
C++:
VideoCapture::
VideoCapture
(int device)
VideoCapture::open
打开一个视频文件或摄像头
C++:
bool VideoCapture::
open
(const string& filename)
C++:
bool VideoCapture::
open
(int device)
该方法会首先调用VideoCapture::release()来关闭掉已经打开的文件或摄像头
VideoCapture::isOpened
如果视频捕获已经初始化完成,则返回true。
C++:
bool VideoCapture::
isOpened
()
如果之前调用了构造函数或VideoCapture::open,则该方法会返回true。
VideoCapture::release
关闭视频文件或摄像头
VideoCapture::release()
该方法会被后来的VideoCapture::open()或者解构函数调用。
VideoCapture::grab
抓获视频文件或摄像头的下一帧
C++:
bool VideoCapture::
grab
()
如果捕获成功,则返回true
本方法主要用在多相机的场合,尤其是当多相机没有硬件同步时。也就是,你可以对每一个相机分别调用VideoCapture::grab(),然后再对每一个相机调用更花时间的VideoCapture::retrieve()方法,来解码和得到图像。这样获取到的几帧图像的时间戳将更加接近。