Opencv2.4学习::视频读取

 

 

 通用代码:

#include "stdafx.h"
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace std;
using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{
	//定义VideoCapture对象
	VideoCapture capture;
	//读取视频文件
	capture.open("F:\\opencv_re_learn\\1.mp4");
	//判断是否出错
	if (!capture.isOpened()){
		cout << "some thing wrong" << endl;
		system("pause");
		return -1;
	}
	//获取视频文件相关信息----总帧数
	long nTotalFrame = capture.get(CV_CAP_PROP_FRAME_COUNT);
	cout << "this video frames:" << nTotalFrame << endl;
	//获取视频相关信息---分辨率(宽、高)
	int  frameHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
	int frameWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH);
	cout << "this video is :" << frameWidth << "*" << frameHeight<< endl;
	//获取视频帧率
	double FrameRate = capture.get(CV_CAP_PROP_FPS);
	cout << "the frame rate is:" << FrameRate << endl;
	//循环读取一帧
	Mat frameImg;
	long nCount = 1;
	while (1){
		//输出当前帧数
		cout << "Current frame" << nCount << endl;
		capture >> frameImg;
		//判断是否读完
		if (!frameImg.empty()){
			imshow("frame", frameImg);
		}
		else{
			break;
		}
		//按Q退出
		if (char(waitKey(1000 / FrameRate) == 'q')){
			break;
		}
		nCount++;
	}
	//释放视频文件
	capture.release();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/dieju8330/article/details/82387979