Wednesday, June 18, 2008

Assignment #10

The given image has a radius of 180 pixels and was rotated 540 degrees clockwise. To see who this celebrity is, we must use the following code:

A=imread("star1.jpg");
function [outpic]=swirl(A,r,gamma);
A=double(A);
Cx=size(A,1)/2;
Cy=size(A,2)/2;
for x=1:size(A,1);
for y=1:size(A,2);
d=sqrt((x-Cx)^2+(y-Cy)^2);
if (d>r)||(d<1);
outpic(x,y,:)=A(x,y,:);
else;
if (y>Cy);
theta=acos((x-Cx)/d);
else;
theta=-acos((x-Cx)/d);
end;
fr=(d/r)^2*(1-(d/r))^2*16;
newx=d*cos(theta+gamma*fr)+Cx;
newy=d*sin(theta+gamma*fr)+Cy;
a=floor(newx);
b=floor(newy);
for k=1:3;
outpic(x,y,k)=[1-newx+a, newx-a]*[A(a,b,k), A(a,b+1,k);A(a+1,b,k), A(a+1,b+1,k)]*[1-newy+b; newy-b];
end;
end;
end;
end;
endfunction;

B=swirl(A,180,-3*pi);
C=double(B)/255;
imwrite("starA.jpg",C(:,:,1),C(:,:,2),C(:,:,3));


this command also shows image:
image(swirl(A,180,-3*pi))





For the following black and white (2-D) picture, the radius is 150 pixels and it too was rotated 540 degrees but counterclockwise so the following code (similar to the one above) must rotate the pic clockwise 540 degrees. Here is the code:

A=imread("star2.jpg");
function [outpic]=swirl(A,r,gamma);
A=double(A);
Cx=size(A,1)/2;
Cy=size(A,2)/2;
for x=1:size(A,1);
for y=1:size(A,2);
d=sqrt((x-Cx)^2+(y-Cy)^2);
if (d>r)||(d<1);
outpic(x,y)=A(x,y);
else;
if (y>Cy);
theta=acos((x-Cx)/d);
else;
theta=-acos((x-Cx)/d);
end;
fr=(d/r)^2*(1-(d/r))^2*16;
newx=d*cos(theta+gamma*fr)+Cx;
newy=d*sin(theta+gamma*fr)+Cy;
a=floor(newx);
b=floor(newy);
for k=1:3;
outpic(x,y)=[1-newx+a, newx-a]*[A(a,b), A(a,b+1);A(a+1,b), A(a+1,b+1)]*[1-newy+b; newy-b];
end;
end;
end;
end;
endfunction;
B=swirl(A,150,3*pi); #the pic was rotated counterclockwise 540 degrees so gamma=+3*pi
image(B)

No comments: