Friday, May 23, 2008

Assignment 5

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

Solution: (done on Octave 3.0.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;
B = 255*A
imshow(B)






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

Solution: (done on Octave 3.0.1)

circle1=zeros(256);
for x=1:256; for y=1:256;
if (x-99.1324)^2 + (y-128)^2 <= 2500;
circle1(x,y)=1;
endif;
endfor;
endfor;
circle2=zeros(256);
for x=1:256; for y=1:256;
if (x-142.4337567)^2 + (y-103)^2 <= 2500;
circle2(x,y)=1;
endif;
endfor;
endfor;
circle3=zeros(256);
for x=1:256; for y=1:256;
if (x-142.4337567)^2 + (y-153)^2 <= 2500;
circle3(x,y)=1;
endif;
endfor;
endfor;
A=255*circle1;
B=255*circle2;
C=255*circle3;
D(:,:,1)=B;
D(:,:,2)=A;
D(:,:,3)=C;
imshow(D)


3. a) Consider the black and white image from the matrix


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



b) Skew the image by a factor of 2 pixels horizontally.

Note: The next part of this solution comes from a collaborative effort with Violeta, Malgorzata & Mike. In fact, I'm not sure which part of this (if any) I can really take credit for... oh yes, I drew borders around the images so that you could tell where the image ends and the blog begins.


Solution: (done on Matlab 2007)

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



c) Rotate the image (5*pi/4) degrees.

Solution: (done on Matlab 2007)

for x=1:256; for y=1:256;
u=x*cos(5*pi/4)-y*sin(5*pi/4);
v=x*sin(5*pi/4)+y*cos(5*pi/4);
u1=round(u);
v1=round(v);
bigT2(mod(u1,256)+1,mod(v1,256)+1)=bigT1(x,y);
end; end;
imshow(bigT2)


And here's my own attempt to put both transformations together in a shorter sequence of commands. Here are the matrices that would be multiplied to give the overall transformation:




These are not the ones listed in our text, but are the CORRECT ones.





At each step, we will have to take the values (mod 256)+1 so that the image will wrap around.
Next, I will create a sequence of commands that will take each point (x, y) and map it to (x', y').


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;
ymod=mod(2*x+y,256)+1;
xp=x*cos(5*pi/4)-ymod*sin(5*pi/4);
yp=x*sin(5*pi/4)+ymod*cos(5*pi/4);
xpr=round(xp);
ypr=round(yp);
newbigT(mod(xpr,256)+1,mod(ypr,256)+1)=bigT(x,y);
end;
end;
imshow(newbigT)

No comments: