Friday, June 13, 2008

Wednesday, June 11, 2008

Swirling Mike


Assignment 10

The colour picture:

function [ outpic] = swirl( inpic, r, gamma )
inpic=double(inpic);
cx = size(inpic,1)/2;
cy = size(inpic, 2)/2;
outpic(:,:,1)=zeros(size(inpic,1),size(inpic,2));
outpic(:,:,2)=zeros(size(inpic,1),size(inpic,2));
outpic(:,:,3)=zeros(size(inpic,1),size(inpic,2));
for x = 1:size(inpic,1);
for y=1:size(inpic,2);
d=sqrt((x-cx)^2+(y-cy)^2);
if (d greater than r) or (d less than 1);
outpic(x,y,:)=inpic(x,y,:);
else if (y less than 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;
t=floor(newx);
u=floor(newy);
for k=1:3;
outpic(x,y,k)=[1-newx+t newx-t]*[inpic(t,u,k) inpic(t,u+1,k);inpic(t+1,u,k) inpic(t+1,u+1,k)]*[1-newy+u; newy-u];
end;
end;
end;
end;













The black & white picture:

function [ outpic] = swirlbw( inpic, r, gamma )
inpic=double(inpic);
cx = size(inpic,1)/2;
cy = size(inpic, 2)/2;
outpic=zeros(size(inpic,1),size(inpic,2));
for x = 1:size(inpic,1);
for y=1:size(inpic,2);
d=sqrt((x-cx)^2+(y-cy)^2);
if (d greater than r) or (d less than 1);
outpic(x,y)=inpic(x,y);
else if (y less than 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;
t=floor(newx);
u=floor(newy);
outpic(x,y)=[1-newx+t newx-t]*[inpic(t,u) inpic(t,u+1);inpic(t+1,u) inpic(t+1,u+1)]*[1-newy+u; newy-u];
end;
end;
end;


Monday, June 9, 2008

Assignment 9

















































































Assignment 8 - Q3 & 4

I'm putting these two questions as a separate blog posting since I'm tired of fighting with blogspot and the images.

3. Enlarging using bilinear interpolation approximation.

Here is the Matlab M-file:
function [ largeimage ] = enlargebi( picture, f )
picture=double(picture);
Mp=floor(size(picture,1));
Np=floor(size(picture,2));
largeimage(:,:,1)=zeros(f*Mp,f*Np);
largeimage(:,:,2)=zeros(f*Mp,f*Np);
largeimage(:,:,3)=zeros(f*Mp,f*Np);
for i=0:(f*Mp);
for j=0:(f*Np);
a=i/f;
b=j/f;
r=floor(a)+2; %to get rid of black border (thanks Thomas)
s=floor(b)+2;
if s>0 && s<(size(picture,2)) && r>0 && r<(size(picture,1));
for k=1:3;
largeimage(i+1,j+1,k)=[1-a+(r-2) a-(r-2)]*[picture(r-1,s-1,k) picture(r-1,s,k); picture(r,s-1,k) picture(r,s,k)]*[1-b+(s-2); b-(s-2)];
end;
end;
end;
end;
image((largeimage)/255);
end


Sample commands:

A=imread('rainbow.jpg');
B=shrinkbi(A, 0.75);
C=enlargebi(A, 4/3);

I don't think that these look right. On other people's blogs the enlarged images come out much clearer than these. But, my commands are the same as theirs, so I don't know why this is happening. The images (shrunk first, then enlarged):

f = 0.75, f = 4/3






f = 0.25, f = 4







f = 0.1, f = 10







f=0.75, f = 4/3









f = 0.25, f = 4










f = 0.1, f = 10









f = 0.75, f = 4/3








f = 0.25, f = 4









f = 0.1, f = 10






4. Lots of people seem to be saying that the bilinear interpolation does a better job at preserving the resolution when an image is first reduced and then enlarged. As an example, here is the rainbow reduced (f = 0.25) and then enlarged (f = 4) using bilinear interpolation.
















Supposedly this appears better than the nearest neighbour method (which might be 'grainier'?)shown below:










As I understand it, Photoshop uses a bicubic interpolation as the default when it scales images. I presume that GIMP does likewise. In Photoshop, with the commands Image -> Resize -> Resample Image it is clear that 'bicubic' is selected unless the user changes the option to either 'bilinear' or 'nearest neighbor'.