Create a movie from images

44 views (last 30 days)
N B
N B on 4 Mar 2016
Edited: Josep Llobet on 4 Aug 2021
I have saved some images to a directory and would now like to play these as a movie.
This is what i have done:
% load the images
images = cell(30,1);
for i=1:30
images{i} = imread(sprintf('%d.jpg',i));
end
% create the video writer with 30 fps
writerObj = VideoWriter('Velocity.avi');
writerObj.FrameRate = 30;
% open the video writer
open(writerObj);
% write the frames to the video
for u=1:30
% convert the image to a frame
frame = im2frame(images{u});
for v=1:30
writeVideo(writerObj, frame);
end
end
% close the writer object
close(writerObj);
implay('Velocity.avi');
This creates a video, but the frame rate is not what i expected; it creates a video of 900 frames and jumps 30 frames a second.
I guess writerObj.FrameRate = 30 doesn't do what i originally thought. Nut now i'm unsure how to get it to play at a rate of 30 frames per second.
Also, why does the video have 900 frames when i only have 30 images?
Help much appreciated.

Accepted Answer

N B
N B on 4 Mar 2016
Edited: N B on 4 Mar 2016
Scratch that.
Just figured it out. I didn't need:
for v=1:30
writeVideo(writerObj, frame);
end
in the loop.
It's odd, spent an hour trying to figure this out and within seconds of posting this the solution came to me.
  1 Comment
Josep Llobet
Josep Llobet on 4 Aug 2021
Edited: Josep Llobet on 4 Aug 2021
True!
I did the same.
Thank you for the code

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 4 Mar 2016
I don't think that the call to im2frame is necessary. You can directly write the image returned by imread to the video. (I've never used im2frame, I'm not sure what effect it has in your case).
For each image, you have a v loop that writes it 30 times. 30 images written 30 times = 900 frames. The frame rate does what you asked it. It plays the video at 30 frames per second, but since each group of 30 frame is identical you have in effect 1 different image per second.
Why are writing the same image 30 times?

Community Treasure Hunt

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

Start Hunting!