Open In App

Nearest-Neighbor Interpolation Algorithm in MATLAB

Last Updated : 26 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Nearest neighbor interpolation is a type of interpolation. This method simply determines the “nearest” neighboring pixel and assumes its intensity value, as opposed to calculating an average value using some weighting criteria or producing an intermediate value based on intricate rules.

Interpolation:

A method for adding new data points within a range of a set of known data points is called interpolation. Interpolation can be used to fill in gaps in data, smooth out data, make predictions, and more. Techniques for data points on a grid and scattered data points are the two subsets of interpolation in MATLAB

The process of interpolation involves figuring out the unknown values that lie between the known data points. For any geographically related data points, such as noise level, rainfall, elevation, and so forth, it is primarily used to forecast unknown values.

The binary search algorithm is enhanced by the interpolation search algorithm.

Nearest-neighbor Interpolation Method:

The simplest method is a round interpolation (also known as nearest-neighbor interpolation), which simply finds the closest data value at an integer position by rounding the expected position’s value. 

Nearest-neighbor Algorithm:

The k-nearest neighbor’s algorithm, also referred to as KNN or k-NN, is a supervised learning classifier that uses proximity to make classifications or predictions about the grouping of a single data point. Although it can be applied to classification or regression problems, it is typically used as a classification algorithm because it relies on the idea that similar points can be found close to one another.

The number of nearest neighbors that should be included in the majority of the voting process is referred to as "k" in KNN.       

The pixel values present in the input vector or matrix are re-sampled using this technique, which is the simplest one. Interpolating the images in MATLAB is done using the “imresize” function.

Syntax:

knn = nearest neighbor(I)

knn = nearest neighbor(I,Name,Value)

[knn,SI] = nearest neighbor(___)

  1. knn- From image I, the nearest neighbor algorithm generates the Nearest Neighbor Interpolation.
  2. knn- Depending on the values of the optional name-value pair arguments, the nearest neighbor (I, Name, Value) returns one or more nearest neighbor algorithm matrices.
  3. [knn,SI]- The scaled image, SI, used to calculate the nearest neighbor algorithm matrix is returned by the nearest neighbor interpolation method..

Example 1:

Matlab

% MATLAB CODE for READ AN INPUT IMAGE
X=imread('image.tif');
  
% SET THE SIZE OF THE RESAMPLE
Col = 256;
Row = 256;
  
% contrast the new size with the previous size.
rtR = Row/size(X,1);
rtC = Col/size(X,2);
  
%CARRY OUT THE INTERPOLATED POSITIONS
IR = cell
         ([1:(size(X,1)*rtR)]./(rtR));
IC = cell
         ([1:(size(X,2)*rtC)]./(rtC));
  
%WISE ROW INTERPOLATION
Y = X(IR);
  
%WISE INTERPOLATION BY COLUMN
Y = Y(IC);
figure,subplot(101),imshow(X);
title('BEFORE INTERPOLATION'); 
axis([0,256,0,256]);axis on;
subplot(111),imshow(Y);
title('AFTER INTERPOLATION');  
axis([0,256,0,256]);axis on;

                    

Output:

BEFORE INTERPOLATION

AFTER INTERPOLATION

Example 2:

Matlab

% MATLAB CODE FOR READ AN INPUT IMAGE
X=imread('GEEKSFORGEEKS.tif');
  
% SET THE SIZE OF THE RESAMPLE
Col = 256;
Row = 256;
  
% Contrast the new size with the previous size.
rtR = Row/size(X,1);
rtC = Col/size(X,2);
  
% CARRY OUT THE INTERPOLATED POSITIONS
IR = cell
         ([1:(size(X,1)*rtR)]./(rtR));
IC = cell
         ([1:(size(X,2)*rtC)]./(rtC));
  
% WISE ROW INTERPOLATION
Y = X(IR);
  
% WISE INTERPOLATION BY COLUMN
Y = Y(IC);
figure,subplot(101),imshow(X);
title('BEFORE INTERPOLATION'); 
axis([0,256,0,256]);axis on;
subplot(111),imshow(Y);
title('AFTER INTERPOLATION');  
axis([0,256,0,256]);axis on;

                    

Output:

BEFORE INTERPOLATION

AFTER INTERPOLATION



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads