Monday, June 9, 2008

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'.

No comments: