介绍
视频读取本质上就是读取图像,因为视频是由一帧一帧图像组成的。1秒24帧基本就能流畅的读取视频了。
①读取视频有两种方法:
A. VideoCapture cap;
cap.open(“1.avi”);
B. VideoCapture cap(“1.avi”);
②循环显示每一帧:
while(1)
{
Mat frame; //定义Mat变量,用来存储每一帧
cap>>frame; //读取当前帧方法一
//cap.read(frame); //读取当前帧方法二
imshow(“视频显示”, frame); //显示一帧画面
waitKey(30); //延时30ms
}
读取视频示例程序
#include<opencv2/opencv.hpp>
using namespace cv;
void main(){
VideoCapture cap;
cap.open("E://2.avi"); //打开视频,以上两句等价于VideoCapture cap("E://2.avi");
//cap.open("http://www.laganiere.name/bike.avi");//也可以直接从网页中获取图片,前提是网页有视频,以及网速够快
if(!cap.isOpened())//如果视频不能正常打开则返回
return;
Mat frame;
while(1)
{
cap>>frame;//等价于cap.read(frame);
if(frame.empty())//如果某帧为空则退出循环
break;
imshow(