c - OpenCV在写入视频时出现相机问题

标签 c opencv camera v4l

我尝试使用 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 测试了每个编解码器,但仍然出现错误。

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

最佳答案

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

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

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

关于c - OpenCV在写入视频时出现相机问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25921512/

相关文章:

video - 将 jpg 文件流转换为 FLV 流

c - 为什么我不能在busybox中用 '\t'模拟自动补全

c++ - 如果程序的一部分表现出未定义的行为,它会影响程序的其余部分吗?

c - 如何在C中为矩阵的特定位置赋值?

java - 如何在Java OpenCV中将Blob转换为Mat?

c++ - 来自 opencv gpu::convolve 的 cuda convnet equvilent 方法

image - 如何将矩形放入具有其尺寸和形状的空间(获取相对相机位置)?

iphone - 使用ARKit的ARSCNView时,可以使用iPhone的前置摄像头吗?

c - 用 C 程序写入帧缓冲区非常慢(Raspberry Pi)

c# - 在 Emgu 中使用 Erode 和 Dilate (OpenCV for C#)