Create a movie from images

48 次查看(过去 30 天)
N B
N B 2016-3-4
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.

采纳的回答

N B
N B 2016-3-4
编辑:N B 2016-3-4
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 个)

Guillaume
Guillaume 2016-3-4
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!

Translated by