MRimg2video

This is useful for creating animations from existing image files. Look at an existing folder and if there are image files present, format into a movie at a specified frame rate.

 

function MRimg2video(folder, fileName, frameRate)
%
% grab .png files from folder and create a .avi with given framerate
%
% folder = folder where the pngs are
%
% fileName = the filename the final video will have
%
% frameRate = the frameRate to use
%

if (exist(folder, ‘dir’) ~= 7)
error(‘folder does not exist’);
end

im_files = [];
im_files = [im_files, dir(‘*.png’)];
im_files = [im_files, dir(‘*.jpg’)];
im_files = [im_files, dir(‘*.jpeg’)];
im_files = [im_files, dir(‘*.tif’)];
im_files = [im_files, dir(‘*.bmp’)];

%%%%add any other file extensions you would like to load here…

if length(im_files) > 0

myVideo = VideoWriter(fileName);
myVideo.FrameRate = frameRate;
open(myVideo);

more off;
for file = 1:length(im_files)

fprintf(‘\n frame = %d’, file);
writeVideo(myVideo, imread(im_files(file).name));

end
close(myVideo);

fprintf(‘\n video done \n’);
else
error(‘no image files found’);
end

end

download_mrimg2video