Home > BiKEGG > Img2Animation.m

Img2Animation

PURPOSE ^

Img2Animation

SYNOPSIS ^

function Img2Animation (Idf)

DESCRIPTION ^

 Img2Animation
 gets consecutive image files, and based on Idf value
 generates a Video or GIF file as output. Frames are sorted based on image
 numbers, which performed by SaveFlxImgs function

 O. Jamialahmadi
 TMU, Chem. Eng. Dept., Biotech. Group 
 Sep. 2015

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function Img2Animation (Idf)
0002 % Img2Animation
0003 % gets consecutive image files, and based on Idf value
0004 % generates a Video or GIF file as output. Frames are sorted based on image
0005 % numbers, which performed by SaveFlxImgs function
0006 %
0007 % O. Jamialahmadi
0008 % TMU, Chem. Eng. Dept., Biotech. Group
0009 % Sep. 2015
0010 
0011 if isempty(Idf) || Idf > 2 || Idf <= 0
0012     waitfor(msgbox('Unknown identifier (1 | 2)','Error','error'));
0013     return
0014 end
0015 % Check if image formats are consistent
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 % Video file
0042     VideoFile=strcat(ImagesFolder,'\Output video');
0043     writerObj = VideoWriter(VideoFile);
0044 %     Input dialog box for framerate
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 % GIF file
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

Generated on Sat 16-Jul-2016 20:21:30 by m2html © 2005