API:
VideoCapture name(const string filename, int apiPreference = CAP_ANY)
参数说明:
filename | 1、视频路径,name of video file (eg. 2、一系列连续图像or image sequence (eg. 3、视频链接or URL of video stream (eg. 4、or GStreamer pipeline string in gst-launch tool format in case if GStreamer is used as backend Note that each video stream or IP camera feed has its own URL scheme. Please refer to the documentation of source stream to know the right URL. |
apiPreference | preferred Capture API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW. |
当使用 VideoCapture capture(0)时,能实现电脑摄像头调用。
.get(param):
输入不同的param可以获取各种视频的信息,参考页面:OpenCV: Additional flags for video I/O API backends
代码:
void QuickDemo::video_demo()
{
VideoCapture capture(0);
Mat frame;
//获取帧率等信息
int fps = capture.get(CAP_PROP_FPS);
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int height = capture.get(CAP_PROP_FRAME_HEIGHT);
int num = capture.get(CAP_PROP_FRAME_COUNT);
cout << "fps" << fps << endl;
cout << "width" << width << endl;
cout << "height" << height << endl;
cout << "num" << num << endl;
while (true) {
int c = waitKey(10);
if (c == 27) {//退出
break;
}
else if(frame.empty()){
break;
}
capture.read(frame);
//翻转输出图片
flip(frame, frame, 1);
// TODO,框图等操作,颜色变换
imshow("frame", frame);
}
}
如果要打开视频文件的话,将上述代码capture(0),改为capture(videopath)即可。
注:完成后需要释放空间,一般API会完成自动释放,不过最好自定义释放位置。
capture.release();
此外:
要获取在线视频,可以在网站的开发人员界面获取实际视频链接,然后在origin中输入对应字符串型的网址即可。
VideoCapture capture(origin);