Ask Your Question

Iterating Through a Video Frame by Frame

asked 2013-11-27 15:12:03 -0600

dmcenan gravatar image

Hi Folks, In the code below (for playing a video), how does the capture object "know" how to iterate through the video frame by frame? Usually in a while or for loop we use some sort of incrementing technique such as i++ etc.

I think I have seen this before with stream extraction iterators where they automatically increment to read the contents of a buffer but I am not sure how it works. I tried to view the contents of the Videocapture class but can only find the definition (which includes an overloaded>> function) but can not see how it is implemented.


int main()

{ namedWindow("Video"); VideoCapture cap;"video.avi");

Mat frame;


    imshow("Video", frame);

    if(waitKey(300) >= 0)   


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-11-27 19:02:23 -0600

Hi dmcenan!

This works thanks to the magic of OOP (Object Oriented Programming), you can checkout the source code of highgui/src/cap.cpp where the overloaded >> is implemented (as you observed) and if you follow the track of that overloaded method you will reach the file highgui/src/cap_ffmpeg_impl.hpp which implements the method:


That method is in charge of grabbing a frame from a video file source, and at some point after some checks and decodification stuff, there is a wonderful


Which causes the VideoCapture object give you the next frame on the video stream the next time you call cap >> frame

I hope this helps.

edit flag offensive delete link more



Hello Martin, Thank you for taking the time to compose a concise and clear explanation. This helps a lot! Regards, dmcenen.

dmcenan gravatar imagedmcenan ( 2013-11-27 22:57:37 -0600 )edit

Question Tools


Asked: 2013-11-27 15:12:03 -0600

Seen: 5,820 times

Last updated: Nov 27 '13