tags:

views:

235

answers:

5

Hey everyone,

I've been trying to find/implement a seek and rewind function (for video (.avi)) using OpenCV in C++, but I cant find a way of doing it, other than going through the entire file once and saving each image. Is there any other way?

Any help would be much appreciated; Thanks ahead of time!

A: 

In the highgui library you'll find functions for a seek bar (cvCreateTrackbar and friends).

mmsmatt
the way it's described it doesnt sound like a seekbar; it sounds liek a regular trackbar. Also, every example I found for it online, utilized it for other random shiz, like changing picture hue.
Cenoc
A: 

I think you'd have to read in the entire file into an array of IplImages, then work through that. The reason is, cvQueryFrame is a one-way process, it reads one frame at a time in order. I can't think of any other way. Depending on the length of the video the initialisation time may not be too bad.

The cvTrackbars are as you say, mainly used for altering parameters. They alter the value of a variable (given as a parameter in pointer form) and throw a callback function. Unfortunately they are the only button-style elements in HighGUI as far as I know

ianhales
Wow, thanks a lot. Guess that narrows it down for me; I thought that might be the case. I guess I have to stop being lazy and do it.
Cenoc
+4  A: 

Using cvSetCaptureProperty() you can cycle through frames, either in miliseconds or by ordinal frame number.

int cvSetCaptureProperty( CvCapture* capture, int property_id, double value );

property_id is a property you would need to use. It can be one of the following:

  1. CV_CAP_PROP_POS_MSEC - position in milliseconds from the file beginning
  2. CV_CAP_PROP_POS_FRAMES - position in frames
  3. CV_CAP_PROP_POS_AVI_RATIO - position in relative units (0 - start of the file, 1 - end of the file)
  4. CV_CAP_PROP_FRAME_WIDTH - width of frames in the video stream (only for cameras)
  5. CV_CAP_PROP_FRAME_HEIGHT - height of frames in the video stream (only for cameras)
  6. CV_CAP_PROP_FPS - frame rate (only for cameras)
  7. CV_CAP_PROP_FOURCC - 4-character code of codec (only for cameras).

The first two is of your interest.

EDIT: more info :)

You can cycle through frames just by repeatedly calling the mentioned function with various frame indices.

cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, frameIndex);

Example:

IplImage*  frame;
CvCapture* capture = cvCreateFileCapture("test.avi");

/* iterate through first 10 frames */
for (int i = 0; i < 10; i++)
{
   /* set pointer to frame index i */
   cvSetCaptureProperty(capture, CV_CAP_POS_FRAMES, i);

   /* capture the frame and do sth with it */
   frame = cvQueryFrame(capture);
}

You could put similar code to execute each time user clicks a button to forward/rewind the video.

Adi
Any examples? Say if I have a .avi file, how would I use this to cycle through the frames?
Cenoc
Thanks for the pointer by the way.
Cenoc
Edited my answer.
Adi
Thanks for the answer, worked!
Cenoc
Wow, never knew about this! Funny what gaps get left in your knowledge when your too focussed on one thing!
ianhales
A: 

By the way just as tip, the OpenCV seek doesnot work properly on flv's if you get stuck anytime figuring out seeking didn't work. Took me a day to figure it out!!!

Wajih
A: 

0 down vote By the way just as tip, the OpenCV seek doesnot work properly on flv's if you get stuck anytime figuring out seeking didn't work. Took me a day to figure it out!!!

Wajih