Program to add a trackbar slider to the basic viewer window: when the slider is moved, the function onTrackbarSlide() is called
Program to add a trackbar slider to the basic viewer window: when the slider is moved, the function onTrackbarSlide() is called and then passed to the slider’s new value
#include “cv.h”
#include “highgui.h”
int g_slider_position = 0;
CvCapture* g_capture = NULL;
void onTrackbarSlide(int pos) {
cvSetCaptureProperty(
g_capture,
CV_CAP_PROP_POS_FRAMES,
pos
);
}
int main( int argc, char** argv ) {
cvNamedWindow( “Example3”, CV_WINDOW_AUTOSIZE );
g_capture = cvCreateFileCapture( argv[1] );
int frames = (int) cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
if( frames!= 0 ) {
cvCreateTrackbar(
“Position”,
“Example3”,
&g_slider_position,
frames,
onTrackbarSlide
);
}
IplImage* frame;
// While loop (as in Example 2) capture & show video
…
// Release memory and destroy window
…
return(0);
}
Now we defi ne a callback routine to be used when the user pokes the slider. Th is routine will be passed to a 32-bit integer, which will be the slider position. Th e call to cvSetCaptureProperty() is one we will see oft en in the future, along with its counterpart cvGetCaptureProperty(). Th ese routines allow us to confi gure (or query in the latter case) various properties of the CvCapture 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 can use AVI_RATIO instead of FRAMES if we want to set the position as a fraction of the overall video length). Finally, we pass in the new value of the position. Because HighGUI is highly civilized, it will automatically handle such issues as the possibility that the frame we have requested is not a key-frame; it will start at the previous key-frame and fast forward up to the requested frame without us having to fuss with such details. int frames = (int) cvGetCaptureProperty( g_capture, CV_CAP_PROP_FRAME_COUNT ); As promised, we use cvGetCaptureProperty()when we want to query some data from the CvCapture structure. In this case, we want to fi nd out how many frames are in the video so that we can calibrate the slider (in the next step). if( frames!= 0 ) { cvCreateTrackbar( “Position”, “Example3”, &g_slider_position, frames, onTrackbarSlide ); } The last detail is to create the trackbar itself. Th e function cvCreateTrackbar() allows us to give the trackbar a label* (in this case Position) and to specify a window to put the trackbar in. We then provide a variable that will be bound to the trackbar, the maximum value of the trackbar, and a callback (or NULL if we don’t want one) for when the slider is moved. Observe that we do not create the trackbar if cvGetCaptureProperty() returned a zero frame count. Th is is because sometimes, depending on how the video was encoded, the total number of frames will not be available. In this case we will just play the movie without providing a trackbar. It is worth noting that the slider created by HighGUI is not as full-featured as some sliders out there. Of course, there’s no reason you can’t use your favorite windowing toolkit instead of HighGUI, but the HighGUI tools are quick to implement and get us off the ground in a hurry. Finally, we did not include the extra tidbit of code needed to make the slider move as the video plays. Th is is left as an exercise for the reader.
0 commentaires: