C++ 如何在 OpenCV 2.4.3 中编写视频文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13623394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write video file in OpenCV 2.4.3
提问by E_learner
I am using OpenCV 2.4.3 to read and write a video file. My code is like this:
我正在使用 OpenCV 2.4.3 来读取和写入视频文件。我的代码是这样的:
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?
然后结果视频是一个空文件,我无法打开它。我在这里做错了什么?
回答by karlphillip
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();
回答by Saikat
You declare cv::VideoWriter output
of frame size 1200*1600.
So resize the frame to 1200*1600 using
cv::resize(img,img,cv::Size(1200,1600));
before output.write(img);
您声明cv::VideoWriter output
帧大小为 1200*1600。因此使用cv::resize(img,img,cv::Size(1200,1600));
之前将框架调整为 1200*1600
output.write(img);