如何在 OpenCV 2.4.3 中编写视频文件

How to write video file in OpenCV 2.4.3(如何在 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 中编写视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
What is an event loop in Qt?(Qt 中的事件循环是什么?)