
Today I will show you how to display a video in our application using OpenCV. It's as easy as working with an image. In addition to past actions, we will need to do a cycle, to read each frame of the video, as well we will need a team, by which we can get out of this cycle, if the video seems too boring. =)
Let's get started!
#include “highgui.h”
int main( int argc, char** argv )
{
cvNamedWindow( “AVI Video”, CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( argv[1] );
IplImage* frame;
while(1)
{
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( “AVI Video”, frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( “AVI Video” );
}
The result of the program (a frame from the movie "Transformers").')
The functions that we considered in the last lesson will not be described in this section.
CvCapture* capture = cvCreateFileCapture( argv[1] );
This function takes as an argument the parameter in which we pass the path of the readable AVI file and returns a pointer to the CvCapture structure. This structure stores all information about the AVI file.
frame = cvQueryFrame( capture );
Inside the while (1) loop, we start reading the AVI file. cvQueryFrame () takes as its argument a pointer to a CvCapture structure. And then with each cycle, the next frame of the video is memorized. The pointer returns to this frame.
char c = cvWaitKey(33);
if( c == 27 ) break;
When we have displayed the next frame, we are waiting for 33 milliseconds (in fact, you can set any delay, but this is considered optimal for displaying 30 frames per second, and take 3 milliseconds for granted :)) before displaying the next frame. If the user presses a key on the keyboard, the cvWaitKey () function sends the ASCII code of this key to the variable “c” and if the user presses Esc (ASCII 27), then we exit the cycle, otherwise it goes 33 ms and the cycle continues.
cvReleaseCapture( &capture );
One way or another, the cycle has been interrupted (the video has ended or the Esc key has been pressed), then with this function we release the memory associated with the CvCapture structure.
That's all! A little later, I will talk about how to add a slidebar to our application so that you can rewind the video.
Good luck!;)