OpenCV » Update the Slider Position

OpenCV: Open Computer Vision Library

I am currently making some research on OpenCV to accomplish some knowledge necessary for working on Computer Vision and lately on Gesture Recognition.

The book i am reading now is Learning OpenCV (Computer Vision with the OpenCV Library) by Gary Bradski & Adrian Kaehler

On Chapter 2: Introduction to OpenCV there are some samples. Second Program is about Playing AVI Video with OpenCV. And Third Program is about move around quickly within the video. We can read these on page 21:

* This code does not update the slider position as the video plays; we leave that as an exercise for the reader.
Also note that some mpeg encodings do not allow you to move backward in the video.

That exercise is relative simple so this is the code i propose:

#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("AviPlayer", CV_WINDOW_AUTOSIZE);
   g_capture = cvCreateFileCapture(argv[1]);
   int frames = (int) cvGetCaptureProperty(
      g_capture,
      CV_CAP_PROP_FRAME_COUNT
   );
   if(frames!=0){
      cvCreateTrackbar(
         "Position",
         "AviPlayer",
         &g_slider_position,
         frames,
         onTrackbarSlide
      );
   }
   IplImage* frame;
   while(1){
      frame = cvQueryFrame(g_capture);
      if(!frame) break;
      
		double newpos = cvGetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES);
      cvSetTrackbarPos("Position", "AviPlayer", newpos);
      
      cvShowImage("AviPlayer", frame);
      char c = cvWaitKey(33);
      if ( c == 27 ) break;
   }
   cvReleaseCapture(&g_capture);
   cvDestroyWindow("AviPlayer");
   return(0);
}

References:
cvGetCaptureProperty: Gets video capturing properties

Anuncios

3 pensamientos en “OpenCV » Update the Slider Position”

  1. Hey,

    I am also learning opencv with this book.

    And I had some problem to update the slider position.
    Even using your code, my video is just stuck between the 2 first frames.

    So i wanted to ask if you had this problem? Or if you have any idea i could resolve it.

    Thanks

    Me gusta

  2. Hi

    I checked your code but the result is not as i expected since the program just execute so fast and exit right after it starts.

    I worked up later and found out that your code line 36, the returned value of variable nexpos is the total frame of g_capture, then the next code in 37 always set the slider control to the last value of slider position. That’s why the program run too fast.

    Me gusta

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s