Is there a way of randomly reading video frames in OpenCV....just like arrays are accessed using indexes? Otherwise, if I want to load complete video in CPU or GPU how can I do it?
Asked
Active
Viewed 904 times
1 Answers
5
You can use the set(int propId, double value)
method on your video capture (also check out the documentation), where propId
can either be one of the following:
CV_CAP_PROP_POS_MSEC
: Current position of the video file in milliseconds.CV_CAP_PROP_POS_FRAMES
: 0-based index of the frame to be decoded/captured next.CV_CAP_PROP_POS_AVI_RATIO
: Relative position of the video file: 0 - start of the film, 1 - end of the film.
A small example that plays a video 50 seconds in:
int main( int argc, char **argv )
{
namedWindow("Frame", CV_WINDOW_NORMAL);
Mat frame;
VideoCapture capture(argv[1]);
if (!capture.isOpened())
{
//error in opening the video input
cerr << "Unable to open video file: " << argv[1] << endl;
exit(EXIT_FAILURE);
}
capture.set(CV_CAP_PROP_POS_MSEC, 50000);
for (;;)
{
//read the current frame
if (!capture.read(frame))
{
cerr << "Unable to read next frame." << endl;
cerr << "Exiting program!" << endl;
exit(EXIT_FAILURE);
}
imshow("Frame", frame);
waitKey(20);
}
}

tschale
- 975
- 1
- 18
- 34
-
Thank you very much :).... Solved my problem. Just one more question...would this accessing random frame would be an O(1) complexity operation? – user3566188 May 13 '14 at 07:42
-
I'm afraid I'm not able to help you on this one. I couldn't find anything after a quick google search... But it would be quite interesting. So if you happen to find out something, please keep me informed ;) – tschale May 13 '14 at 07:51
-
I computed time for setting to a desired frame...the result were: frame_index -> time taken (ms) => 0 -> 0.022 ms => 50 -> 37.44 ms => 100 -> 75.48 ms => 400 -> 123.21 ms – user3566188 May 13 '14 at 08:04
-
Implies it is an time consuming operation...hey, please have a look at my other question and see if you can help.http://stackoverflow.com/questions/23625487/increasing-stack-memory-for-image-procesing-c-opencv – user3566188 May 13 '14 at 08:07