OpenCV读写视频文件解析
一.视频读写类
视频处理的是运动图像,而不是静止图像。视频资源可以是一个专用摄像机、网络摄像头、视频文件或图像文件序列。
在
OpenCV 中,VideoCapture 类和 VideoWriter 类为视频处理中所涉及的捕获和记录任务提供了一个易用的
C++API。
cv::VideoCapture类
1、对象的构造函数,如下面的例子:
cv::VideoCapture capture("D:\\Camera Road 01.avi");
参数为const string&,即读入彩色图像,若设置为0则读取摄像头。
2、验证视频读入是否成功,如下:
if (!capture.isOpened())
{
std::cout << "Vidoe open failed!" << std::endl;
return -1;
}
3、验证完成后,就可以开始读取视频啦!
cv::Mat frame;
capture >> frame;
VideoCapture对象的操作可以像流一样读入到Mat类型的对象(即图像)中。
cv::VideoWriter类
这个类是用来写入一个视频的,使用起来比capture麻烦一些。
构造函数 cv::VideoCapture(const string& path,int fourcc,double fps, Size framesize, bool isColor=true)
需要注意的是fourcc,cv::VideoWriter::fourcc(char c1,char c2,char c3,char c4)
常用的格式有
二.视频读写示例
剩下的就与VideoCapture差不多了,不过是输出流的操作。
下方的 recVideo 示例是一个简短的代码片段,可以让你了解如何使用一个默认摄像机作为一个捕捉设备来抓取帧,对它们进行边缘检测,并且将新的转换视频帧作为一个文件保存。而且,创建两个窗口同时显示原始帧和处理过的帧。
recVideo 示例的代码为:
1. #include <opencv2/opencv.hpp>
2. #include <iostream>
3. using namespace std;
4. using namespace cv;
5.
6. int main(int, char **)
7. {
8. Mat in_frame, out_frame;
9. const char win1[]="Grabbing...", win2[]="Recording...";
10. double fps=30;//每秒的帧数
11. char file_out[]="recorded.avi";
12.
13. VideoCapture inVid(O) ; //打开默认摄像机
14. if ( !inVid.isOpened () ) { //检查错误
15. cout << "Error! Camera not ready...\n";
16. return -1;
17. }
18. //获取输入视频的宽度和高度
19. int width = (int)inVid.get(CAP_PROP_FRAME_WIDTH);
20. int height = (int)inVid.get(CAP_PR〇P_FRAME_HEIGHT);
21. VideoWriter recVid(file out,VideoWriter::fourcc(‘M‘,‘S‘,‘V‘,‘C‘), fps, Size(width, height));
22. if (!recVid.isOpened()) {
23. cout << "Error! Video file not opened...\n";
24. return -1;
25. }
26. //为原始视频和最终视频创建两个窗口
27. namedWindow(win1);
28. namedWindow(win2);
29. while (true) {
30. //从摄像机读取帧(抓取并解码)
31. inVid >> in frame;
32. //将帧转换为灰度
33. cvtColor(in_frame, out_frame, C0L0R_BGR2GRAY);
34. //将幀写入视频文件(编码并保存)
35. recVid << out_frame ?
36. imshow (win1, in_frame);// 在窗口中显示帧
37. imshow(win2, out_frame); // 在窗口中显示帧
38. if (waitKey(1000/fps) >= 0)
39. break;
40. }
41. inVid.release(); // 关闭摄像机
42. return 0;
43.}
在本示例中,应该快速浏览以下这些函数:
原文:https://www.cnblogs.com/wujianming-110117/p/13139174.html