【问题标题】:Why the function 'x'.write(frame) isn't work for me?为什么函数 'x'.write(frame) 对我不起作用?
【发布时间】:2016-12-19 16:49:50
【问题描述】:

当我在函数中使用:'x'.write(frame) for write to video file in opencv 程序传递代码并且我编译它时没有错误但是当我打开文件时我看到它是 0 kb 并且播放器无法播放。 有人可以帮我吗?

这是我的代码:

    // Setup output video
    cv::VideoWriter output_cap("output.avi",
        CV_CAP_PROP_FOURCC,
        CV_CAP_PROP_FPS,
        cv::Size(1376, 768));


    // Loop to read frames from the image and write it to the output capture
    cv::Mat frame = imread("1.jpg", 0);
    for(int hgf=1;hgf<=300;hgf++)
    {

        if (!frame.data)
        {
            break;
        }

            output_cap.write(frame);

    }

大家好!

【问题讨论】:

  • 我怀疑打开文件“1.jpg”失败,所以没有什么可写的。首先确保它正确打开。
  • 它是打开的“1.JPG”我检查了它。
  • 你检查它是打开的而不是空的吗?尝试显示它,并在!frame.empty() 上添加检查。首先确保您的输入是正确的。我建议使用docs.opencv.org/2.4/doc/tutorials/introduction/…
  • CV_CAP_PROP_FOURCC 和 CV_CAP_PROP_FPS 的设置是什么?您是否尝试将output_cap.write(frame); 替换为output_cap &lt;&lt; frame? frame 频道的数量是否等于 VideoWriter 设置?

标签: c++ opencv video


【解决方案1】:

我认为主要问题是您的代码将错误的 FOURCC 值传递给 VideoWriter。 CV_CAP_PROP_FOURCC(#defined 为 6)用于为 FOURCC 属性命名,但它不是正确的值。 CV_CAP_PROP_FPS (#defined 为 5) 也是如此,但这里的效果只是告诉 VideoWriter 使用 5.0 fps。

这对我有用:

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    if ( argc != 2 ) {
        cout << "image required" << endl;
        return -1;
    }
    Mat frame = imread(argv[1], 1);

    VideoWriter output_cap("output.avi", CV_FOURCC('M','J','P','G'), 15,
        frame.size());

    for(int hgf=1; hgf<=300; hgf++) {
        output_cap.write(frame);
    }

    return 0;
}

注意:根据我的经验,在 Linux 上,VideoWriter 对视频格式的支持一般。对于 M-JPEG(上面使用的)和 H.264 这两种广泛使用的格式,M-JPEG 在 OpenCV 2.4 上运行良好,但在 3.X 和 H.264 中失败的方式与在 question 中对于 2.4 和3.X.

【讨论】:

    【解决方案2】:

    我将我的代码更改为这个,我将我的图像更改为更小的图像,它的工作!

    我的代码:

    cv::Mat frame = imread("01.jpg",1);
    VideoWriter output_cap("output.avi", CV_FOURCC('M', 'J', 'P', 'G'), 1,
    frame.size(),true);
    
    for (int hgf = 1; hgf <= 10; hgf++) {
    output_cap.write(frame);
    std::cout << "-";
    }
    

    问题是我的图像是 4608x3456 并且 opencv 不能在这个文件中使用来创建视频。我更改为 752x515 和这项工作!美好的一天!!!!!!

    【讨论】:

    • 虽然可以回答您自己的问题,但如果您的答案与您已经得到的答案非常接近,Stack Overflow 礼仪将改为接受该答案并可能发表评论。回复:4608x3456,OpenCV 在我的 Ubuntu 12.04 机器上处理了具有此分辨率的测试图像。 (电影播放器​​无法播放生成的视频,但 Avidemux 可以。)
    • @UlrichStern 我同意你的评论,但我告诉我的答案,我没有注意到我写了一个答案,但无论如何我留下了它,因为代码对我有用,有更多细节。美好的一天!
    • @UlrichStern 请记住,提问者甚至没有义务接受答案。
    猜你喜欢
    • 2022-11-25
    • 2019-03-11
    • 2013-02-13
    • 2020-03-11
    • 2021-03-21
    • 2021-12-18
    • 2011-06-23
    相关资源
    最近更新 更多