randomly picking a fram from a video in matlab

my problem is that i want to randomly pick a frame from a video , using mmreader , and also i am getting the last frame of the video so that i can set limit, so that when using rand function the number can be picked from 1 to the last frame number,the code is;
readerobj = mmreader(myvideoname);
last_Frame_number = read(readerobj, inf);
end_frame=readerobj.NumberOfFrames;
random_frame=randi([1,end_frame]);
can someone tell a better solution because this take alot of time because every video has a varying size , and i want to make it more efficient.

3 Comments

Do the videos have variable-rate frames, or just that the videos are different sizes? Variable-rate frames require something like what you did.
no videos are of different sizes.
and longer the video size more time it takes as it needs to fetch the last frame number

Sign in to comment.

 Accepted Answer

For videos that are not variable-frame-rate, the NumberOfFrames property will be set when the object is created. If you are not getting a warning when you create the object (before you even do anything to it) then the NumberOfFrames will reflect the total number in the file.

10 Comments

yes i understand that, but this process is slow, isn't there a way where i can randomly read a frame from a video that can make it more efficient?
i am not getting any warning as well
You have changed your code to not use read(readerobj, inf) now ?
You just have
readerobj = mmreader(myvideoname);
end_frame=readerobj.NumberOfFrames;
random_frame=randi([1,end_frame]);
?
i didn't get what you are trying to say, can you elaborate please?
Have you deleted (or commented out) the read(readerobj,inf) line and tried the timing without it?
if i comment is , it means that it will read who video at once, and this results out of memory error.
Your line of code
last_Frame_number = read(readerobj, inf);
is the only line in that series that reads any frames. If you get rid of that line of code, then no frames will be read before the random frame number is determined.
so this means theres no alternative?
Show your current code, and indicate which line it runs out of memory on.
readerobj = mmreader('myvideo.mpg');
last_Frame = read(readerobj, inf);
last_Frame_number=readerobj.NumberOfFrames;
frame_rand=randi([1,last_Frame_number]);
vidFrames = read(readerobj,frame_rand);
this is my code, it takes alot of time, and if i run on loop like for 5 times, it gives timeout error.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!