The method zoom() will get the image name and the zoom factor as arguments and produce the required zoomed image using both Nearest neighbour and Bilinear interpolation methods to help us compare the results.
In nearest neighbour method, I just repeated the rows and columns to get the required zoom level.
In bilinear interpolation method, first I copied the original pixel values to a new matrix and did the interpolation in two steps. First, I interpolated the columns (in x-direction) using original pixel values. Then, I interpolated the rows (in y-direction) which finished the bilinear interpolation.
Matlab Code
function zoom (name,n)
close all
im=imread(name);
im=rgb2gray(im);
imshow(im);
[r,c]=size(im);
% NEAREST NEIGHBOUR
x=zeros(r*n,c*n);
for i=1:r
for j=1:c
x(i*n-n+1:i*n,j*n-n+1:j*n)=repmat(im(i,j),n,n);
end
end
figure
subplot (1 ,2 ,1)
imshow(mat2gray(x));
title('Nearest Neighbour');
% BILINEAR
im = im2double(im);
[r,c]=size(im);
y=zeros(r*n-n+1,c*n-n+1);
% COPY THE ORIGINAL VALUES
for i=1:r
for j=1:c
y(i*n-n+1,j*n-n+1)=im(i,j);
end
end
% INTERPOLATE IN X-DIRECTION
[row,col]=size(y);
for i=1:n:row
for j=1:col
m=mod(j,n);
if(m==0)
m=n;
end
if(m~=1)
y(i,j)=((m-1)*y(i,j-(m-1)+n) + (n-(m-1))*y(i,j-(m-1)))/n;
end
end
end
% INTERPOLATE IN Y-DIRECTION
for i=1:row
for j=1:col
m=mod(i,n);
if(m==0)
m=n;
end
if(m~=1)
y(i,j)=((m-1)*y(i-(m-1)+n,j) + (n-(m-1))*y(i-(m-1),j))/n;
end
end
end
subplot (1 ,2 ,2)
imshow(y);
title('Bilinear');
end
Output
You can notice that, bilinear interpolation method gives more smooth output.
here what is the 'n' mean?
ReplyDeleteHere the 'n' means the scale of the zoom. 2x, 3x like that, where n is 2 or 3.
Deletesimple and lucid explanation
ReplyDeletei am getting error input argument n is undefined
ReplyDeletecan you please insert bicubic method also?
ReplyDeletehere it doesnt zoom the image...i need the image to be zoomed using bilinear interpolation....
ReplyDeleteIt zooms the image, when you save the image you can verify.
Delete-> before the "end" keyword, add "imwrite(y,'zoomed.jpg','jpg');" and run the code to save the result.
Good explanation!! Just, I don't get the formulas, how did you make them??? Sorry!!
ReplyDeletehttp://www.cambridgeincolour.com/tutorials/image-interpolation.htm
DeleteThis may be helpful...
I was curious about checking the performance of the algorithm in terms of the execution time. I tried interpolating a simple image (using n=3) using the Nearest Neighbor and the algorithm took about 5.666231 seconds to generate the final image (the time, however, will depend on the hardware and software constraints). I developed an alternative code which, for the same machine constraints, took about 0.253769 seconds to reach the same results. Here is my code:
ReplyDeletefunction [ scaledImg ] = imgResize( inputImg, factor, method )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
[rows, cols, channel] = size(inputImg);
scaledImg=zeros(rows*factor, cols*factor, channel);
switch method
case 0
tic
for m=1:channel
for i=1:(rows*factor)
for j=1:(cols*factor)
x=floor(i/factor);
y=floor(j/factor);
if x==0
x=1;
end
if y==0
y=1;
end
scaledImg(i,j,m) = inputImg(x,y,m);
end
end
end
toc
end
size(scaledImg)
figure(1);imshow(inputImg, []);
figure(2);imshow(uint8(scaledImg), []);
end
how to do bicubic interpolation
ReplyDeleteHOW TO PERFORM CUBIC B SPLINE INTERPOLATION???
ReplyDeleteThank you. This simple code helped me a lot.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteplease give bicubic interpolation code
ReplyDeletei need codes on bilinear and bicubic interpolation, anyone help????
ReplyDeletewhy not..!!
Delete