Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Nearest Neighbor Interpolation


This method is the simplest technique that re samples the pixel values present in the input vector or a matrix. In MATLAB, ‘imresize’ function is used to interpolate the images.


Consider the following example,



The pictorial representation depicts that a 3x3 matrix is interpolated to 6x6 matrix. The values in the interpolated matrix are taken from the input matrix (i.e) no new value is added.

MATLAB CODE:

%3x3 Matrix
A = zeros(3,3);
A(:)=[10,2,9,4,18,14,22,7,25];

display(A); %Before Interpolation

C = imresize(C,[6,6],'nearest');
display(C); %After Interpolation

EXPLANATION:
The result as shown in the pictorial representation can be achieved using the MATLAB function ‘imresize’
Now let’s see how to perform nearest neighbor interpolation without using the MATLAB ‘imresize’ function



MATLAB CODE:
%READ AN INPUT IMAGE
A=imread('cameraman.tif');


% DEFINE THE RESAMPLE SIZE
Col = 512;
Row = 512;


%FIND THE RATIO OF THE NEW SIZE BY OLD SIZE
rtR = Row/size(A,1);
rtC = Col/size(A,2);


%OBTAIN THE INTERPOLATED POSITIONS
IR = ceil([1:(size(A,1)*rtR)]./(rtR));
IC = ceil([1:(size(A,2)*rtC)]./(rtC));


%ROW_WISE INTERPOLATION
B = A(:,IR);


%COLUMN-WISE INTERPOLATION
B = B(IC,:);



figure,subplot(121),imshow(A);title('BEFORE INTERPOLATION'); axis([0,512,0,512]);axis on;

subplot(122),imshow(B);title('AFTER INTERPOLATION');  axis([0,512,0,512]);axis on;




MATLAB CODE :

clear all
clc
close all
%READ A RGB IMAGE
A=imread('peppers.png');


% DEFINE THE RESAMPLE SIZE
Col = 256;
Row = 192;


%FIND THE RATIO OF THE NEW SIZE BY OLD SIZE
rtR = Row/size(A,1);
rtC = Col/size(A,2);

%OBTAIN THE INTERPOLATED POSITIONS
IR = ceil([1:(size(A,1)*rtR)]./(rtR));
IC = ceil([1:(size(A,2)*rtC)]./(rtC));


%RED CHANNEL
Temp= A(:,:,1);
%ROW-WISE INTERPOLATION
Red = Temp(IR,:);
%COLUMNWISE INTERPOLATION
Red = Red(:,IC);



%GREEN CHANNEL
Temp= A(:,:,2);
%ROW-WISE INTERPOLATION
Green = Temp(IR,:);
%COLUMNWISE INTERPOLATION
Green = Green(:,IC);


%BLUE CHANNEL
Temp= A(:,:,3);
%ROW-WISE INTERPOLATION
Blue = Temp(IR,:);
%COLUMNWISE INTERPOLATION
Blue = Blue(:,IC);



Output=zeros([Row,Col,3]);
Output(:,:,1)=Red;
Output(:,:,2)=Green;
Output(:,:,3)=Blue;

Output = uint8(Output);

figure,subplot(121),imshow(A);title('BEFORE INTERPOLATION'); axis([0,512,0,384]);axis on;

subplot(122),imshow(Output);title('AFTER INTERPOLATION');  axis([0,512,0,384]);axis on;




EXPLANATION:

For RGB image, the Red, Green and Blue channels are interpolated separately.




like button Like "IMAGE PROCESSING" page

0 comments:

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com