How can I read only selected frames of a video without using a loop?

How can I read only selected frames of a video without using a loop?
For example, I want to read every 10 frames of a video. The command:
Video_Obj = VideoReader('myvideo.mp4'); read(Video_Obj,[1:10:5000]);
does not work as read() only needs a start and stop frame to read all the frames in between. Obviously, I do not want to read all the frames and then keep every 10 frames (for memory and efficiency reasons).
I appreciate your comments. Thanks!

Answers (1)

I would just use a for-loop and loop over the values you want to read specifying each at a time.
idx = [567 580:590];
for ii = numel(idx:-1:1)
C(ii) = read(vidObj,idx(ii))
end
Something like the above (not tested in MATLAB!)

4 Comments

Thanks Sean. I was trying to avoid the loop assuming that reading a whole bunch of frames should be faster than the loop. I don't see why read() function does not have such capability.
Don't worry about using a for loop. 5000 or even 50,000 iterations of a for loop will happen faster than a blink of an eye . You aren't being slowed down by the fact that you're using a for loop. The read() function takes up way more time than the time to increment a loop variable.
Not to mention that the engine under read() is going to be called inside of a for-loop within the read() method anyway...
Well we don't know that. What we (as users) know is that there is a fair bit of overhead in firing up a read() call on video objects.
It would be quite reasonable for us to speculate that there is no frame number index in the file that points directly to the byte at which the frame starts. That is reasonable as we know that the file format does not have provision for such indices. And with backwards references and forwards references, it would have to point to the relevant keyframe anyhow. So it would be fair for us to expect that in order to find a particular frame, the read() might need to start at the beginning of the file and decode data enough to determine how many frames it is skipping over in order to find a particular frame. And that would reasonably lead us to expect that if we could provide a list of frame numbers to read, that the read() operation would be faster as it would not need to go back to the beginning each time.

Sign in to comment.

Asked:

on 8 Nov 2013

Commented:

on 8 Nov 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!