Filter löschen
Filter löschen

How to read a video file and save it to multiple frames in numerical order? Because I keep getting back random frames not in order of the video progression

1 Ansicht (letzte 30 Tage)
My current code is
vid = VideoReader('vid.mp4');
numrames = vid.NumberofFrames;
n = numFrames;
for i = 1:1:n
frames = read(vid,i);
imwrite(frames,['mydirectory\'int2str(i),'.jpg']);
im(i) = image(frames);
end
I know that we can use natsortfiles to read a folder of images in numerical order, but how do you do it with a video file?
Thanks in advance

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 23 Apr. 2020
Manuella - does the because I keep getting back random frames not in order of video propression refer to the files that you have created or the images you see in your figure? If the former, try padding some zeros to the integer file name as
imwrite(frames,sprintf('mydirectory\%05d.jpg', i));
so that the file names are like 00001.jpg, 00002.jpg, etc.
If the latter (images you see are in random order) you may need to pause after displaying the image. The pause should be the inverse of the frame rate
vid = VideoReader('vid.mp4');
numFrames = vid.NumberofFrames;
frameRate = vid.FrameRate;
n = numFrames;
for i = 1:1:n
frames = read(vid,i);
imwrite(frames,sprintf('mydirectory\%05d.jpg', i));
im(i) = image(frames);
pause(1/frameRate);
end
Also, depending upon your version of MATLAB, consider using the readFrame function.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by