【问题标题】:How to save output video into a file in OpenCV如何将输出视频保存到 OpenCV 中的文件中
【发布时间】:2011-03-24 23:23:59
【问题描述】:

我想将输出视频保存到文件中而不是显示它并尝试使用 cvcaptureimage 但仍然无法获得结果

#include <highgui.h>

int main(int argc, char *argv[])
{
    // Create a "Capture" object from 1st command line argument
    CvCapture *video = cvCaptureFromFile(argv[1]);
    // this is the structure that will store the frames
    IplImage *frame = NULL;
    // create a window to display the video
    cvNamedWindow("Video", CV_WINDOW_AUTOSIZE);
    // get the next frame
    while (frame = cvQueryFrame(video) )
    {
        // display it in the window we created
        cvShowImage("Video", frame);
        // wait 1000/fps milliseconds
        cvWaitKey((int)(1000/cvGetCaptureProperty(video, CV_CAP_PROP_FPS)));
        //reading into .avi file
        CvCapture*capture=0  
        capture=cvcreateFileCapture(arg[1]);
      if(!capture)
        {
           // intiate the video read
            IpLImage *bgr_frame=cvQueryFrame(capture);
            double fps=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
         } 
    }
}

我能知道我在代码中重复的错误吗?

【问题讨论】:

  • 请使用代码标签。现在已经无法辨认了。

标签: c video opencv


【解决方案1】:

我知道这已经很老了,但我不能同意关于 OSX 的说法。

您可以在 OSX 上轻松构建和编译 FFMPEG。无需使用 QTKIT/Quicktime。我实际上在 OSX 上编写视频没有任何问题。

另一个建议是使用新的 OpenCV 2.0+ 结构,而不是使用那些已弃用的结构。 (使用 Mat 而不是 IplImage/cvMat,使用 VideoWriter 和 VideoCapture。)

Mat image;
VideoCapture capture;
VideoWriter out;

capture = VideoCapture(-1);
image << capture;

out.write(image);

【讨论】:

  • 有没有简单的C/C++库来读/写未压缩的视频文件?
【解决方案2】:

如果您需要参考代码,请检查 this 和 this 以帮助您入门。

无论如何,下面的代码显示来自网络摄像头的视频。如果您仔细观察,您会看到从彩色框架到灰度版本的转换,并且灰度显示在窗口上。

灰度帧也记录在磁盘上的文件中,名为out.avi。您必须知道此代码无法在带有 OpenCV 2.1 的 Mac OS X 上运行,因为 OpenCV 需要使用 ffmpeg 编译以允许将视频编码为文件。

在 Windows 上,cvCreateVideoWriter() 将显示一个对话框,供您在保存视频时选择要使用的适当编解码器。可以通过设置硬编码您偏好的编解码器的参数来更改此函数调用。

我写的很匆忙,但我知道它有效。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cv.h>
#include <highgui.h>
#include <cxtypes.h>
#include <cvaux.h>

int main()
{
    int camera_index = 0;
    IplImage *color_frame = NULL;
    int exit_key_press = 0;

    CvCapture *capture = NULL;
    capture = cvCaptureFromCAM(camera_index);
    if (!capture)
    {
        printf("!!! ERROR: cvCaptureFromCAM\n");
        return -1;
    }

    double cam_w = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
    double cam_h = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
    double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);    
    printf("* Capture properties: %f x %f - %f fps\n", cam_w, cam_h, fps); 

    cvNamedWindow("Grayscale video", CV_WINDOW_AUTOSIZE);

    CvVideoWriter* writer = NULL;
    writer = cvCreateVideoWriter("out.avi", -1, fps, cvSize((int)cam_w,(int)cam_h), 1);
    if (writer == NULL)
    {
        printf("!!! ERROR: cvCreateVideoWriter\n");
        return -1;
    }

    while (exit_key_press != 'q')
    {
        color_frame = cvQueryFrame(capture);
        if (color_frame == NULL)
        {
            printf("!!! ERROR: cvQueryFrame\n");
            break;
        }

        IplImage* gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);  
        if (gray_frame == NULL)
        {
            printf("!!! ERROR: cvCreateImage\n");
            continue;
        }

        cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
        cvShowImage("Grayscale video", gray_frame);

        cvWriteFrame(writer, gray_frame);

        cvReleaseImage(&gray_frame);

        exit_key_press = cvWaitKey(1);
    }

    cvReleaseVideoWriter(&writer);
    cvDestroyWindow("Grayscale video");
    cvReleaseCapture(&capture);

    return 0;
}

【讨论】:

  • 更新了我的答案并附上了你要找的源代码。
  • 我收到错误 ---> 捕获属性:1280.000000 x 720.000000 - 0.000000 fps OpenCV 错误:在 cvtColor,文件 /Users/venushka 中断言失败(scn == 3 || scn == 4) /Documents/opencv/modules/imgproc/src/color.cpp,第 3648 行 libc++abi.dylib:以 cv::Exception 类型的未捕获异常终止:/Users/venushka/Documents/opencv/modules/imgproc/src/ color.cpp:3648: 错误: (-215) scn == 3 ||函数 cvtColor 中的 scn == 4
【解决方案3】:

您似乎为循环内的每一帧创建一次视频文件。

在开始时创建一次 avi 文件,然后使用 cvWriteFrame() 将每个新帧添加到其中

【讨论】:

    【解决方案4】:

    因为你还没有发布图片。它会导致内存中断。 您需要在循环中释放图像。

    【讨论】:

    • 这应该是评论而不是答案。请添加更多描述以使其成为好的答案。
    • @Raju 对此感到抱歉。我是新来的。
    猜你喜欢
    • 2012-11-21
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2015-06-01
    • 2016-08-13
    • 2021-01-08
    相关资源
    最近更新 更多