How to make a video from images

1,059 views (last 30 days)
I have 3 still images saved in .png format. I would like to create a movie from these still images having control over the time interval between each frame of display using Matlab. Also I would like to draw a square/rectangle so that the white spot lies almost within the center of the square.
I will be grateful if someone helps me in this regard.
Thanking You!

Accepted Answer

Geoff Hayes
Geoff Hayes on 8 Sep 2014
Edited: Geoff Hayes on 8 Sep 2014
Pranjal - consider using the videowriter to make your movie. Since you only have three images, I would set the frame rate to one frame per second, and then just copy the same image for that number of seconds you want it displayed until the next one appears. For example, suppose that you want the first image to be present for 5 seconds, the second for 10 seconds, and the third for 15 seconds. You could do something like the following
% load the images
images = cell(3,1);
images{1} = imread('im1.png');
images{2} = imread('im2.png');
images{3} = imread('im3.png');
Since you are adding all three images to the same movie, then each must have the same dimensions - the number of rows and columns for each image must be identical for all three images. I noticed that all three are of different sizes, so you will want to use imresize (or an alternative) to resize all three images to the same mxnx3 dimension. Once you have done that, then do
% create the video writer with 1 fps
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 1;
% set the seconds per image
secsPerImage = [5 10 15];
% open the video writer
% write the frames to the video
for u=1:length(images)
% convert the image to a frame
frame = im2frame(images{u});
for v=1:secsPerImage(u)
writeVideo(writerObj, frame);
% close the writer object
The above code will produce a 30 second movie.
As for drawing a white square or rectangle around the white dot, you could just manipulate the three images after you've resized them, executing code to draw four white lines. For example, we could draw the white rectangle for the first image as
images{1}(100,85:145,:) = 255;
images{1}(150,85:145,:) = 255;
images{1}(100:150,85,:) = 255;
images{1}(100:150,145,:) = 255;
The first two statements draw the horizontal lines (at rows 100 and 150, columns 85 through 145). The last two statements draw the vertical lines (at rows 100 through 150, columns 85 and 145). You can repeat this (or something similar) for the other two images.
Image Analyst
Image Analyst on 27 Mar 2022
@Thomas Carter Maybe use ind2rgb() to convert your indexed image into an RGB image. Or see my attached demo.

Sign in to comment.

More Answers (1)

Manoj Kumar
Manoj Kumar on 21 Feb 2018
Dear Sir,
I have made a movie by using 'implay' command in matlab. Now I want to save this as a movie clip. Please guide me to do so.
Thanking you.
  1 Comment
Geoff Hayes
Geoff Hayes on 21 Feb 2018
Manoj - please post this as a question rather than as an answer (which it isn't). Provide as much details as you can including any code that you have written that you feel may be relevant to your question.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!