Reading in only specific video frames with readFrame
35 views (last 30 days)
Show older comments
Is there any way to read in only specified frames from a video using readFrame? It is possible using VideoReader.read, which accepts an "index" argument to specify which frames are to be read:
video = read(v,index)
But readFrame doesn't have the same option. The read function is being deprecated in a future release, and I would like to ensure that the code that I am writing will continue to work as long as possible into the future. Is this functionality being lost?
The code will potentially need to handle very large video files, so efficiency is important here - I can't just read in every single frame and discard the ones that are not required.
2 Comments
Swarooph
on 9 Aug 2016
The same documentation says VideoReader will replace this functionality. You can try using that. Although it does not seem to have the ability to read a particular frame #, it does have the ability to specify CurrentTime of frame and read between intervals.
Answers (2)
Patrick Feaster
on 1 Sep 2017
I ran into the same issue. The solution I came up with, presuming a constant frame rate, is to detect the video frame rate and then divide the desired frame index number, minus 1, by it. Thus, if you want frame index number 10 and your frame rate is 30 fps, set (10-1)/30 as the current time before using readFrame. Frame index number 1 comes out as (1-1)/30 = current time 0. The following function "readindex" can also substitute for "read" with an index specified and should continue to work after "read" is deprecated, with minimal alteration to existing code. Again, this would only work with a constant frame rate.
function outputFrame=readindex(videoSource,frameNumber)
info=get(videoSource);
videoSource.CurrentTime=(frameNumber-1)/info.FrameRate;
outputFrame=readFrame(videoSource);
3 Comments
Iris Hagoort
on 3 Jan 2017
Have you already found the solution? I also want to change my code so that it will stay functional in the future. I have build a GUI with a scroller, you can open the movie and use the scroller to go to the desired frame. Are there any other solutions?
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!