尽管cvWriteToAVI可以,但OpenCV VideoWriter不会编写任何内容 [英] OpenCV VideoWriter won't write anything, although cvWriteToAVI does

查看:436
本文介绍了尽管cvWriteToAVI可以,但OpenCV VideoWriter不会编写任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从摄像头捕获视频并将其写入AVI文件.我在Windows 7上将Qt 4.8.2与MSVC 2010(x86)一起使用.我有2个版本的代码:一个使用cv :: Mat,另一个使用IplImage *.但是,仅IplImage *版本有效.这是我使用cv :: Mat的代码:

I'm been trying to capture video from a cam and write it into an AVI file. I'm using Qt 4.8.2 with MSVC 2010 (x86) on Windows 7. I have 2 versions of the code: one using cv::Mat and the other using IplImage*. However, only the IplImage* version is working. Here's my code using cv::Mat:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main() {
    VideoCapture* capture2 = new VideoCapture( CV_CAP_DSHOW );
    Size size2 = Size(640,480);
    int codec = CV_FOURCC('M', 'J', 'P', 'G');
    VideoWriter* writer2 = new VideoWriter("video.avi",codec,15,size2);

    int a = 100;
    Mat frame2;
    while ( a > 0 ) {
        capture2->read(frame2);
        writer2->write(frame2);
        a--;
    }

    writer2->release();
    capture2->release();
    return 0;
}

这是使用IplImage *的代码:

And here's the code using IplImage*:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main() {
    CvCapture* capture = cvCaptureFromCAM( CV_CAP_DSHOW );
    CvSize size = cvSize(640,480);
    int codec = CV_FOURCC('M', 'J', 'P', 'G');
    CvVideoWriter* writer = cvCreateVideoWriter("video.avi",codec,15,size);

    int a = 100;
    while ( a > 0 ) {
        IplImage* frame = cvQueryFrame( capture );
        cvWriteToAVI(writer,frame);
        a--;
    }

    cvReleaseVideoWriter(&writer);
    cvReleaseCapture( &capture );
    return 0;
}

基本上是一样的,或者至少对我来说看起来是一样的.它读取100帧并将其写入"video.avi".它可以编译并运行,没有错误,但是cv :: Mat版本没有编写任何内容,并且IplImage *版本可以完美地工作.

It's basically the same, or at least it looks like the same thing to me. It reads 100 frames and should write them into "video.avi". It compiles and runs without errors, but the cv::Mat version doesn't write anything, and the IplImage* version works perfectly.

有人对发生的事情有任何想法吗?

Does someone have any idea on what's going on?

推荐答案

Opencv C ++参考中的语法有些不同,这是C ++中的有效代码. 我刚刚添加了imshow和waitkey,以进行检查,如果需要可以将其删除.

The syntax in Opencv C++ reference is bit different, and here is a working code in C++. I Just added imshow and waitkey, for checking you can remove them if you want.

int main()
{
    VideoCapture* capture2 = new VideoCapture(CV_CAP_DSHOW);
    Size size2 = Size(640, 480);
    int codec = CV_FOURCC('M', 'J', 'P', 'G');
    // Unlike in C, here we use an object of the class VideoWriter//
    VideoWriter writer2("video_.avi", codec, 15.0, size2, true);

    writer2.open("video_.avi", codec, 15.0, size2, true);
    if (writer2.isOpened())
    {
        int a = 100;
        Mat frame2;
        while (a > 0)
        {
            capture2->read(frame2);
            imshow("live", frame2);
            waitKey(100);
            writer2.write(frame2);
            a--;
        }
    }
    else
    {
        cout << "ERROR while opening" << endl;
    }

    // No Need to release the Writer as the distructor will called automatically
    capture2->release();

    return 0;
}

这篇关于尽管cvWriteToAVI可以,但OpenCV VideoWriter不会编写任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆