image thumbnail
from Mosaic random-images by Yasser Arafat
This program creates random mosaic / tile image

[NoorImage resized]=YDynamicTiling(URows,UCols,NImgRows,NImgCols,ShowFlag)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Developed by                                                                  %
%             Syed Yasser Arafat                                                %
%             Riphah International University, Islamabad,Pakistan               %
%             Dated: 16 Nov 2008                                                %
%             Topic: ImageTiling/ImageMosaic                                    %
%             Copyrights: Only allowed for educational purposes                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [NoorImage resized]=YDynamicTiling(URows,UCols,NImgRows,NImgCols,ShowFlag)

UserRows=URows;
UserCols=UCols;
ShoFlg=ShowFlag;

NewImageRows=NImgRows;
NewImageCols=NImgCols;

if  (mod(NewImageRows,UserRows)==0) 
    ReSizeToUserDimension=0;
else
    ReSizeToUserDimension=1;
end

if  (mod(NewImageCols,UserCols)==0 )
    ReSizeToUserDimension=0;
else
    ReSizeToUserDimension=1;
end

NewRsizeRows=floor(NewImageRows/UserRows);
NewRsizeCols=floor(NewImageCols/UserCols);

jpg_files = dir(fullfile([pwd '\images'],'*.jpg'));
if (size(jpg_files,1)< 1)
    disp('No files found.......  Quiting.....');
    return;
end

Color_Image=1;
fileJPG_path=[pwd '\images\' jpg_files(1).name];
test_i=imread(fileJPG_path);
if (size(test_i,3)>2)
else
    Color_Image=0;
end


n=size(jpg_files,1);
f = ceil(n.*rand(n,1));
noorRandom=f;
for k=1:size(jpg_files,1)
     fileJPG_path=[pwd '\images\' jpg_files(k).name];
     i=imread(fileJPG_path);
        resized(k).imageData=imresize(i,[NewRsizeRows NewRsizeCols]);
 end

 CValue=NewRsizeCols;
 RVal=NewRsizeRows;
 
 k=0;
 for i=0:UserCols-1
     for j=0:UserRows-1
        k=k+1;
        k=mod(k,n)+1;
           if Color_Image==0
                 NoorImage(((j*RVal)+1):((j*RVal)+ RVal),((i*CValue)+1):((i*CValue)+ CValue)) =resized(noorRandom(k)).imageData; 
           else
                 NoorImage(((j*RVal)+1):((j*RVal)+ RVal), ((i*CValue)+1):((i*CValue)+ CValue),1) =resized(noorRandom(k)).imageData(:,:,1);  
                 NoorImage(((j*RVal)+1):((j*RVal)+ RVal), ((i*CValue)+1):((i*CValue)+ CValue),2) =resized(noorRandom(k)).imageData(:,:,2);  
                 NoorImage(((j*RVal)+1):((j*RVal)+ RVal), ((i*CValue)+1):((i*CValue)+ CValue),3) =resized(noorRandom(k)).imageData(:,:,3);  
           end
     end
 end

if ShoFlg == 1
        if ReSizeToUserDimension==1
            disp('Not Divisible Exactly..... so .. being resized...............');
            figure,imshow(imresize(NoorImage,[NewImageRows NewImageCols]));
        else
            figure,imshow(NoorImage);
        end
end

Contact us at files@mathworks.com