Friday, May 23, 2008

Octave - Part 2

{deleted pictures for #1/2 to edit blog for #3 more easily}

(1) To construct a 256 x 256 matrix with entries of 0 everywhere except inside of a circle with radius 50 where the values are 1...a white circle with r=50 inside a black square of "length"=256.
S=zeros(256);
for x = 1:256;
for y = 1:256;
if ((x-128)^2 + (y-128)^2<=2500);
S(x,y)=1;
endif;
endfor;
endfor;
I=255*S;
imshow(I).
Thanks Alice, I had everything but the second last line. Thought of something: You must multiply S by 255 because imshow displays a color image and a 255 entry corresponds to White; an entry of 1 corresponds to White when it is a grey scale image. Or.. it is because before we had to /255 with commands, and now we have to *255..something to do with dimensions?

(2) Top of Fig. 6.4:

To determine centre(p,q):=(rows,columns), assuming the three circles intersect at centre(128, 128):
  • R: p = 128+12.5=140.5; q = 90.5+25=115.5
  • G: p = 128-12.5=115.5; q=128
  • B: p = 140.5; q = 128 + 12.5=140.5

C1=zeros(256);

for x = 1:256;

for y = 1:256;

if ((x-140.5)^2 + (y-115.5)^2<=2500);

C1(x,y)=1;

endif;

endfor;

endfor;

C2=zeros(256);

for x = 1:256;

for y = 1:256;

if ((x-115.5)^2 + (y-128)^2<=2500);

C2(x,y)=1;

endif;

endfor;

endfor;

C3=zeros(256);

for x = 1:256;

for y = 1:256;

if ((x-140.5)^2 + (y-140.5)^2<=2500);

C3(x,y)=1;

endif;

endfor;

endfor;

I(:,:,1)=255*C1;

I(:,:,2)=255*C2;

I(:,:,3)=255*C3;

imshow(I).

At first, I had C1(x,y)=G; C2(x,y)=R, and C3(x,y)=B...where RGB are defined in Assignment #4:

R(:,:,1)=ones(256);R(:,:,2)=[ones(256,1)*[0:1:255]/255];R(:,:,3)=zeros(256).

G(:,:,1)=zeros(256);G(:,:,2)=ones(256);G(:,:,3)=[ones(256,1)*[0:1:255]/255].

B(:,:,1)=[ones(256,1)*[0:1:255]/255];B(:,:,2)=zeros(256);B(:,:,3)=ones(256).

This doesn't work due to a dimension error, but I am still trying to comprehend this in 3-space and Octave 2-space.

(3) First display bigT:

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

a) Skew the image by a factor of 1 pixel horizontally:

for x=1:256;
for y=1:256;
u=x;
v=mod((1*x)+y,256)+1;
newbigT1(x,y)=bigT(u,v);
endfor;
endfor;
imshow(newbigT1).

b) Rotate the image by 5pi/4 degrees [(x,y) -> (x*cos(theta) - y*sin(theta), x*sin(theta) + y*cos(theta)]:

bigT2=ones(256);
for x=1:256;
for y=1:256;
newx=round(mod(x*cos(5*pi/4) - y*sin(5*pi/4), 256) +1);
newy=round(mod(x*sin(5*pi/4) + y*cos(5*pi/4), 256) +1);
bigT2(newx,newy) = bigT(x,y);
endfor;
endfor;
imshow(bigT2).


c) Skew the image by a factor of 1 pixel horizontally and then rotate by 5pi/4 degrees:
- This was done using all of the commands in a & b, with a change in the line that assigns bigT2: bigT2(newx, newy)=newbigT1(x,y)

1 comment:

alice said...

You must be mistaking me for someone who has a clue about what is going on ... I just did it because Mike said so in the lab Wed. night. I'm sure he told us the reason, but I can't remember it.
By the way, the Octave on my computer didn't like the elseif's. I can get all 3 circles separately and in the right places, but still can't figure out how to combine them ihto 1 image.

Alice