How to make a video from images
521 次查看(过去 30 天)
显示 更早的评论
Hi,
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!
0 个评论
采纳的回答
Geoff Hayes
2014-9-8
编辑:Geoff Hayes
2014-9-8
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
open(writerObj);
% 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);
end
end
% close the writer object
close(writerObj);
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.
25 个评论
Geoff Hayes
2022-3-27
@Thomas Carter - you may need to show more of your code so that we can get an idea of what you have tried to do.
Image Analyst
2022-3-27
@Thomas Carter Maybe use ind2rgb() to convert your indexed image into an RGB image. Or see my attached demo.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!