我尝试用我的笔记本电脑相机使用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测试了每个编解码器,但仍然出现错误。
我做错了什么?我有编解码器问题吗?
答案 0 :(得分:0)
问题可能只是filename
没有扩展名。将您的代码更新为:
const char filename[] = "video.avi";
另一个潜在的问题是相机返回的fps
。 0 会导致您遇到的问题。
答案 1 :(得分: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;
}