一、知识点
1、VideoCapture类
(1)、用于从视频文件、摄像机或图像序列中捕获视频帧。
(2)、构造函数 VideoCapture(const String & filename, int apiPreference = CAP_ANY)
a、filename可以是视频文件的名称(例如"video.avi"),可以是图像序列(例如"img%02.jpg", 它将读取"img00.jpg"、"img01.jpg"、"img02.jpg"等),还可以是URL。
b、apiPreference是实际执行捕获的API后端,VideoCaptureAPIs枚举值,如: CAP_ANY、CAP_FFMPEG、CAP_IMAGES等。
(3)、构造函数 VideoCapture(int index, int apiPreference = CAP_ANY)
a、打开摄像头进行视频拍摄。
b、index是视频捕获设备的索引,默认摄像头传递0。
c、apiPreference是实际执行捕获的API后端,VideoCaptureAPIs枚举值,如: CAP_ANY、CAP_FFMPEG、CAP_IMAGES等。
(4)、成员函数 virtual bool read(OutputArray image)
a、抓取、解码并返回一个视频帧。
b、image为返回的视频帧。 如果没有抓取任何帧,则图像为空,返回false。
(5)、成员函数 virtual void release();
a、关闭视频文件或捕获设备。
二、示例代码
#include <iostream>
#include <opencv2/opencv.hpp>
int main()
{
cv::VideoCapture capture("../images/video.mp4");
cv::Mat frame;
while (true)
{
capture.read(frame);
if (frame.empty())
{
break;
}
//此处可以对frame做各种处理...
cv::imshow("frame sequence", frame);
int c = cv::waitKey(1);
if (c == 27)
{
break;
}
}
capture.release();
system("pause");
return 0;
}
下一章,讲解视频处理与保存。