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);
本文详细介绍了OpenCV中的VideoCapture API,包括如何通过路径、图像序列、视频链接或GStreamer管道来打开视频源。同时讲解了如何设置首选的捕获API后端,并展示了如何使用.get()方法获取视频的帧率、宽度、高度和帧数。通过示例代码,演示了视频播放、帧处理和显示的基本操作,以及如何调整和释放资源。此外,还提到可以通过获取网站视频的实际链接来捕获在线视频流。
3584

被折叠的 条评论
为什么被折叠?



