Tuesday, June 10, 2008

Assignment 9 question 4

Take the images from Assignment 6 number
#3 part (c) and turn them into a greyscale image with only 64 intensity values.
Use values between 0 and 252 but that are divisible by 4.

>> B = togray(jade);
>> C = floor(B/4)*4;
>> imshow(C/255)


>> B = togray(bluelines);
>> C = floor(B/4)*4;
>> imshow(C/255)

Assignment 9 question 5


Take the images from Assignment 6 number #3 part (c)
and turn them into a greyscale image with only 16 intensity values. Use values
between 0 and 240 but that are divisible by 16.

>> B = togray(bluelines);
>> C = floor(B/16)*16;
>> imshow(C/255)

>> B = togray(jade);
>> C = floor(B/16)*16;
>> imshow(C/255)

Assignment 8 question 1

Modify Assignment 7 problem #1 so that you can scale in the x and y directions
by different factors. Scale one of the two pictures that you downloaded of the web

function [ smim ] = shrinkxy( picture,fx, fy )
Mp = floor(size(picture,1)*fy);
Np = floor(size(picture,2)*fx);

smim(:,:,1)=zeros(Mp,Np);
smim(:,:,2)=zeros(Mp,Np);
smim(:,:,3)=zeros(Mp,Np);

for i = 0:(Mp-1)
for j = 0:(Np -1)
for x = floor(i/fy):ceil((i+1)/fy)-1
for y = floor(j/fx):ceil((j+1)/fx)-1
ival = picture(x+1,y+1,:);
if(x ival =ival*(1+x-i/fy);
end
if (x+1 > (i+1)/fy)
ival = ival*(1+((i+1)/fy)-(x+1));
end
if (y< j/fx)
ival = ival*(1-(j/fx)+y);
end
if (y+1 > (j+1)/fx)
ival =ival*(1-(y+1)+(j+1)/fx);
end

smim(i+1,j+1,:) = smim(i+1,j+1,:)+ival;

end;
end;
smim(i+1,j+1,:) = smim(i+1,j+1,:)/(1/fx)/(1/fy);
end
end

>> imshow(shrinkxy(bluelines,1,0.5))
>> imshow(shrinkxy(bluelines,0.5,1))
>> imshow(shrinkxy(bluelines,0.2,0.8))




Assignmet 8 Question 2

Enlarge the color images that you scaled down in assignment 7 using a factor of
1/f. Use the 'nearest neighbor' approximation.

function [largeimage] = enlargenn( picture, f )
picture=double(picture);
Mp = size(picture,1);
Np = size(picture,2);
largeimage(:,:,1)=zeros(f*Mp,f*Np);
largeimage(:,:,3)=zeros(f*Mp,f*Np);
largeimage(:,:,2)=zeros(f*Mp,f*Np);
for i = 1:(f*Mp);
for j = 1:(f*Np );
if (i/f < 1 && j/f < 1)
largeimage(i,j,:)=picture(1,1);
end
if ((i/f < 1) && (j/f >= 1))
largeimage(i,j,:)=picture(1,round(j/f),:);
end
if ((i/f >= 1) && (j/f < 1))
largeimage(i,j,:)=picture(round(i/f),1,:);
end
if ((i/f>=1) && (j/f>=1))
largeimage(i,j,:)=picture(round((i)/f),round((j)/f),:);
end
end;
end;

>> B = shrink3(bluelines,0.1);
>> imshow(enlargenn(B,10))
>> B = shrink3(bluelines,0.25);
>> imshow(enlargenn(B,4))
>> B = shrink3(bluelines,0.75);
>> imshow(enlargenn(B,4/3))






Assignment 8 Question 3

Enlarge the color images that you scaled down in assignment 7 using a factor of
1/f. Use the bilinear interpolation approximation.

function [ largeimage ] = enlargebi( picture, f )
picture=double(picture);
Mp = size(picture,1);
Np = size(picture,2);

for i = 1:(f*Mp);
for j = 1:(f*Np);
a=i/f;
b=j/f;
r=floor(a)+2;
s=floor(b)+2;
if s>0 && s<=size(picture,2) && r>0 && r<=size(picture,1)
for k=1:3;
largeimage(i,j,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;

>> B = shrink(bluelines,0.1);
>> imshow(enlargebi(B,10))
>> B = shrink(bluelines,0.25);
>> imshow(enlargebi(B,4))
>> B = shrink(bluelines,0.75);
>> imshow(enlargebi(B,4/3))






Assignment 8 Question 4


Question 4.
Now that you have experimented a bit, which method (or combination of methods)
of shrinking and enlarging images is most effective at preserving the resolution?
Explain why you feel that way by pointing to examples.
Try to determine what GIMP/Photoshop does when it scales images.


Technically they all have the same resolution. I am not sure if we are talking about detail, contrast or overall likeness to the original image. I think if we are looking at detail and contrast then some combination of nearest neighbour and bilinear interpolation will be the best, because nearest neighbour does not reduce the contrast of the original pixels at all. It also seems to retain more detail. The bilinear interpolation is also good at retaining more contrast and detail. On the other hand they produce choppy images that get worse as the factor increases. The average color method produces images that are blurry and have a lower contrast but are much closer to the original in overall likeness.


The following four images compare different methods to shrink and expand by a factor of four. The first is the original, the second uses bilinear shrink and expand, the third uses nearest neighbour shrink and bilinear expand, the fourth uses average shrink and bilinear expand. The fourth is the closest to the original in overall likeness.








When using GIMP we have the option of nearest neighbour resizing , Linear interpolation,
Cubic interpolation or something called Sinc (Lanczos 3)

Assignment 8 question 5



function [ im ] = shrinkpaste( pic,k ,f)
im = pic;
temp = pic;
Np = size(pic,1);
Mp = size(pic,2);
for i = 1:k
temp = shrink(temp,f);
n = size(temp,1);
m = size(temp,2);
stn = round(Np/2 - n/2);
stm = round(Mp/2 - m/2);
im(stn:stn+n-1,stm:stm+m-1,:)=temp;
end
end
>> imshow(shrinkpaste(start,4,0.75))