OpenCV VideoWriter does not write anything, although cvWriteToAVI does

I am trying to capture a video from a camera and write it to an AVI file. I am 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 version of IplImage * works. Here is 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;
}

And here is 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;
}

It is basically the same, or at least it seems to be the same for me. It reads 100 frames and must write them in "video.avi". It compiles and starts without errors, but the cv :: Mat version does not write anything, and the IplImage * version works fine.

Does anyone have any ideas on what's going on?

+5
source share
4 answers

Opencv ++ , ++. imshow waitkey, , .

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;
}
+8

, , , .

, ( ) . sudo chmod u+rwx python script.

0

, , . .

 capture2->read(frame2);
 cv::resize(frame2,frame2,cv::Size(640,480);
 writer2->write(frame2);
0

:

->/home/input_dir/your

video_path ->/home/input_dir/outputfile.avi

: , ffmpeg , ,

0

All Articles