How do I create an animation video in MATLAB by importing a set of images?

83 次查看(过去 30 天)
I have a set of 100 images (frames) and I want to create an animation video from this using MATLAB. How can I do this?
Right now I'm using the following code in MATLAB itself to generate and save the images in a specific folder on my PC. I want to make an animation from these saved images.
x = 1:44;
y = 1:44;
for i = 1:100 a = xlsread('\\uoa.auckland.ac.nz\air.xlsx',strcat('A',int2str(46*i-18),':','AR',int2str(46*i+25)));
figure;
surf(x,y,a);
axis([0 44 0 44 0 1100]);
fname = sprintf('A%d.png',i);
saveas(gcf,fname);
end

采纳的回答

Image Analyst
Image Analyst 2016-5-15
See the latter half of the attached demo, where I create a movie from a bunch of still frames saved on disk as individual images.
  3 个评论
Image Analyst
Image Analyst 2016-5-15
The relevant part is this:
% Create a VideoWriter object to write the video out to a new, different file.
writerObj = VideoWriter('NewRhinos.avi');
open(writerObj);
% Read the frames back in from disk, and convert them to a movie.
% Preallocate recalledMovie, which will be an array of structures.
% First get a cell array with all the frames.
allTheFrames = cell(numberOfFrames,1);
allTheFrames(:) = {zeros(vidHeight, vidWidth, 3, 'uint8')};
% Next get a cell array with all the colormaps.
allTheColorMaps = cell(numberOfFrames,1);
allTheColorMaps(:) = {zeros(256, 3)};
% Now combine these to make the array of structures.
recalledMovie = struct('cdata', allTheFrames, 'colormap', allTheColorMaps)
for frame = 1 : numberOfFrames
% Construct an output image file name.
outputBaseFileName = sprintf('Frame %4.4d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
% Read the image in from disk.
thisFrame = imread(outputFullFileName);
% Convert the image into a "movie frame" structure.
recalledMovie(frame) = im2frame(thisFrame);
% Write this frame out to a new video file.
writeVideo(writerObj, thisFrame);
end
close(writerObj);
% Get rid of old image and plot.
delete(hImage);
delete(hPlot);
% Create new axes for our movie.
subplot(1, 3, 2);
axis off; % Turn off axes numbers.
title('Movie recalled from disk', 'FontSize', fontSize);
% Play the movie in the axes.
movie(recalledMovie);
% Note: if you want to display graphics or text in the overlay
% as the movie plays back then you need to do it like I did at first
% (at the top of this file where you extract and imshow a frame at a time.)
msgbox('Done with this demo!');
Now half the lines, or more, are just comments so you can get rid of those but I'd not recommend that. Sorry - that's just the way I write my demos, with lots of comments so people can follow along, though I know it makes the code seem longer and more complicated. If you want to get rid of the fprintf(), movie(), delete(), and msgbox() and most other stuff after the for loop (except for close(writerObj);), you can do that. If you do all that, you'll have just like 10 lines of code or so. (If someone later sees that cryptic uncommented code, they may ask you to put in comments to explain it.)

请先登录,再进行评论。

更多回答(1 个)

parth pandya
parth pandya 2016-5-15
Hi Jasnoor, Image Analyst usually includes lot's of validation in his code so,that actually people can get idea that what is error. But study entire code of him whenever you feel good with matlab.
Now,to answer your question, By animation video there is lots of possibility in matlab like you can create GIF files, mp4 or AVI videos, AVI videos with sound ( I don't know about mp4 with sound.)
I will try to include all:
1) For GIF you can use following code:
x = 0:0.01:1;
figure(1)
filename = 'E:\store\testnew51.gif';
for n = 1:0.5:5
y = x.^n;
plot(x,y)
drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
just set proper path in "filename" variable it will give you idea.
you can also use following code:
x=rand(100,10);
plot(x(:,1))
figure,
f = getframe;
[im,map] = rgb2ind(f.cdata,256,'nodither');
for k=1:10,
plot(x(:,k));
f=getframe;
im(:,:,1,k) = rgb2ind(f.cdata,map,'nodither');
end
imwrite(im,map,'E:\store\imagefile.gif','DelayTime',0,'LoopCount',inf);
To convert AVI file into GIF file:
vrinfo = aviinfo('shuttle.avi');
filename = 'E:\store\test2.gif';
mov1 = mmreader('shuttle.avi');
vidFrames = read(mov1);
for n = 1:vrinfo.NumFrames
[imind,cm] = rgb2ind(vidFrames(:,:,:,n),255);
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
This will run directly as shuttle.avi is matlab provided file.
2) To create MP4 Video:
I am not able to include all images here bt it will give you idea about creating video from still images.Remember don't try to create too low frame rate video < 10 fps.
function CreateVideo()
global BaseDrive;
global Folder;
global SubFolder;
global FileFormat;
global nFramesWithFace;
global FrameRate;
TimeSet = [ 1.5, ... %1
1.5, ... %2
1.5, ... %3
1.5, ... %4
0.5, ... %5
0.5, ... %6
0.5, ... %7
0.5, ... &8
0.5, ... %9
2, ... %10
1.5, ... %11
1.5, ... %12
1.5, ... %13
1.5, ... %14
3]; ... %15
% Creating an output video.
outputVideo = VideoWriter( fullfile( BaseDrive, Folder, SubFolder,'Friends.mp4') , 'MPEG-4');
outputVideo.FrameRate = 20;
open(outputVideo);
for ii = 1:nFramesWithFace
img = imread(fullfile( BaseDrive, Folder, SubFolder, [sprintf('%01d',ii) FileFormat]));
nRequiredFrames = round( TimeSet(ii) * FrameRate );
for loop = 1:nRequiredFrames
writeVideo(outputVideo,img);
end
end
close(outputVideo);
end
Regards, parth

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by