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(___)
- knn- From image I, the nearest neighbor algorithm generates the Nearest Neighbor Interpolation.
- 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.
- [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:
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:
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.
Contact Us