【问题标题】:Unable to read video file in OpenCV无法在 OpenCV 中读取视频文件
【发布时间】:2014-01-26 06:57:13
【问题描述】:

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

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.cpp:2482:错误:(-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;
}

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

【问题讨论】:

    标签: c++ opencv video video-capture


    【解决方案1】:

    您需要更正几件事以使其正常工作:

    1. 您必须先创建窗口,然后才能在窗口中显示图像。

    2. 您必须先关闭编写器才能完成写入,然后再打开它。

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

    整个固定代码如下:

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

    【讨论】:

    • 谢谢!我很粗心。
    猜你喜欢
    • 2018-07-22
    • 2018-02-27
    • 2015-12-05
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    相关资源
    最近更新 更多