Wednesday, July 2, 2008

Photomosaic... Finally





Here it is...after days of messing about, trying to find a short set of commands to define and read in/replace the 27 small pictures (with no success).

The images are in reverse order: final output, 27 colours, enlarged, 40 x 40 matrix, and the original image.

The original is from a photo I took in 2003 on a trip "home" with my son and my mother. The elevator is located a mile or so down the road from the farm I lived on as a child. The old red elevators have all but disappeared (as has the town of Nut Mountain, Sask.) but a few were sold to private owners and maintained.

I will post the array of small pictures I used as soon as I can organize it for the blog.



The Code:


#Set up Octave

pkg load image
cd 'C:\pictures'

#read in the original image

B=imread ("Nutmountainsquare.jpg");
B=double(B)/255;
size(B)
imshow(B)

#shrink the image

function Step1 = PM1(A,f)
rows=floor(size(A,1)*f);
cols=floor(size(A,2)*f);
for i=1:rows;
for j=1:cols;
a=round(i/f);
b=round(j/f);
Step1(i,j,:)=A(a,b,:);
end;
end;
endfunction

PM2=PM1(B,1/12.5);
size(PM2)
imshow(PM2)

#reduce to 27 colours

PM3=floor(255*(PM2)/86)*86 + 42;
imshow(double(PM3)/255)
size(PM3)

#enlarge the image

function largeimage=grow1(smallimage,f)
rows=size(smallimage,1);
cols=size(smallimage,2);

Mp=floor(size(smallimage,1)*f);
Np=floor(size(smallimage,2)*f);

for i=0:(Mp-1);
for j=0:(Np-1);
a=round((i/f)+1);
b=round((j/f)+1);

if (a(greaterthan)0) & (a(lessthan)rows) & (b(greaterthen)0) & (b(lessthan)cols)
largeimage(i+1,j+1,:)=smallimage(a,b,:);
end;
end;
end;
endfunction;


PM4=grow1(PM3,15);
size(PM4)
imshow(double(PM4)/255)

#read in the 27 pictures

cd 'C:\pictures\27pics'

B01=imread('one.jpg');
B02=imread('two.jpg');
B03=imread('three.jpg');
B04=imread('four.jpg');
B05=imread('five.jpg');
B06=imread('six.jpg');
B07=imread('seven.jpg');
B08=imread('eight.jpg');
B09=imread('nine.jpg');
B10=imread('ten.jpg');
B11=imread('eleven.jpg');
B12=imread('twelve.jpg');
B13=imread('thirteen.jpg');
B14=imread('fourteen.jpg');
B15=imread('fifteen.jpg');
B16=imread('sixteen.jpg');
B17=imread('seventeen.jpg');
B18=imread('eighteen.jpg');
B19=imread('nineteen.jpg');
B20=imread('twenty.jpg');
B21=imread('twentyone.jpg');
B22=imread('twentytwo.jpg');
B23=imread('twentythree.jpg');
B24=imread('twentyfour.jpg');
B25=imread('twentyfive.jpg');
B26=imread('twentysix.jpg');
B27=imread('twentyseven.jpg');

#create oneD array of small pictures

Y=[B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12,B13,B14,B15,B16,B17,B18,B19,B20,B21,B22,B23,B24,B25,B26,B27];

#create matrix of 27 rgb values

A01(:,:,1)=42;
A01(:,:,2)=42;
A01(:,:,3)=42;

A02(:,:,1)=42;
A02(:,:,2)=42;
A02(:,:,3)=128;

A03(:,:,1)=42;
A03(:,:,2)=42;
A03(:,:,3)=214;

A04(:,:,1)=42;
A04(:,:,2)=128;
A04(:,:,3)=42;

A05(:,:,1)=42;
A05(:,:,2)=128;
A05(:,:,3)=128;

A06(:,:,1)=42;
A06(:,:,2)=128;
A06(:,:,3)=214;

A07(:,:,1)=42;
A07(:,:,2)=214;
A07(:,:,3)=42;

A08(:,:,1)=42;
A08(:,:,2)=214;
A08(:,:,3)=128;

A09(:,:,1)=42;
A09(:,:,2)=214;
A09(:,:,3)=214;

A10(:,:,1)=128;
A10(:,:,2)=42;
A10(:,:,3)=42;

A11(:,:,1)=128;
A11(:,:,2)=42;
A11(:,:,3)=128;

A12(:,:,1)=128;
A12(:,:,2)=42;
A12(:,:,3)=214;

A13(:,:,1)=128;
A13(:,:,2)=128;
A13(:,:,3)=42;

A14(:,:,1)=128;
A14(:,:,2)=128;
A14(:,:,3)=128;

A15(:,:,1)=128;
A15(:,:,2)=128;
A15(:,:,3)=214;

A16(:,:,1)=128;
A16(:,:,2)=214;
A16(:,:,3)=42;

A17(:,:,1)=128;
A17(:,:,2)=214;
A17(:,:,3)=128;

A18(:,:,1)=128;
A18(:,:,2)=214;
A18(:,:,3)=214;

A19(:,:,1)=214;
A19(:,:,2)=42;
A19(:,:,3)=42;

A20(:,:,1)=214;
A20(:,:,2)=42;
A20(:,:,3)=128;

A21(:,:,1)=214;
A21(:,:,2)=42;
A21(:,:,3)=214;

A22(:,:,1)=214;
A22(:,:,2)=128;
A22(:,:,3)=42;

A23(:,:,1)=214;
A23(:,:,2)=128;
A23(:,:,3)=128;

A24(:,:,1)=214;
A24(:,:,2)=128;
A24(:,:,3)=214;

A25(:,:,1)=214;
A25(:,:,2)=214;
A25(:,:,3)=42;

A26(:,:,1)=214;
A26(:,:,2)=214;
A26(:,:,3)=128;

A27(:,:,1)=214;
A27(:,:,2)=214;
A27(:,:,3)=214;


#creat a oneD array of colour chips

X=[A01,A02,A03,A04,A05,A06,A07,A08,A09,A10,A11,A12,A13,A14,A15,A16,A17,A18,A19,A20,A21,A22,A23,A24,A25,A26,A27];

#replace big "pixels" with small colour pictures

for i = 1:15:578
for j = 1:15:578
for m = 1:27

if PM4(i,j,:)==X(1,m,:)
PM4(i:i+15-1,j:j+15-1,:)=Y(1:15,(m-1)*15+1:m*15,:);
end;
end;
end;
end;

imshow(double(PM4)/255)

No comments: