Writing to an AVI File

In many applications we will want to record streaming input or even disparate captured images to an output video stream, and OpenCV provides a straightforward method for doing this. Just as we are able to create a capture device that allows us to grab frames one at a time from a video stream, we are able to create a writer device that allows us to place frames one by one into a video file. The routine that allows us to do this is cvCreateVideoWriter().

Once this call has been made, we may successively call cvWriteFrame(), once for each frame, and finally cvReleaseVideoWriter() when we are done. Example 2-10 describes a simple program that opens a video file, reads the contents, converts them to a log-polar format (something like what your eye actually sees, as described in Chapter 6), and writes out the log-polar image to a new video file.

Example 2-10. A complete program to read in a color video and write out the log-polar transformed video

// Convert a video to grayscale // argv[1]: input video file // argv[2]: name of new output file // #include "cv.h" #include "highgui.h" main( int argc, char* argv[] ) { CvCapture* capture = 0; capture = cvCreateFileCapture( argv[1] ); if(!capture){ return -1; } IplImage *bgr_frame=cvQueryFrame(capture);//Init the video read double fps = cvGetCaptureProperty ( capture, CV_CAP_PROP_FPS ); CvSize size = cvSize( (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT) ...

Get Learning OpenCV now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.