0001 function Img2Animation (Idf)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 if isempty(Idf) || Idf > 2 || Idf <= 0
0012 waitfor(msgbox('Unknown identifier (1 | 2)','Error','error'));
0013 return
0014 end
0015
0016 ImagesFolder=uigetdir;
0017 TempF=dir(ImagesFolder);
0018 TempF1=TempF(3).name;
0019 T1=strfind(TempF1,'.');
0020 TempF2=TempF1(T1+1:end);
0021 if size(TempF,1) < 4
0022 msgbox(['There is only one image ',...
0023 'in this directory!'],'Warning','warn');
0024 return
0025 end
0026 TempG1=TempF(4).name;
0027 G1=strfind(TempF1,'.');
0028 TempG2=TempG1(G1+1:end);
0029 if ~strcmp(TempF2,TempG2)
0030 msgbox(['File types in the chosen ',...
0031 'folder are not consistent!'],'Warning','warn');
0032 return
0033 end
0034
0035 ImgFiles = dir(strcat(ImagesFolder,'\*',TempF2));
0036
0037 S = [ImgFiles(:).datenum];
0038 [~,S] = sort(S);
0039 ImgFilesS = ImgFiles(S);
0040
0041 if Idf == 1
0042 VideoFile=strcat(ImagesFolder,'\Output video');
0043 writerObj = VideoWriter(VideoFile);
0044
0045 prompt='Insert the desired framerate value:';
0046 name='Frame rate: ';
0047 numlines=1;
0048 defaultanswer={'1'};
0049 options.Resize='on';
0050 options.WindowStyle='normal';
0051 options.Interpreter='tex';
0052 answer=inputdlg(prompt,name,numlines,defaultanswer,options);
0053 fps=str2double(answer(1));
0054 writerObj.FrameRate = fps;
0055 open(writerObj);
0056 for t= 1:length(ImgFilesS)
0057 Frame=imread(strcat(ImagesFolder,'\',ImgFilesS(t).name));
0058 writeVideo(writerObj,im2frame(Frame));
0059 end
0060 close(writerObj);
0061 elseif Idf == 2
0062 GIFfilename = strcat(ImagesFolder,'\OutputAnimatedImg.gif');
0063 for t= 1:length(ImgFilesS)
0064 Frame=imread(strcat(ImagesFolder,'\',ImgFilesS(t).name));
0065 [imind,cm] = rgb2ind(Frame,256);
0066 if t == 1;
0067 imwrite(imind,cm,GIFfilename,'gif','LoopCount',Inf,'DelayTime',1);
0068 else
0069 imwrite(imind,cm,GIFfilename,'gif','WriteMode','append','DelayTime',1);
0070 end
0071 end
0072 end