-1

我正在尝试使用我的笔记本电脑摄像头使用 OpenCV 编写视频,但出现以下错误:

VIDEOIO ERROR: V4L/V4L2: VIDIOC_S_CROP
VIDEOIO ERROR: V4L2: getting property #5 is not supported
OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend does not support this file type.) in CvVideoWriter_GStreamer::open, file /home/tul1/Downloads/opencv-3.0.0-alpha/modules/videoio/src/cap_gstreamer.cpp, line 1265
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/tul1/Downloads/opencv-3.0.0-alpha/modules/videoio/src/cap_gstreamer.cpp:1265: error: (-210) Gstreamer Opencv backend does not support this file type. in function CvVideoWriter_GStreamer::open

我正在使用的代码是:

#include <opencv/cv.hpp>
#include <opencv2/opencv.hpp>
#include <opencv/highgui.h>

int main(int argc, char** argv)
{
    cvNamedWindow("DisplayCamera", CV_WINDOW_AUTOSIZE);
    CvCapture* capture = cvCreateCameraCapture(0);
    IplImage* frame;

    const char filename[] = "video";

//  int fourcc = CV_FOURCC('X','V','I','D');
//  int fourcc = CV_FOURCC('P','I','M','1');
    int fourcc = CV_FOURCC('M','J','P','G');
//  int fourcc = CV_FOURCC('M', 'P', '4', '2');
//  int fourcc = CV_FOURCC('D', 'I', 'V', '3');
//  int fourcc = CV_FOURCC('D', 'I', 'V', 'X');
//  int fourcc = CV_FOURCC('U', '2', '6', '3');
//  int fourcc = CV_FOURCC('I', '2', '6', '3');
//  int fourcc = CV_FOURCC('F', 'L', 'V', '1');
//  fourcc = -1;
//  int fourcc = 0;

//  printf("%d\n", fourcc);

    double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    int width = (int) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH );
    int height = (int) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT );
    CvSize size = cvSize( width , height );
    int isColor = 1;
    CvVideoWriter* writer = cvCreateVideoWriter(filename, fourcc, fps, size, isColor);

    while(1)
    {
        frame = cvQueryFrame( capture );
        if( !frame )
        {
            fprintf(stderr,"Frame error");
            break;
        }

        cvWriteFrame(writer , frame);
        cvShowImage("DisplayCamera", frame);

        char c = cvWaitKey( 30 );
        if( c == 27 ) break;    
    }

    cvReleaseVideoWriter(&writer);
    cvReleaseImage(&frame);
    cvReleaseCapture(&capture);
    cvDestroyWindow("DisplayCamera");
    return 0;
}

如您所见,我已经使用 CV_FOURCC 测试了每个编解码器,但仍然出现错误。

我究竟做错了什么?我有编解码器问题吗?

4

2 回答 2

0

问题可能只是filename没有扩展名。将您的代码更新为:

const char filename[] = "video.avi";

另一个潜在的问题是fps相机返回。0会导致您遇到的问题。

于 2014-09-18T20:22:33.067 回答
0

尝试使用此代码。

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(){

    VideoCapture vcap(0); 
      if(!vcap.isOpened()){
             cout << "Error opening video stream or file" << endl;
             return -1;
      }

   double fps =  vcap.get(CV_CAP_PROP_FPS);
   int frame_width=   vcap.get(CV_CAP_PROP_FRAME_WIDTH);
   int frame_height=   vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
   VideoWriter video("out.avi",CV_FOURCC('M','J','P','G'),fps, Size(frame_width,frame_height),true);

   for(;;){

       Mat frame;
       vcap >> frame;
       video.write(frame);
       imshow( "Frame", frame );
       char c = (char)waitKey(33);
       if( c == 27 ) break;
    }
  return 0;
}
于 2014-09-19T04:18:14.670 回答