opencv中通过VideoCaptrue类对视频进行读取操作以及调用摄像头.常用的操作如下:
1、常用构造函数
1.VideoCapture类的构造函数:
C++: VideoCapture::VideoCapture()
C++: VideoCapture::VideoCapture(const string& filename)
C++: VideoCapture::VideoCapture(int device)
2、判断是否打开
cap.isOpened()
3、获取视频相关属性
cout << "视频相关信息如下" << endl;
cout << "宽度:" << cap.get(CV_CAP_PROP_FRAME_WIDTH) << endl;
cout << "高度:" << cap.get(CV_CAP_PROP_FRAME_HEIGHT) << endl;
cout << "总帧数:" << cap.get(CV_CAP_PROP_FRAME_COUNT) << endl;
cout << "帧率(fps):" << cap.get(CV_CAP_PROP_FPS) << endl;
4、逐帧读取
Mat frame;
cap >> frame;
5、判断是否完毕
frame.empty()
6、显示视频
imshow("readvideo", frame);
1 include <opencv2\opencv.hpp>
2 #include <iostream>
3 using namespace std;
4 using namespace cv;
5 int main()
6 {
7 cout <<CV_VERSION << endl;
8 VideoCapture cap("c://dev//res//out.avi");
9 if (!cap.isOpened())
10 {
11 cout << "视频文件打开失败" << endl;
12 }
13 cout << "视频相关信息如下" << endl;
14 cout << "宽度:" << cap.get(CV_CAP_PROP_FRAME_WIDTH) << endl;
15 cout << "高度:" << cap.get(CV_CAP_PROP_FRAME_HEIGHT) << endl;
16 cout << "总帧数:" << cap.get(CV_CAP_PROP_FRAME_COUNT) << endl;
17 cout << "帧率(fps):" << cap.get(CV_CAP_PROP_FPS) << endl;
18
19 Mat frame;
20 while (1)
21 {
22 cap >> frame;
23 if (frame.empty())
24 {
25 break;
26 }
27 imshow("readvideo", frame);
28 waitKey(20);//win10 vs2015返回值-1
29 }
30 getchar();
31 return 0;