Opencv学习笔记(二十)--读取视频帧的几种方法

1、第一种方法

#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
	VideoCapture cap("E:\\图片\\视频材料\\AVSEQ01.avi");// open the default camera
	if(!cap.isOpened()) // check if we succeeded
	return -1;
	namedWindow("src");
	for( ; ; )
	{
		Mat frame;
		cap >> frame; // get a new frame from camera
		imshow("src", frame);
		if(waitKey(30) >= 0) 
			break;
	}
	return 0;
}


2、第二种方法


#include <iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
Mat frame; //视频帧
int main()
{
	char FilePathName[200]="E:\\图片\\视频材料\\AVSEQ01.avi";//视频路径
	VideoCapture capture(FilePathName);//读取本地视频
	if(!capture.isOpened())
	{
		cout<<"加载视频失败,请检查文件路径设置!"<<endl;
		return -1;
	}
	namedWindow("原视频");
	bool stop(false);
	while(!stop)
	{
		if (!capture.read(frame))//读取视频的一帧
			break;
		imshow("原视频",frame);
		if (waitKey(10)>=0)
			stop= true;
	}
	waitKey();
	return 0;
}


3、第三种方法

#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
	VideoCapture cap("E:\\图片\\视频材料\\AVSEQ01.avi");
	if(!cap.isOpened()) // check if we succeeded
		return -1;
	namedWindow("src");
	for(;;)
	{
		Mat frame;
		cap.grab(); //从视频文件或捕获设备获取下一帧
		cap.retrieve(frame);//解码并返回抓取了的视频帧
		imshow("src", frame);
		if(waitKey(30) >= 0) 
			break;
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章