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);
include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
cout <<CV_VERSION << endl;
VideoCapture cap("c://dev//res//out.avi");
if (!cap.isOpened())
{
cout << "视频文件打开失败" << endl;
}
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; Mat frame;
while ()
{
cap >> frame;
if (frame.empty())
{
break;
}
imshow("readvideo", frame);
waitKey();//win10 vs2015返回值-1
}
getchar();
return ;