2014-01-26 31 views
0

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

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

的代码是:

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

有人可以帮我吗?谢谢!

回答

2

您需要正确的几件事情,使其工作:

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

谢谢!我很粗心。 – usb