2009年1月4日 星期日

Trackbar Slider

// Example 2-3. Program to add a trackbar slider to the basic viewr 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"

void onTrackbarSlide(int);

int g_slider_position = 0;
CvCapture *g_capture = NULL;

int main(int argc, char **argv)
{
 cvNamedWindow("Example3",CV_WINDOW_AUTOSIZE);
 g_capture = cvCreateFileCapture(argv[1]);

 // we use it when we want to query some data from the CvCapture structure
 // it will return how many frames are in the video
 int frames = (int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);
 IplImage *frame;
 if(frames!=0)
  for(;;g_slider_position++)
  {
   // the last parameter is a callback function that reset the position of trackbar slider
   cvCreateTrackbar("Position","Example3",&g_slider_position,frames,onTrackbarSlide);
   frame = cvQueryFrame( g_capture );
   if( !frame )
    break;
   cvShowImage("Example3",frame);
   char c = cvWaitKey(33);
   if( c==27 )
    break;
   while((c=='p')||(c=='P'))
   {
    c = cvWaitKey(0);
    if( c==27 )
    {
     cvReleaseCapture( &g_capture );
     cvDestroyWindow("Example3");
     return 0;
    }
    if( (c=='c')||(c=='C')||(c==13) )
     break;
    else
     c='p';
   }
  }
 cvReleaseCapture( &g_capture );
 cvDestroyWindow("Example3");
 return 0;
}

void onTrackbarSlide(int pos)
{
 cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);
}

沒有留言: