0
votes

I am trying to capture a video and store it in a file and then read the same video file. I am able to write it but not able to read the same file. On pressing escape, the program is supposed to quit the webcam and play the recorded video but displays the following error instead:

mpeg1video @ 0x2a16f40] ac-tex damaged at 14 28 [mpeg1video @ 0x2a16f40] Warning MVs not available OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /home/ujjwal/Downloads/OpenCV-2.4.0/modules/core/src/array.cpp, line 2482 terminate called after throwing an instance of 'cv::Exception' what(): /home/ujjwal/Downloads/OpenCV-2.4.0/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat

The code is:

#include <sstream>
#include <string>
#include <iostream>
#include <opencv/highgui.h>
#include <opencv/cv.h>

using namespace cv;
int main(int argc, char* argv[])
{
    Mat inputVideo;
    Mat frame;
    Mat HSV;
    Mat tracking;
    char checkKey;
    VideoCapture capture;
    capture.open(0);
    capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);
    VideoWriter writer("OutputFile.mpeg", CV_FOURCC('P','I','M','1'), 50, Size(640, 480));
    while(1){
        capture.read(inputVideo);
        imshow("Original Video",inputVideo);
        writer.write(inputVideo);
        checkKey = cvWaitKey(20);
        if(checkKey == 27)
            break;
    }
    capture.open("OutputFile.mpeg");
    capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);
    while(1){
        capture.read(inputVideo);
        imshow("Tracking Video", inputVideo);
    }
    return 0;
}

Can someone please help me? Thanks!

1

1 Answers

2
votes

You need to correct several things to make it work:

  1. You have to create the window before showing images in the window.

  2. You have to close the writer to finish writing before open it later.

  3. You need to add cvWaitKey(20) for the second image showing (check out here for why this is essential).

The whole fixed code is as follows:

#include <sstream>
#include <string>
#include <iostream>
#include <opencv/highgui.h>
#include <opencv/cv.h>

using namespace cv;
int main(int argc, char* argv[])
{
    Mat inputVideo;
    Mat frame;
    Mat HSV;
    Mat tracking;
    char checkKey;
    VideoCapture capture;
    capture.open(0);
    capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);
    VideoWriter writer("OutputFile.mpeg", CV_FOURCC('P','I','M','1'), 50, Size(640, 480));
    namedWindow("Original Video", WINDOW_AUTOSIZE );
    while(1){
        capture.read(inputVideo);
        imshow("Original Video",inputVideo);
        writer.write(inputVideo);
        checkKey = cvWaitKey(20);
        if(checkKey == 27)
            break;
    }
    writer.release();
    capture.open("OutputFile.mpeg");
    capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);
    namedWindow("Tracking Video", WINDOW_AUTOSIZE );
    while(1){
        capture.read(inputVideo);
        if (!inputVideo.empty())
        {
            imshow("Tracking Video", inputVideo);
            checkKey = cvWaitKey(20);
        }
        else
            break;
    }
    return 0;
}