How to write video file in OpenCV 2.4.3(如何在 OpenCV 2.4.3 中编写视频文件)
问题描述
我使用 OpenCV 2.4.3 来读取和写入视频文件.我的代码是这样的:
I am using OpenCV 2.4.3 to read and write a video file. My code is like this:
cv::VideoCapture video;
video.open ( "D:\testVideo.avi" );
cv::VideoWriter output;
output.open ( "D:\outputVideo.avi", CV_FOURCC('D','I','V','X'), 120, cv::Size ( 1200,1600), true );
cv::Mat img;
for ( int n = 0; ; n ++ )
{
video >> img;
output.write ( img );
}
然后结果视频是一个空文件,我无法打开它.我在这里做错了什么?
Then the result video was an empty file, and I couldn't open it. What did I do wrong here?
推荐答案
问题可能在于您使用的编解码器.
The problem might be the codec you are using.
确保您的东西正常工作的一个简单测试是简单地从网络摄像头检索帧并将它们写入视频文件:
A simple test to make sure your stuff is working properly is to simply retrieve frames from a webcam and write them on a video file:
// Load input video
cv::VideoCapture input_cap(argv[1]);
if (!input_cap.isOpened())
{
std::cout << "!!! Input video could not be opened" << std::endl;
return;
}
// Setup output video
cv::VideoWriter output_cap(argv[2],
input_cap.get(CV_CAP_PROP_FOURCC),
input_cap.get(CV_CAP_PROP_FPS),
cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH),
input_cap.get(CV_CAP_PROP_FRAME_HEIGHT)));
if (!output_cap.isOpened())
{
std::cout << "!!! Output video could not be opened" << std::endl;
return;
}
// Loop to read from input and write to output
cv::Mat frame;
while (true)
{
if (!input_cap.read(frame))
break;
output_cap.write(frame);
}
input_cap.release();
output_cap.release();
这篇关于如何在 OpenCV 2.4.3 中编写视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 OpenCV 2.4.3 中编写视频文件


基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09