2

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

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

1
  • Please use code tags. It's illegible now. Commented Aug 3, 2010 at 21:35

4 Answers 4

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a simple C / C++ library to read / write uncompressed video files?
2

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

2 Comments

Updated my answer and attached the source code you're looking for.
I got the Error ---> Capture properties: 1280.000000 x 720.000000 - 0.000000 fps OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /Users/venushka/Documents/opencv/modules/imgproc/src/color.cpp, line 3648 libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/venushka/Documents/opencv/modules/imgproc/src/color.cpp:3648: error: (-215) scn == 3 || scn == 4 in function cvtColor
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()

Comments

0

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

2 Comments

This should be a comment not a an answer. Please add more description to make it good answer.
@Raju Sorry about that. I'm new in here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.