Friday, July 4, 2008

Photomosaic 1

After considering many images I decided to go with this image.



pkg load image
cd C:\
K=imread("photomosaic9.jpg");
K=double(K)/255;
imshow(K)
size(K)
124 124 3


















#Step 1
function Z=zoom(large,f)
cols=floor(size(large,2)*f);
rows=floor(size(large,1)*f);
for i1=1:rows;
for j1=1:cols;
a=round(i1/f);
b=round(j1/f);
Z(i1,j1,:)=large(a,b,:);
end;
end;
endfunction
K1=zoom(K,1/(3.1));
imshow(K1)
size(K1)
40 40 3




























#Step 2
B=floor(255*double(K1)/86)*86+42;
imshow(B/255)

































#Step 3
function Z=stretching(small,f)
ocols=size(small,2);
orows=size(small,1);
rows=floor(size(small,1)*f);
cols=floor(size(small,2)*f);
for i1=0:(rows-1);
for j1=0:(cols-1);
a=floor((i1/f)+1);
b=floor((j1/f)+1);
if (a greater 0)&(a less or equal orows)&(b greater 0)&(b less or equal ocols)
Z(i1+1,j1+1,:)=small(a,b,:);
end;
end;
end;
endfunction
C=stretching(B,16);
imshow(double(C)/255)
size(C)
640 640 3


























I took the following image and resized it using GIMP and made 27 images with different average colors.

#Step 4
e=imread("red2.jpg");
size(e)
75 75 3
















x111=(imread("x111.jpg"));
x112=(imread("x112.jpg"));
x113=(imread("x113.jpg"));
x121=(imread("x121.jpg"));
x122=(imread("x122.jpg"));
x123=(imread("x123.jpg"));
x131=(imread("x131.jpg"));
x132=(imread("x132.jpg"));
x133=(imread("x133.jpg"));
x211=(imread("x211.jpg"));
x212=(imread("x212.jpg"));
x213=(imread("x213.jpg"));
x221=(imread("x221.jpg"));
x222=(imread("x222.jpg"));
x223=(imread("x223.jpg"));
x231=(imread("x231.jpg"));
x232=(imread("x232.jpg"));
x233=(imread("x233.jpg"));
x311=(imread("x311.jpg"));
x312=(imread("x312.jpg"));
x313=(imread("x313.jpg"));
x321=(imread("x321.jpg"));
x322=(imread("x322.jpg"));
x323=(imread("x323.jpg"));
x331=(imread("x331.jpg"));
x332=(imread("x332.jpg"));
x333=(imread("x333.jpg"));

R=[x111,x112,x113,x121,x122,x123,x131,x132,x133,x211,x212,x213,x221,x222,x223,x231,x232,x233,x311,x312,x313,x321,x322,x323,x331,x332,x333];

S01(:,:,1)=42;
S01(:,:,2)=42;
S01(:,:,3)=42;
S02(:,:,1)=42;
S02(:,:,2)=42;
S02(:,:,3)=128;
S03(:,:,1)=42;
S03(:,:,2)=42;
S03(:,:,3)=214;
S04(:,:,1)=42;
S04(:,:,2)=128;
S04(:,:,3)=42;
S05(:,:,1)=42;
S05(:,:,2)=128;
S05(:,:,3)=128;
S06(:,:,1)=42;
S06(:,:,2)=128;
S06(:,:,3)=214;
S07(:,:,1)=42;
S07(:,:,2)=214;
S07(:,:,3)=42;
S08(:,:,1)=42;
S08(:,:,2)=214;
S08(:,:,3)=128;
S09(:,:,1)=42;
S09(:,:,2)=214;
S09(:,:,3)=214;
S10(:,:,1)=128;
S10(:,:,2)=42;
S10(:,:,3)=42;
S11(:,:,1)=128;
S11(:,:,2)=42;
S11(:,:,3)=128;
S12(:,:,1)=128;
S12(:,:,2)=42;
S12(:,:,3)=214;
S13(:,:,1)=128;
S13(:,:,2)=128;
S13(:,:,3)=42;
S14(:,:,1)=128;
S14(:,:,2)=128;
S14(:,:,3)=128;
S15(:,:,1)=128;
S15(:,:,2)=128;
S15(:,:,3)=214;
S16(:,:,1)=128;
S16(:,:,2)=214;
S16(:,:,3)=42;
S17(:,:,1)=128;
S17(:,:,2)=214;
S17(:,:,3)=128;
S18(:,:,1)=128;
S18(:,:,2)=214;
S18(:,:,3)=214;
S19(:,:,1)=214;
S19(:,:,2)=42;
S19(:,:,3)=42;
S20(:,:,1)=214;
S20(:,:,2)=42;
S20(:,:,3)=128;
S21(:,:,1)=214;
S21(:,:,2)=42;
S21(:,:,3)=214;
S22(:,:,1)=214;
S22(:,:,2)=128;
S22(:,:,3)=42;
S23(:,:,1)=214;
S23(:,:,2)=128;
S23(:,:,3)=128;
S24(:,:,1)=214;
S24(:,:,2)=128;
S24(:,:,3)=214;
S25(:,:,1)=214;
S25(:,:,2)=214;
S25(:,:,3)=42;
S26(:,:,1)=214;
S26(:,:,2)=214;
S26(:,:,3)=128;
S27(:,:,1)=214;
S27(:,:,2)=214;
S27(:,:,3)=214;

Q=[S01,S02,S03,S04,S05,S06,S07,S08,S09,S10,S11,S12,S13,S14,S15,S16, S17,S18,S19,S20,S21,S22,S23,S24,S25,S26,S27];


for p=1:27
for i1=1:16:640
for j1=1:16:640
if C(i1,j1,:)==Q(1,p,:)
C(i1:15+i1,j:15+j1,:)=R(1:16,16*p-15:16*p,:);
endif
end
end
end
imshow(double(C)/255)























I also added a partial image in order to try to get a better view.























































































































































































































































































.

Saturday, June 14, 2008

Assignment # 8

1. Modify assignment 7 so that you can scale in the x and y directions by different factors.

function small=scale(largeimage,fx,fy);
mp=floor(size(largeimage,1)*fx);
np=floor(size(largeimage,2)*fy);
small(:,:,1)=zeros(mp,np);
small(:,:,2)=zeros(mp,np);
small(:,:,3)=zeros(mp,np);
for i=0:(mp-1);
for j=0:(np-1);
for x=floor(i/fx):ceil((i+1)/fx)-1;
for y=floor(j/fy):ceil((j+1)/fy)-1;
ival=largeimage(x+1,y+1,:)
ival=double(ival);
if (x less i/fx)
ival=ival*(1-i/fx+x)
end;
if (x+1 greater (i+1)/fx)
ival=ival*(1-(i+1)/fx+x+1);
end;
if(y less i/fy)
ival=ival*(1-j/fy+y);
end;
if (y+1 greater (j+1)/fy)
ival=ival*(1-(j+1)/fy+y+1);
end;
small(i+1,j+1,:)=small(i+1,j+1,:)+ival;
end;
end;
small(i+1,j+1,:)=small(i+1,j+1,:)/((1/fx)*(1/fy));
end;
end;
endfunction
A=imread("CokeCan.jpg");
B=scale((double(A)/255),1,.5);
imshow(B)



a) fx=1, fy=.5

















b) fx=.5 fy=1
















c) fx=.2 fy=.8


















2. Enlarge color images that you scaled down in assignment #7 using a factor of 1/f. Use nearest neighbour approximation.

function largeimage=growing(small,f)
mp=size(small,1);
np=size(small,2);
newmp=floor(size(small,1)*f);
newnp=floor(size(small,2)*f);
for i=0:(newmp-1)
for j=0:(newnp-1)
a=round((i/f)+1);
b=round((j/f)+1);
if(a greater 0)&(a less mp)&(b greater 0)&(b less np)
largeimage(i+1,j+1,:)=small(a,b,:);
end
end
end
end
A=imread("Rainbow2.jpg");
B=growing((double(A)/255),(4/3));
imshow(B)




f = 4/3

















f=4/3















f = 4





















f=4





















f = 4
























3. Enlarge images from assignment #7 by 1/f. Use bilinear interpolation.

function largeimage=growing(small,f)
rows=size(small,1);
cols=size(small,2);
mp=floor(size(small,1)*f);
np=floor(size(small,2)*f);
for i=1:(mp-1);
for j=1:(np-1);
a=i/f+1;
b=j/f+1;
r=floor(a);
s=floor(b);
if(r greater 0)&(r less rows)&(s greater 0)&(s less cols)
for k = 1:3;
largeimage(i,j,k)=[1-a+r, a-r]*double([small(r,s,k),small(r,s+1,k);small(r+1,s,k),small(r+1,s+1,k)])*[1-b+s;b-s];
end
end
end
end
endfunction
A=imread("CHALLENGE.jpg");
B=growing((double(A)/255),(4));
imshow(B)

























































































4. Bilinear interpolation seems to be the best at preserving resoltution. I am basing this on the rainbow image only because that image seemed to be preserved quite well under the transformation of shrinking or enlarging.My other two images were dark to begin with so I could not tell with those two images.





5. Start with an original and make a new image.


































Assignment # 9

Question #1

A=imread("CokeCan.jpg");
B=floor((A)/64)*85;
C=255*B;
imshow(C)








































Question # 2

A=imread("CokeCan.jpg");
E=floor((A)/85)*128;
F=255*E;
imshow(F)














































3. Question #3

A=imread("CokeCan.jpg");
K=(A(:,:,1)+A(:,:,2)+A(:,:,3))/3;
imshow(K)






























































can't really see much too dark



































Question # 4
E=floor(double(K)/4)*4;
imshow(E)


















This image completely washed out for # 4 and #5.




















Question # 5

Z=floor(double(K)/16)*16;
imshow(Z)


Sunday, June 8, 2008

Assignment 7

Programs to create images below are at the bottom of the blog.






Image #1 f=0.75






















Image #2 f=0.75



























Image #1 f=0.25





























Image #2 f=0.25






















Image#1 f=0.10
























Image #2 f=0.10


























Method #1

pkg load image
cd C:\
largeimage=double(imread("CokeCan.jpg"));
f=0.10;
Mp=floor(size(largeimage,1)*f);
Np=floor(size(largeimage,2)*f);
small(:,:,1)=zeros(Mp,Np);
small(:,:,2)=zeros(Mp,Np);
small(:,:,3)=zeros(Mp,Np);
for i=0:(Mp-1);
for j=0:(Np-1);
for x=(floor(i/f)):ceil((i+1)/f)-1;
for y=(floor(j/f)):ceil((j+1)/f)-1;
ival=largeimage(x+1,y+1,:);
if (x<(i/f)); ival=ival*(1+x-(i/f)); end if ((x+1)>(i+1)/f);
ival=ival*(1-(x+1)+((i+1)/f));
end
if (y<(j/f)); ival=ival*(1+y-(j/f)); end if ((y+1)>(j+1)/f);
ival=ival*(1-(y+1)+((j+1)/f));
end
small(i+1,j+1,:)=small(i+1,j+1,:)+ival;
end
end
end
end
small=small*f*f;
imshow(double(small)/255);




Method #2

pkg load image
cd C:\
largeimage=imread("CokeCan.jpg");
largeimage=double(largeimage);
f=0.75;
mp=floor(size(largeimage,1)*f-1);
np=floor(size(largeimage,2)*f-1);
small(:,:,1)=zeros(mp-1,np-1);
small(:,:,2)=zeros(mp-1,np-1);
small(:,:,3)=zeros(mp-1,np-1);
for i=1:(mp-1)
for j=1:(np-1)
a=round(i/f);
b=round(j/f);
small(i,j,:)=largeimage(a,b,:);
end;
end;
imshow(double(small/255))





Method #3

pkg load image
cd C:\
largeimage=double(imread("CokeCan.jpg"));
f=0.75;
mp=floor(size(largeimage,1)*f);
np=floor(size(largeimage,2)*f);
small(:,:,1)=zeros(mp,np);
small(:,:,2)=zeros(mp,np);
small(:,:,3)=zeros(mp,np);
for i=1:mp
for j=1:np
a=i/f;
b=j/f;
r=floor(a);
s=floor(b);
if (r>0) &(r(lessthan size(largeimage,1)&(s>0)&(s(lessthan size(largeimage,2) #blog will not post properly with mathematical symbols in above line.
for k=1:3
small(i,j,k)=[1-a+r,a-r]*[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;
end;
imshow(small/255)

Sunday, June 1, 2008

Assignment #6

1. Skew and rotate an image using bilinear interpolation to determine an intensity value for each point.

Using the ideas discussed in class:

changedT=255*ones(256);
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
u=x*cos(5*pi/3)-y*sin(5*pi/3)
v=x*sin(5*pi/3)+y*cos(5*pi/3)
a=u
b=mod((v-1*u),256)+1
r=floor(a)
s=floor(b)
if((r>0)&(r<256)&(s>0)&(s<256))
changedT(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];
endif
endfor
endfor
imshow(changedT)














2. Create a 256x256 matrix with 1s in the (i,i+1) position and zeros


triu(ones(256),1)-triu(ones(256),2)

For the purposes of demonstration I will present a 6x6 matrix.

0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0



3. Given a color, compute the average color. Compute the average color for the image with

a) R=ones(100);
G=tril(ones(100));
B=zeros(100);

A(:,:,1)=(ones(100));
A(:,:,2)=tril(ones(100));
A(:,:,3)=zeros(100);
imshow(A)
sum(sum(A))/100/100
ans =

ans(:,:,1) = 1
ans(:,:,2) = 0.50500
ans(:,:,3) = 0







































b)
R(:,:,1)=ones(256);
R(:,:,2)=ones(256,1)*[0:1:255]/255;
R(:,:,3)=zeros(256);
Y(:,:,1)=ones(256,1)*[255:-1:0]/255;
Y(:,:,2)=ones(256);
Y(:,:,3)=zeros(256);
G(:,:,1)=zeros(256);
G(:,:,2)=ones(256);
G(:,:,3)=ones(256,1)*[0:1:255]/255;
C(:,:,1)=zeros(256);
C(:,:,2)=ones(256,1)*[255:-1:0]/255;
C(:,:,3)=ones(256);
B(:,:,1)=ones(256,1)*[0:1:255]/255;
B(:,:,2)=zeros(256);
B(:,:,3)=ones(256);
M(:,:,1)=ones(256);
M(:,:,2)=zeros(256);
M(:,:,3)=ones(256,1)*[255:-1:0]/255;
K=[R,Y,G,C,B,M];
imshow(K)
size(K)

256 1536 3

sum(sum(K))/(256*1536)
ans =

ans(:,:,1) = 0.50000
ans(:,:,2) = 0.50000
ans(:,:,3) = 0.50000













































c)
pkg load image
cd C:\
A=imread("CokeCan.jpg")
size(A)
ans =

108 160 3
sum(sum(A))/(108*160)
ans =

ans(:,:,1) = 80.181
ans(:,:,2) = 148.30

ans(:,:,3) = 163.15





















pkg load image
cd C:\
B=imread("CHALLENGE.jpg")
size(B)
ans =

160 117 3
sum(sum(B))/(160*117)
ans =

ans(:,:,1) = 103.46
ans(:,:,2) = 106.67
ans(:,:,3) = 83.541















Tuesday, May 27, 2008

Assignment 5

Assignment 5

1. Find octave commands for constructing a 256x256 matrix with entries of 0 everywhere except inside the circle with radius 50 where the values are 1.

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;
endfor;
Imshow(A*256)









2. Give octave commands to draw the top part of figure 6.4 in the book.

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. Consider the black and white image from the matrix. Skew the image by a factor of s pixels. Rotate the image theta degrees

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

BIG T










bigT=255*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150,36);
function retval=newy(x,y,s)
retval=y+mod(x*s,256)+1;
endfunction
for x=1:256;
for y=1:256;
y1=int32(newy(x,y,1));
if(y1>256)
y1=y1-256;
endif;
bigT1(x,y1)=bigT(x,y);
endfor;
endfor;
imshow(bigT1)



Skew by a factor of 0.9 and 1














Rotation of 5pi/3

bigT=255*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150,36);
bigT2=ones(256);
for x=1:256;
for y=1:256;
xnew=int32(mod(x*cos(5*pi/3)-y*sin(5*pi/3),256)+1);
ynew=int32(mod(x*cos(5*pi/3)+y*sin(5*pi/3),256)+1);
bigT2(xnew,ynew)=bigT(x,y);
endfor
endfor; imshow(bigT2)

Sunday, May 25, 2008

Assignment 4

1. Make a matrix of 256 x 1 ones.
Answer ones(256)

2. Make a matrix of the integers 0 to 255 in a single row.
Answer [0:1:255]

3. Make a column of the integers o to 255.
Answer [0:1:255]’

4. Make a matrix 256 x 256 with the rows all the same equal to the numbers 0 to 255.
Answer ones(256)*[0:1:255]

5. Make a matrix 256 x 256 with the columns all the same equal to the numbers 0 to 255.
Answer [0:1:255]’*ones(1,6)

6. Make a matrix 256 x 256 of all zeros.
Answer zeros(256)

7. Make a matrix 256 x 256 of all ones


Answer ones(256)

8. Make a matrix 256 x 256 of the value 128.


Answer 128*ones(1,256)
9. same as # 6

10. Display the grey scale image .


Answer imshow(ones(256,1)*[0:1:255]/255)’










Display the different faces of the color cube.
11. Red - Blue
RB face of the colour cube.
RB(:,:,1)=(ones(256)*((255:-1:0)/255)’;
RB(:,:,2)=zeros(256);
RB(:,:,3)=(ones(256,1)*(0:1:255)/255);
imshow(RB)














12. Red – Green
RG face of the colour cube.
RG (:,:,1)= (ones(256,1)*(0:1:255)/255)’;
RG (:,:,2)= (ones(256,1)*((255:-1:0)/255);
RG (:,:,3)= zeros(256);
imshow(RG)

















13. Blue - Green
BG face of the colour cube.
BG (:,:,1)= zeros(256);
BG (:,:,2)= (ones(256,1)*(0:1:255)/255);
BG (:,:,3)= (ones(256,1)*((255:-1:0)/255)’;
imshow(RB)


















14. Cyan – Yellow
CY face of the colour cube.
CY(:,:,1)=[ones(256,1)*[255:-1:0]/255]';
CY(:,:,2)=ones(256);
CY(:,:,3)=[ones(256,1)*[0:1:255]/255]’;
imshow(CY)



















15. Cyan - Magenta
BW face of the colour cube.
BW (:,:,1)= zeros(256);
BW (:,:,2)= (ones(256,1)*(0:1:255)/255);
BW (:,:,3)= (ones(256,1)*((255:-1:0)/255)’;
imshow(BW)













16. Magenta – Yellow
MY(:,:,1)=ones(256);
MY(:,:,2)=(ones(256,1)*(0:1:255)/255);
MY(:,:,3)=(ones(256,1)*(255:-1:0)/255)’;
imshow(MY)


























17. Display the whole color cube in a cross with white regions where there doesen’t need to be anything.

Not exactly sure how to do this just yet.

18. Display the rainbow image that appears on the Math 5330 facebook page.

Not exactly sure how to do this just yet.


19. Shift entries of a matrix A one place left. (Wrapping)

Suppose A = [1,2,3;4,5,6;7,8,9]. To shift A left we perform the following operation.

[1,2,3;4,5,6;7,8,9]*[0,0,1;1,0,0;0,1,0] = [2,3,1;5,6,4;8,9,7]

20. Shift the entries of a matrix A one pixel down.

Suppose A = [1,2,3;4,5,6;7,8,9]. To shift A one pixel down we perform the following operation.

[0,0,0;1,0,0;0,1,0]* [1,2,3;4,5,6;7,8,9] = [0,0,0;1,2,3;4,5,6]

21. Shift entries of a matrix A one place left (drop values off left edge)

Suppose A = [1,2,3;4,5,6;7,8,9]. To shift A one place left (drop values off left edge) we perform the following operation.

[1,2,3;4,5,6;7,8,9]*[0,0,0;1,0,0;0,1,0] = [2,3,0;5,6,0;8,9,0]

22. Suppose A = [1,2,3;4,5,6;7,8,9]. To shift A one place left (drop values off bottom edge) perform the following operation.

[0,0,0;1,0,0;0,1,0]*[1,2,3;4,5,6;7,8,9] = [0,0,0;1,2,3;4,5,6]