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