开发者

How to save output video into a file in OpenCV

I want to save the output video into a file instead of displaying it and tried using cvcaptureimage but still unable to get the result

#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 th开发者_高级运维e 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);
         } 
    }
}

can I know the mistake I am repeating in my code?


I know that this is pretty old, but I cannot agree to that statement about OSX.

You can easily build and compile FFMPEG on OSX. No need for using QTKIT/Quicktime. I'm actually writing Video on OSX without any problems.

Another suggestion would be to use the new OpenCV 2.0+ structures instead of using those deprecated ones. (Use Mat instead of IplImage/cvMat, use VideoWriter and VideoCapture.)

Mat image;
VideoCapture capture;
VideoWriter out;

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

out.write(image);


If you need reference code check this and this to get you started.

Anyway, the code below displays video from the webcam. If you take a closer look you'll see there's a conversion from the colored frame to a grayscale version and the grayscale is displayed on the window.

The grayscale frame is also recorded on file on the disk, named out.avi. You must know that this code wont work on Mac OS X with OpenCV 2.1 simply because OpenCV needs to be compiled with ffmpeg to allow enconding video to a file.

On Windows, cvCreateVideoWriter() will display a dialog box for you to select the appropriate codec you want to use when saving the video. Its possible to change this function call by setting a parameter that will hardcode the codec of your preference.

I wrote it in a hurry, but I know it works.

#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;
}


You seem to be creating the video file once for each frame inside the loop.

Create the avi file once at the beginning, then add each new frame to it with cvWriteFrame()


Because you haven't release the image. it will cause the memory break. you need to release the image in your loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜