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 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('w3wiki.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



Nearest-Neighbor Interpolation Algorithm in MATLAB

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.

Similar Reads

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...

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....

Contact Us