How to make movies in MATLAB?

73 views (last 30 days)
Mark Erick
Mark Erick on 4 Nov 2014
Commented: Image Analyst on 10 Jun 2021
my instructor required me to make a movie of my life using MATLAB, i have pictures but i don't know how to import them or make them a slide show. can anybody help me with this? thanks God Bless :)

Answers (2)

Geoff Hayes
Geoff Hayes on 4 Nov 2014
Edited: Geoff Hayes on 4 Nov 2014
Mark - use imread to read in the individual images. Depending upon the number, you may want to code up a for loop to read each image from a directory/folder. All images will need to be of the same size (height and width will need to be the same for each image), so you may need to resize each. If you have the Image Processing Toolbox, you can use imresize to do this.
To create the video, use the videowriter. From their second example you could do something like
% create video writer object
writerObj = VideoWriter('myLife.avi');
% set the frame rate to one frame per second
set(writerObj,'FrameRate',1);
% open the writer
open(writerObj);
% iterate over each image
for k=1:numImages
% use imread to read the image
img = imread(...);
% resize the image
img = imresize(img,...);
% convert the image to a frame using im2frame
frame = im2frame(img);
% write the frame to the video
writeVideo(writerObj,frame);
end
% close the writer
close(writerObj);
Each image needs to be converted to a frame before writing it to file. We use im2frame to do this.
The above isn't tested but should give you a good starting point.

Image Analyst
Image Analyst on 9 Nov 2014
  2 Comments
Victoria Boatwright
Victoria Boatwright on 9 Jun 2021
I just used your demo for my own plots and it was super helpful! I'm wondering if there is a way to grab the image in the figure as well as any axis titles and colorbars (aka the entire window that shows in the pop-up figure)? Thank you so much!
Image Analyst
Image Analyst on 10 Jun 2021
@Victoria Boatwright, if you want to write out each individual frame to the drive, you can insert this code into the loop:
%------------------------------------------------------------------------------------------------------------------------------------------
% OPTIONAL - ONLY IF YOU WANT TO SAVE EXTRACTED FRAMES.
% Write the image array to the output file, if requested.
if writeToDisk
% Construct an output image file name.
outputBaseFileName = sprintf('Frame %4.4d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
if hasComputerVisionToolbox
% User has the Computer Vision Toolbox
% so we can use insertText() to burn frame stamp into image, if desired.
if wantsFrameStamps
thisFrame = insertText(thisFrame, [5, 5], caption, 'FontSize', 20, 'TextColor', 'yellow', 'BoxColor', 'black'); % Burn text into image
end
imwrite(thisFrame, outputFullFileName) % Original size image
else
% No Computer Vision Toolbox, so can't use insertText(). Must use text() instead.
if wantsFrameStamps
% Stamp the name and frame number onto the image.
% At this point it's just going into the overlay,
% not actually getting written into the pixel values.
text(5, 5, caption, 'FontSize', 20);
% Extract the image with the text "burned into" it.
frameWithText = getframe(gca); % Get screenshot of just this axes.
% frameWithText.cdata is the image with the text
% actually written into the pixel values.
text(x, y, caption); % Put text into overlay, rather than burn it into image.
thisFrame = frameWithText.cdata;
if wantsSameSize
% Screenshot does not have the same size. Need to force it to have the same size.
thisFrame = imresize(thisFrame, [vidHeight, vidWidth]);
% It's the same size but may be blurry.
end
else
% thisFrame = original image (nothing to do)
% Image will have a different size
end
% Write it out to disk.
imwrite(thisFrame, outputFullFileName, 'png');
end
end
%------------------------------------------------------------------------------------------------------------------------------------------
See attached example. Adapt as needed.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!