Thursday, July 3, 2008

The "large pixels"

Here are the original images which were resized to be the 15 x 15 pixel images in the photmosaic. I tried to use as many "prairie" images as I could in keeping with the theme of the big red elevator.

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)

Saturday, June 7, 2008

Assignment #7






function smallimage=shrink2(largeimage,f);

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

smallimage(:,:,1)=zeros(Mp-1,Np-1);
smallimage(:,:,2)=zeros(Mp-1,Np-1);
smallimage(:,:,3)=zeros(Mp-1,Np-1);


for i=1:(Mp-1)
for j=1:(Np-1)

a=round(i/f);
b=round(j/f);
smallimage(i,j,:)=largeimage(a,b,:);
end;
end;
endfunction;



A=imread("image.jpg");

B=shrink2((double(A)/255),f);
function smallimage=shrink3(largeimage,f)


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

smallimage(:,:,1)=zeros(Mp-1,Np-1);
smallimage(:,:,2)=zeros(Mp-1,Np-1);
smallimage(:,:,3)=zeros(Mp-1,Np-1);


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

r=floor(a);
s=floor(b);

for k = 1:3;

smallimage(i,j,k)=[1-a+r, a-r]*double([largeimage(r,s,k),largeimage(r,s+1,k);largeimage(r+1,s,k),largeimage(r+1,s+1,k)])*[1-b+s;b-s];

end;
end;
end;
endfunction;



A=imread("image.jpg");

B=shrink3((double(A)/255),0.75);


Monday, June 2, 2008

Assignment #6-question 3

Assignment 6 - 3 b)


pkg load image

cd 'C:\pictures'
A=imread("Rainbow.jpg");
B=double(A)/255;
size(B)

ans =
109 200 3

aveB=sum(sum(B))/109/200

ans(:,:,1)=0.56770
ans(:,:,2)=0.34527
ans(:,:,3)=0.30058


aveB(:,:,1)=0.56770
aveB(:,:,2)=0.34527
aveB(:,:,3)=0.30058

imshow(aveB)


6 -3 b)

Find the average colour of two images from the web.

C=imread("green.jpg");
D=double(C)/255;
imshow(D)
size(D)

ans =
110 160 3

aveD=sum(sum(D))/110/160

ans(:,:,1)=0.46077
ans(:,:,2)=0.66349
ans(:,:,3)=0.069944


aveD(:,:,1)=0.46077
aveD(:,:,2)=0.66349
aveD(:,:,3)=0.069944

imshow(aveD)


E=imread("blue.jpg");
F=double(E)/255
imshow(F)
size(F)
ans =

106 160 3

aveF=sum(sum(F))/106/160

ans(:,:,1)=0.24725
ans(:,:,2)=0.35670
ans(:,:,3)=0.76474


aveF(:,:,1)=0.24725
aveF(:,:,2)=0.35670
aveF(:,:,3)=0.76474
imshow(aveF)

When the original image is predominately one colour, the average colour is similar. When the original image has many colours, the average colour is less predictable.

Saturday, May 31, 2008

Assignment #6

1. Use bilinear interpolation on the skewed and rotated image from Assignment 5.

bigT=255.0*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150,36);
newmatrix=255*ones(256);
for x=1:256; for y=1:256;
u=x*cos(5*pi/6)+y*sin(5*pi/6);
v=-x*sin(5*pi/6)+y*cos(5*pi/6);
a=u;
b=mod((v-2*u),256)+1;
r=floor(a);
s=floor(b);
if ((r>0) & (r<256)>0) & (s<256));
newmatrix(x,y)=[1-a+r, a-r]*[bigT(r,s),bigT(r,s+1); bigT(r+1,s),bigT(r+1,s+1)]*[1-b+s;b-s];
end; end; end; imshow(newmatrix)


2. Create a 256 x 256 matrix with 1's in the i,i+1 positions.

A=tril(ones(256),1);
B=tril(ones(256),0);
A-B


3. Determine the average colour:

a) F(:,:,1)=ones(100);
F(:,:,2)=tril(ones(100));
F(:,:,3)=zeros(100);

size(F)
ans=
100 100 3

aveF=sum(sum(M))/100/100
aveF=
ans(:,:,1)=1
ans(:,:,2)=0.50500
ans(:,:,3)=0

A(:,:,1)=1;
A(:,:,2)=0.505;
A(:,:,3)=0;



b) Using the rainbow picture from Assignment 4:

A=zeros(256);
B=ones(256);
C=[ones(256,1)*[255:-1:0]/255];
D=[ones(256,1)*[0:1:255]/255];

R(:,:,1)=B;
R(:,:,2)=D;
R(:,:,3)=A;

Y(:,:,1)=C;
Y(:,:,2)=B;
Y(:,:,3)=A;

G(:,:,1)=A;
G(:,:,2)=B;
G(:,:,3)=D;

Cy(:,:,1)=A;
Cy(:,:,2)=C;
Cy(:,:,3)=B;

Bl(:,:,1)=D;
Bl(:,:,2)=A;
Bl(:,:,3)=B;

Ma(:,:,1)=B;
Ma(:,:,2)=A;
Ma(:,:,3)=C;

RB=[R,Y,G,Cy,Bl,Ma];

size(RB)
ans:
256 1536 3
aveRB=sum(sum(RB))/256/1536
aveRB=
ans(:,:,1)=0.50000
ans(:,:,2)=0.50000
ans(:,:,3)=0.50000




b) I am still trying to get images into Octave...

Saturday, May 24, 2008

Assignment #5



1. Here are the commands that create a white circle in a black square:

A=zeros(256);
for x=1:256;
for y=1:256;
if (x-128)^2 + (y-128)^2 <=2500;
A(x,y)=1;
endif;

endfor;
imshow(A*255)





2. To get the three circles, in colour:


A=zeros(256);
B=zeros(256);

C=zeros(256);
for x=1:256;

for y=1:256;

if (x-93)^2 + (y-128)^2<=2500;
A(x,y)=1;

endif;

if (x-153)^2 + (y-93)^2<=2500;
B(x,y)=1;

endif;
if (x-153)^2 + (y-153)^2 <=2500;
C(x,y)=1;
endif;

endfor;

endfor;
E(:,:,1)=A*255;

E(:,:,2)=B*255;

E(:,:,3)=C*255;
imshow(E)


3. bigT=255.0*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150,36);
imshow(bigT)


bigT1=255*ones(256);
function retval=newy(x,y,s)
retval=y+mod(x*s,256)+1;
endfunction

for x=1:256;
f or y=1:256;
y1=int32(newy(x,y,2));

if(y1>256)
y1=y1-256;
end;

bigT1(x,y1)=bigT(x,y);
endfor;
endfor;




bigT2=ones(256);
for x=1:256;
for y=1:256;

newx=int32(mod(x*cos(5*pi/6)-y*sin(5*pi/6),256)+1);
newy=int32(mod(x*sin(5*pi/6)+y*cos(5*pi/6),256)+1);

bigT2(newx,newy)=bigT1(x,y);

end;
end;










There's still something not quite right, but it's late and I keep making the same mistakes...like Arnold, I'll be back.


Another look at #3. I have deleted 3.0.0 and am using 3.0.1 again. After trying out several versions of the code posted by classmates, I have a somewhat different set of images. Here goes:

The original BigT:

bigT=255*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150,36);

The resized (*2) bigT1

for x=1:256;
for y=1:256;
y1=mod(2*x+y,256)+1;
bigT1(x,y)=bigT(x,y1);
end;
end;




The rotated bigT:

f or x=1:256;
for y=1:256;
x2=x*cos(5*pi/6)-y*sin(5*pi/6);
y2=x*sin(5*pi/6)+y*cos(5*pi/6);
x3=round(x2);
y3=round(y2);
bigT2(mod(x3,256)+1,mod(y3,256)+1)=bigT(x,y);
end;
end;


The resized and rotated bigT:

bigT=255*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150,36);
for x=1:256;
for y=1:256;
y1=mod(2*x+y,256)+1;
bigT1(x,y)=bigT(x,y1);
end;
end;

for x=1:256;
for y=1:256;
x2=x*cos(5*pi/6)-y*sin(5*pi/6);
y2=x*sin(5*pi/6)+y*cos(5*pi/6);
x3=round(x2);
y3=round(y2);
bigT2(mod(x3,256)+1,mod(y3,256)+1)=bigT1(x,y);
end;
end;