Tuesday, September 22, 2009

OpenCV -- 2: how to read, process and write a video

In the previous article I have written something about openCV. At that time it is about how to set the Visual Studio to use OpenCV library and a simple example about how to read an image by cvLoadImage() function. Besides statical image processing, openCV is also able to handle dynamical images -- video. Here I'd like to use a example to explain some relevant functions. Below is the video about a short introduction of the code and the visual result about how to open an available video and process it in a determined way as well as store another video in avi form:





from the video we have seen the structure of the codes. The thought of this code is first generating proper number of windows to demonstrate the videos. Actually video is composed of every single image, so the method to process the image is in fact the core of processing. The whole processing is just done during one while loop until all frames are handled or press the "ESC" button. Of course to implement the functions there are more details for us to care about, such as achieving the infromation of original image and set them for the new constructed video. And for different purposes the image processing methods are also diverse. And here are the codes of the implementation:

#include "cv.h"
#include "highgui.h"
#include <stdio.h>



int main( int argc, char* argv[] ) {
cvNamedWindow
( "Color_Image", CV_WINDOW_AUTOSIZE );
cvNamedWindow
( "Gray_Image", CV_WINDOW_AUTOSIZE );
cvNamedWindow
( "Log_Polar", CV_WINDOW_AUTOSIZE );
CvCapture
* capture = cvCreateFileCapture( "lzs.avi" );
if (!capture){
return -1;
}
IplImage
* bgr_frame;
double fps = cvGetCaptureProperty (
capture
,
CV_CAP_PROP_FPS
);
printf
("fps=%d\n",(int)fps);

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

printf
("frame (w, h) = (%d, %d)\n",size.width,size.height);
CvVideoWriter
* writer = cvCreateVideoWriter(
"cope.avi",
CV_FOURCC
('D','X','5','0'),
fps
,
size
);

IplImage
* logpolar_frame = cvCreateImage(
size
,
IPL_DEPTH_8U
,
3
);

IplImage
* gray_frame = cvCreateImage(
size
,
IPL_DEPTH_8U
,
1
);

while( (bgr_frame=cvQueryFrame(capture)) != NULL ) {
cvShowImage
( "Color_Image", bgr_frame );
cvConvertImage
(
bgr_frame
,
gray_frame
,
0
);

cvLogPolar
( bgr_frame, logpolar_frame,
cvPoint2D32f
(bgr_frame->width/4,
bgr_frame
->height/4),
60,
CV_INTER_LINEAR
+CV_WARP_FILL_OUTLIERS );
cvShowImage
("Gray_Image", gray_frame);
cvShowImage
( "Log_Polar", logpolar_frame );
cvWriteToAVI
( writer, logpolar_frame );
char c = cvWaitKey(30);
if( c == 27 ) break;
}

cvReleaseVideoWriter
( &writer );
cvReleaseImage
( &gray_frame );
cvReleaseImage
( &logpolar_frame );
cvReleaseCapture
( &capture );
}

Inside it the meaning of some significant functions are like this:

cvNamedWindow( "Color_Image", CV_WINDOW_AUTOSIZE ): it is used to generate a new window to illustrate a image or video;
CvCapture* capture = cvCreateFileCapture( "lzs.avi" ): it is used to capture or read a video from the determind path;
CvVideoWriter* writer = cvCreateVideoWriter("cope.avi",CV_FOURCC('D','X','5','0'), fps,size): it is used to create a new video with fixed name and determinded characters;
IplImage* gray_frame = cvCreateImage( size,IPL_DEPTH_8U,1): it is used to create a new image with the expected requirements, here the processed image will be stored here for a further processing;
cvConvertImage() and cvLogPolar() are two special functions in openCV to process the images, in the articles in the future, I will introduce them in details;
cvShowImage("Gray_Image", gray_frame): is the function to demonstrate the image or video;
cvWriteToAVI( writer, logpolar_frame ): write down the new generated video into the disk;
Because the names of the functions are able to imply the application and meanings very obviously, so I will just attach my project code here for you. The detailed description you can find in openCV wiki or reference site here.

1 comments:

Anonymous said...

thanks for the post:),
i have a problem and i hope that u help me :(
when i use:

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

capture is NULL always, i tried many code, and i made sure that the file "in.avi" is in the code directory.

thanks in advance,