简体   繁体   中英

Unable to create/save output avi file in OpenCV

I Have following source code to detect BLOB and i am using 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;
}

when i run the program i am getting following run time error when following line encounters

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

Output #0 , avi , to 'monoimg.avi' : Stream #0.0: Video mgeg4, yuv420p, q=2-31, 90k tbn [mpeg4 @ 0x37e5c0] framerate not set OpenCV Error: Bad Argument (Could not open codec 'mpeg 4′:Unspecified Error) in unknown function , file C:\\User\\VP\\ocv\\opencv\\src\\highgui\\cvcap_ffmpeg.cpp, line 1306

First off getCaptureProperties kinda sucks at actually getting anything, so you should check that fps actually has what you think it does. Some codecs can't encode at certain framerates so try just explicitly setting fps to 30 and see if it works.

otherwise you are missing the mpeg 4 codec as it says. I'd recommend:

1.) download some codecs and try again. http://www.divx.com/en/software/divx-plus/codec-pack probably has what you're looking for.

2.) you can change the

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

line to use some other codec. I played around with a couple of codecs and put the amount of time for encoding a 7 min video on my system.

   (\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) 

In particular I would recommend trying the FLV1 codec as I've had a lot of luck with that. So in summary try:

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

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM