Code covered by the BSD License  

Highlights from
Mosaic generator

image thumbnail

Mosaic generator

by

 

15 Jan 2011 (Updated )

Photo mosaic generator. Choose a tile image, and a directory with images, and see the magic.

mosaic_imges=prepare_mosiac_elements(mosiacFiles, mosaicHeigth, mosaicWidth, neededNOfFiles)
function mosaic_imges=prepare_mosiac_elements(mosiacFiles, mosaicHeigth, mosaicWidth, neededNOfFiles)
if nargin<4
    neededNOfFiles=Inf;
end

%% get all imge files from user defined directory
imageFormats=imformats;
imageFormatsExtCell=cat(2, imageFormats.ext);
nFiles=length(mosiacFiles);
isRelevantImageFile=false(1, nFiles);
iMosaic=1;
for iFile=1:nFiles
   [~, ~, fileEx] = fileparts(mosiacFiles{iFile});
   if any(strcmpi(fileEx(2:end), imageFormatsExtCell)) % if the current file is graphical
      isRelevantImageFile(iFile)=true;
      iMosaic=iMosaic+1;
      if (iMosaic==neededNOfFiles)
          break; % no need to get more then neededNOfFiles 
      end
   end
end
chosenMosiacFiles=mosiacFiles(isRelevantImageFile);

%% Read all images, and for each do the following:
% (1) make it square- what if mosaicHeigth~=mosaicWidth?
% (2) resize to [mosaicHeigth,mosaicWidth]
h = waitbar(0,'Reading mosaic images.');
nFiles=length(chosenMosiacFiles);
mosaic_imges=cell(1,nFiles);
for iMosaic=1:nFiles
    img=imread(chosenMosiacFiles{iMosaic});
    [img_rows, img_cols, clr_dim]=size(img);
    if img_rows>img_cols
        diff=img_rows-img_cols;
        img=img(1+ceil(diff/2):end-floor(diff/2),:,:);  
    elseif img_cols>img_rows
        diff=img_cols-img_rows;
        img=img(:,1+ceil(diff/2):end-floor(diff/2),:);  
    end
    res_img=uint8(imresize(img,[mosaicHeigth,mosaicWidth]));
    if clr_dim==1
            mosaic_imges{iMosaic}=repmat(res_img,[1,1,3]);
    else
            mosaic_imges{iMosaic}=res_img;
    end
    waitbar(iMosaic / nFiles,h);
end
close(h);

Contact us