Sunday, June 22, 2008

Assign 7 Q1

Shrinking the image
Please see the following useful link:
http://www.shrinkpictures.com

Method one:
Average over all pixels in the destination point(Sub square average shrink)
Code would be like this:
pkg load image
cd C:\
largeimage=double(imread("p1.jpg"));
f=0.10;
Mp=floor(size(largeimage,1)*f);
Np=floor(size(largeimage,2)*f);
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/f)):ceil((i+1)/f)-1;
for y=(floor(j/f)):ceil((j+1)/f)-1;
ival=largeimage(x+1,y+1,:);
if (x<(i/f)); ival=ival*(1+x-(i/f)); end if ((x+1)>(i+1)/f);
ival=ival*(1-(x+1)+((i+1)/f));
end
if (y<(j/f)); ival=ival*(1+y-(j/f)); end if ((y+1)>(j+1)/f);
ival=ival*(1-(y+1)+((j+1)/f));
end
smallimage(i+1,j+1,:)=smallimage(i+1,j+1,:)+ival;
end
end
end
end
small=smallimage*f*f;
imshow(double(small)/255);


Method 2:
To take the nearest neighborhood.
That is, the pixel at (i,j) on the smaller image
will be equal to the value of the pixel nearest
to (i/f, j/f) on the larger image.


pkg load image
cd C:\
largeimage=imread("p1.jpg");
largeimage=double(largeimage);
f=0.75;
Mp=floor(size(largeimage,1)*f-1);
Np=floor(size(largeimage,2)*f-1);
smallimage(:,:,1)=zeros(Mp-1,Np-1);
smallimage(:,:,2)=zeros(Mp-1,Np-1);
smallimage(:,:,3)=zeros(Mp-1,Np-1);
for i=1:(Mp-1)
for j=1:(Np-1)
a=round(i/f);
b=round(j/f);
smallimage(i,j,:)=largeimage(a,b,:);
end;
end;
imshow(double(smallimage/255))



Method 3
To take bilinear interpolation of the larger image.
The pixel at (i,j) on the smaller image will be equal
to the bilinear interpolation of 4 nearest neighbors of (i/f, j/f).


pkg load image
cd C:\
largeimage=double(imread("P1.jpg"));
f=0.75;
Mp=floor(size(largeimage,1)*f);
Np=floor(size(largeimage,2)*f);
smallimage(:,:,1)=zeros(Mp,Np);
smallimage(:,:,2)=zeros(Mp,Np);
smallimage(:,:,3)=zeros(Mp,Np);
for i=1:Mp
for j=1:Np
a=i/f;
b=j/f;
r=floor(a);
s=floor(b);
if (r>0) &(r(lessthan size(largeimage,1)&(s>0)&(s(lessthan size(largeimage,2)
for k=1:3
smallimage(i,j,k)=[1-a+r,a-r]*[largeimage(r,s,k),largeimage(r,s+1,k);largeimage(r+1,s,k),largeimage(r+1,s+1,k)]*[1-b+s;b-s];
end;
end;
end;
end;
imshow(smallimage/255)








No comments: