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 中编写视频文件


基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07