OpenCV. Video output
Today I will show you how to display video in our application using OpenCV. It is as easy as working with an image. In addition to the previous actions, we need to make a cycle to read each frame of the video, we also need a team that we can exit from this cycle if the video seems too boring. =)
Let's get started! The result of the program (frame from the movie "Transformers"). The functions that we examined in the last lesson will not be described in this. This function takes as an argument a parameter in which we pass the path of the readable AVI file and returns a pointer to the CvCapture structure. This structure stores all the information about the AVI file.
#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” );
}
CvCapture* capture = cvCreateFileCapture( argv[1] );
frame = cvQueryFrame( capture );
Inside the while (1) loop, we start reading the AVI file. cvQueryFrame () takes a pointer to a CvCapture structure as an argument. And then with each cycle it stores the next frame of the video. The pointer returns to this frame. When we displayed the next frame, we wait 33 milliseconds (in fact, you can set any delay you like, but this one is considered optimal for displaying 30 frames per second, and take 3 milliseconds so far for confidence :)) before displaying the next frame. If the user presses a key on the keyboard, then the cvWaitKey () function passes the variable “c” the ASCII code of this key and if the user presses Esc (ASCII 27), then we exit the loop, otherwise 33 ms pass and the loop continues.
char c = cvWaitKey(33);
if( c == 27 ) break;
cvReleaseCapture( &capture );
One way or another, the cycle was interrupted (the video ended or the Esc key was pressed), then with this function we free 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!;)