Friday, June 13, 2008

Assignment #8

1. For question 1, we were asked to modify the pictures by scaling the x and y directions by different factors. By using the Average Over All Pixels method, this Octave command (like the one in Assign#7) does the job.

function [smallimage]=modify(girl,fx,fy);
girl=double(imread("girl.jpg"));
Mp = floor(size(girl,1)*fx);
Np = floor(size(girl,2)*fy);
smallimage(:,:,1) = zeros(Mp,Np);
smallimage(:,:,2) = zeros(Mp,Np);
smallimage(:,:,3) = zeros(Mp,Np);
for i=0:(Mp–1);
for j=0:(Np–1);
for x =floor(i/fx):ceil((i+1)/fx)-1;
for y =floor(j/fy):ceil((j+1)/fy)-1;
ival = girl(x+1,y+1,:);
if (x lessthan i/fx);
ival=ival*(1+x–(i/fx));
end;
if ((x+1)>((i +1)/fx));
ival=ival*(1–(x+1)+((i+1)/fx));
end;
if (y < (j/fy)); ival=ival*(1+y–(j/fy)); end; if ((y+1)>((j+1)/fy));
ival=ival*(1–(y+1)+((j+1)/fy));
end;
smallimage(i+1,j+1,:)=smallimage(i+1,j+1,:)+ival;
end;
end;
smallimage(i+1,j+1,:)=smallimage(i+1,j+1,:)/(1/(fx*fy))^2;
end;
end;
endfunction;

to show image:
A=imread("girl.jpg");
B=modify(A,fx,fy);
C=double(B)/255;
imwrite("girl1a.jpg",C(:,:,1),C(:,:,2),C(:,:,3))

Comparing the original and Scaled image (fx=1 and fy=0.5).




fx=0.5 and fy=1



fx=0.2 and fy=0.8




2. To enlarge colour images by the 'nearest neighbour' method, we use the same commands as those in Assign#7. However, to enlarge images, the f factor for my commands must be greater than 1.

function [smallimage]=enlarge(pic,f);
Mp = floor(size(pic,1)*f);
Np = floor(size(pic,2)*f);
smallimage(:,:,1) = zeros(Mp-1,Np-1);
smallimage(:,:,2) = zeros(Mp-1,Np-1);
smallimage(:,:,3) = zeros(Mp-1,Np-1);
for i=0:(Mp-1);
for j=0:(Np-1);
a=floor((i/f)+1);
b=floor((j/f)+1);
if (a>0)&(a<(size(pic,1)))&(b>0)&(b<(size(pic,2)));
smallimage(i+1,j+1,:)=pic(a,b,:);
end;
end;
end;
endfunction;

To show enlarged image, follow these commands.
A=imread("girl.jpg");
B=enlarge(A,f);
C=double(B)/255;
imwrite("girl2.jpg",C(:,:,1),C(:,:,2),C(:,:,3))

3. To enlarge an image using bilinear interpolation, I found that I could use the same command as in Assign#7 but keeping in mind to enlarge an image, my f factor must be greater than one.

function [smallimage]=enlarge(girl,f);
girl=double(imread("girl.jpg"));
Mp = floor(size(girl,1)*f);
Np = floor(size(girl,2)*f);
smallimage(:,:,1) = zeros(Mp-1,Np-1);
smallimage(:,:,2) = zeros(Mp-1,Np-1);
smallimage(:,:,3) = zeros(Mp-1,Np-1);
for i = 0:(Mp-1);
for j = 0:(Np-1);
a = i/f;
b = j/f;
r = floor(a);
s = floor(b);
if (r>0)&(r<256)&(s>0)&(s<256); k="1:3;" a="imread(" b="enlarge(A,f);" c="double(B)/255;" f ="2.5." onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_CYqQJm5povM/SFPuhkQT2yI/AAAAAAAAAHE/_QlwT-Zso9E/s1600-h/girl.jpg">

No comments: