无法在OpenCV中读取视频文件

时间:2014-01-26 06:57:14

标签: c++ opencv video video-capture

我正在尝试捕获视频并将其存储在一个文件中,然后读取相同的视频文件。我能够写它但不能读取相同的文件。在按下转义时,该程序应该退出网络摄像头并播放录制的视频,但显示以下错误:

  

mpeg1video @ 0x2a16f40] ac-tex于14 28受损   [mpeg1video @ 0x2a16f40]警告MV不可用   OpenCV错误:cvGetMat中的错误标志(参数或结构字段)(无法识别或不支持的数组类型),文件/home/ujjwal/Downloads/OpenCV-2.4.0/modules/core/src/array.cpp,第2482行   在抛出'cv :: Exception'的实例后终止调用     what():/ home / ujjwal / Downloads / OpenCV-2.4.0 / modules / core / src / array.pp:2482:error:( - 206)函数cvGetMat中无法识别或不支持的数组类型

代码是:

#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;
}

有人可以帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:2)

你需要纠正几件事才能使它发挥作用:

  1. 您必须在窗口中显示图像之前创建窗口。

  2. 您必须关闭作者才能完成写作,然后再打开它。

  3. 您需要为显示的第二张图片添加cvWaitKey(20)(请查看here了解这是必不可少的原因。

  4. 整个固定代码如下:

    #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;
    }