C++ OpenCV Moving Around

C++ OpenCV Moving Around

                                                           Moving Around:

In the previous example we have seen how to play a video using OpenCV. But to play the video efficiently, we will need a trackbar. The trackbar will allow the user to play,pause,skip to different parts of video. The HighGUI toolkit provides a number of simple instruments for working with images and video beyond the simple display functions we have just demonstrated. To create a trackbar, we call cv::createTrackbar() and indicate which window  we would like the trackbar to appear in.


#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <fstream> using namespace std; int g_slider_position = 0; int g_run = 1, g_dontset = 0; //start out in single step mode cv::VideoCapture g_cap; void onTrackbarSlide( int pos, void *) { g_cap.set( cv::CAP_PROP_POS_FRAMES, pos ); if( !g_dontset ) g_run = 1; g_dontset = 0; } int main( int argc, char** argv ) { cv::namedWindow( "test", cv::WINDOW_AUTOSIZE ); g_cap.open( string(argv[1]) ); int frames = (int) g_cap.get(cv::CAP_PROP_FRAME_COUNT); int tmpw = (int) g_cap.get(cv::CAP_PROP_FRAME_WIDTH); int tmph = (int) g_cap.get(cv::CAP_PROP_FRAME_HEIGHT); cout << "Video has " << frames << " frames of dimensions(" << tmpw << ", " << tmph << ")

." << endl; cv::createTrackbar("Position", "test", &g_slider_position, frames, onTrackbarSlide); cv::Mat frame; for(;;) { if( g_run != 0 ) { g_cap >> frame; if(frame.empty()) break; int current_pos = (int)g_cap.get(cv::CAP_PROP_POS_FRAMES); g_dontset = 1; cv::setTrackbarPos("Position", "test", current_pos); cv::imshow( "test", frame ); g_run-=1; } char c = (char) cv::waitKey(10); if( c == 's' ) // single step { g_run = 1; cout << "Single step, run = " << g_run << endl; } if( c == 'r' ) // run mode { g_run = -1; cout << "Run mode, run = " << g_run <<endl; } if( c == 27 ) break; } return(0); }

Let%u2019s evaluate the program in detail:

int g_slider_position = 0;

 int g_run  = 1;

 int g_dontset  = 0;

First we declare the global variables. g_slider_position is used to keep the trackbar slider position state. Initially the trackbar slider position is set to initial position i.e 0 .Just for convention purpose we declare every variable with g_. We also instantiate another global variable, g_run, which displays new frames as long it is different from zero. A positive number indicates how many frames are displayed before stopping; a negative number means the system runs in continuous video mode. The value g_dontset enables the video to pause whenever the slider control is moved. We capture the video using the global variable g_cap.

void onTrackbarSlide( int pos, void *)


  g_cap.set( cv::CAP_PROP_POS_FRAMES, pos );

  if( !g_dontset ) 

  g_run = 1; 

  g_dontset = 0;


Now we define a callback routine to be used when the user slides the trackbar. This routine will be passed a 32-bit integer, pos, which will be the new trackbar position. Inside this callback, we use the new requested position in g_cap.set() to actually advance the video playback to the new position. The if() statement sets the program to go into single-step mode after the next new frame comes in, but only if the callback was triggered by a user click, not if it was called from the main function.

The call to g_cap.set() is one we will see often in the future, along with its counterpart g_cap.get(). These routines allow us to configure (or query, in the latter case) various properties of the cv::VideoCapture object. In this case, we pass the argument cv::CAP_PROP_POS_FRAMES, which indicates that we would like to set the read position in units of frames. We use g_cap.get() to determine the number of frames in the video and the width and height of the video images. These numbers are printed out. We%u2019ll need the number of frames in the video to calibrate the slider trackbar.Now we create the trackbar.

createTrackbar("Position", "test", &g_slider_position, frames,  onTrackbarSlide);

The function cv::createTrackbar() allows us to give the trackbar (in this case, Position) and to specify a window in which to put the trackbar. We then provide a variable that will be bound to the trackbar, the maximum value of the trackbar (the number of frames in the video), and a callback (or NULL if we don%u2019t want one) for when the slider is moved.

char c = (char) cv::waitKey(10);

if( c == 's' ) // single step


g_run = 1;

 cout << "Single step, run = " << g_run << endl;


 if( c == 'r' ) // run mode  {

g_run = -1;

 cout << "Run mode, run = " << g_run <<endl;


if( c == 27 )


At the bottom of the while loop, we look for keyboard input from the user. If S has been pressed, we go into single-step mode (g_run is set to 1, which allows reading of a single frame). If R is pressed, we go into continuous video mode (g_run is set to -1 and further decrementing leaves it negative for any conceivable video size). Finally, if Esc is pressed(value 27), the program will terminate.