【问题标题】:Unable to create/save output avi file in OpenCV无法在 OpenCV 中创建/保存输出 avi 文件
【发布时间】:2011-03-20 17:33:25
【问题描述】:

我有以下源代码来检测 BLOB,我使用的是 MS 2008,OpenVC 2.1

#include "stdafx.h"

#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

/*You may change the values of the sthreshold and hlower and hupper to get different results....*/
const int sthreshold=210;
const double hlower=178;
const double hupper=3;
int main(int argc, char* argv[]) {

    int i,j,k;//for iterations
    int height,width,step,channels;/*HSV means the frame after color conversion*/
    int heightmono,widthmono,stepmono,channelsmono;/*mono means the frame which has the monochrome image*/
    const char string1[]="monoimg.avi";/*This is the name of the video which would be the outcome of the blob detection program..*/
    uchar *data,*datamono;


    i=j=k=0;

    IplImage *frame = 0;

    int key = 0;/*Initializing the capture from the video...*/

    CvCapture* capture = cvCreateFileCapture( "partofvideo3.avi" );

    double fps = cvGetCaptureProperty (/*getting the capture properties......the frame rate..*/
    capture,CV_CAP_PROP_FPS);

    CvSize size = cvSize(
    (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
    (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
    );

    CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('D', 'I', 'V', 'X') ,fps,size) ;


    if(writer !=NULL)
        printf("Loaded\n");
    else
        printf("Not Loaded\n");
    /* always check */

    if (!capture) {
    fprintf (stderr, "Cannot open video file!\n");
    return(1);
    }


    height = frame->height;
    width = frame->width;
    step = frame->widthStep;
    channels = frame->nChannels;
    data = (uchar *)frame->imageData;

    cvNamedWindow("monoimage", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("original frame", CV_WINDOW_AUTOSIZE);
        for (;;) {/*keep looping till we are out of frames...*/
    if (!cvGrabFrame(capture)) {
    break;
    }

    frame = cvRetrieveFrame(capture);
    IplImage *colimgbot = cvCreateImage( cvGetSize(frame), 8, 3 );
    IplImage *monoimgbot = cvCreateImage( cvGetSize(frame), 8, 1 );
    cvCvtColor(frame,frame,CV_RGB2HSV);

    for(i=0;i< (height);i++)
        {
            for(j=0;j<(width);j++)
            {
            if((data[(height-i)*step+j*channels]<=hlower) && (data[(height-i)*step+j*channels]>=hupper))
                {
                    if((data[(height-i)*step+j*(channels)+1])>sthreshold)
                            /*"height-i" because if we use only "i" we were getting vertically inverted result...hence reinverting the same
                            would do the required....*/
                            datamono[i*stepmono+j*channelsmono]=255;
                        else    
                            datamono[i*stepmono+j*channelsmono]=0;}
                        else datamono[i*stepmono+j*channelsmono]=0;
                }
            }
        cvErode(monoimgbot,monoimgbot,0,14);
        cvDilate( monoimgbot,monoimgbot,0,15);
        cvWriteFrame(writer, monoimgbot);
        cvShowImage("original frame", frame);
        cvShowImage("monoimage", monoimgbot);

        if( (cvWaitKey(10) & 255) == 27 ) break;

        }

    cvReleaseVideoWriter(&writer) ;

    cvDestroyWindow("monoimage");

    cvReleaseCapture(&capture);


    return 0;
}

当我运行程序时,出现以下运行时错误 遇到下一行时

CvVideoWriter* writer=cvCreateVideoWriter(string1, CV_FOURCC( ‘D’,'I’,'V’,'X’),fps,size) ;

输出#0,avi,到‘monoimg.avi’: 流 #0.0:视频 mgeg4,yuv420p,q=2-31,90k tbn [mpeg4 @ 0x37e5c0] 帧率未设置 OpenCV 错误:错误参数(无法打开编解码器“mpeg 4”:未指定错误)在未知函数、文件中 C:\User\VP\ocv\opencv\src\highgui\cvcap_ffmpeg.cpp,第 1306 行

【问题讨论】:

    标签: c++ visual-studio-2008 video opencv


    【解决方案1】:

    首先 getCaptureProperties 在实际获取任何东西方面有点糟糕,因此您应该检查 fps 是否确实具有您认为的功能。某些编解码器无法以某些帧速率进行编码,因此请尝试将 fps 显式设置为 30 并查看它是否有效。

    否则你会错过它所说的 mpeg 4 编解码器。我建议:

    1.) 下载一些编解码器并重试。 http://www.divx.com/en/software/divx-plus/codec-pack 可能有你要找的东西。

    2.) 你可以改变

    CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('D', 'I', 'V', 'X') ,fps,size) ;
    

    line 使用其他一些编解码器。我玩了几个编解码器,并在我的系统上投入了编码 7 分钟视频的时间。

       (\P,\I,\M,\1) ;= MPEG-1 codec      (112913.386195 msecs) (104 MB)
       (\M,\J,\P,\G) ;= motion-jpeg codec (crashed)                                        
       (\M,\P,\4,\2) ;= MPEG-4.2 codec    (107184.186774 msecs) (82 MB)
       (\D,\I,\V,\3) ;= MPEG-4.3 codec    (118308.933328 msecs)  (83 MB)
       (\D,\I,\V,\X) ;= MPEG-4 codec      (99037.738131 msecs)  (85 MB)  
       (\U,\2,\6,\3) ;= H263 codec        (101141.993551 msecs) (89 MB) 
       (\I,\2,\6,\3) ;= H263I codec       (crashed) 
       (\F,\L,\V,\1) ;= FLV1 codec        (104307.567802 msecs) (93 MB) 
    

    我特别推荐尝试 FLV1 编解码器,因为我很幸运。所以总结一下:

    CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('F', 'L', 'V', '1') ,fps,size) ;
    

    祝你好运!

    【讨论】:

    • 我已经安装了 DIVX 编解码器包,我尝试手动将 fps 设置为 30,同时我也尝试了 FLV1,但这次我收到以下错误输出 #0,avi,到 'monoimg. avi': Stream #0.0: Video: flv, yuv420p, q=2-31, 90k tbn, 50 tbc 编译器未对齐堆栈变量。 Libavcodec 已被错误编译,可能非常缓慢或崩溃。这不是 libavcodec 中的错误,而是编译器中的错误。您可以尝试使用 gcc >= 4.2 重新编译。不要向 FFmpeg 开发人员报告崩溃。图片尺寸无效 (0x0) 最后一条消息重复 2 次 avi @ 0x37bfd0]尺​​寸未设置
    • 在 osx opencv2.2 和 MPEG-4 编解码器中手动将 fps 设置为 30 对我有用
    • 请你帮我回答这个问题stackoverflow.com/questions/9063746/…
    猜你喜欢
    • 2010-11-11
    • 2013-07-13
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    相关资源
    最近更新 更多