First, the nearest neighbor interpolation algorithm thought & step:
1. Depending on the magnification, create a new size of the original image size * multiples of the 0 Matrix 2.0 matrix of each pixel value based on the original image, that is, the x,y divided by multiples of the decimal rounding (round function in MATLAB to take the nearest integer decimal) 3. For the edge of the situation to be aware
The nearest neighbor interpolation is simple and intuitive, the speed is the fastest, but the image quality is not high.
Code Demo:
A=imread (' E:\matlab\work\tiger.jpg ');% read image information
imshow (A);% Show original
title (' original ')
; Row=size (a,1);
Col=size (a,2);% image rows and columns
nn=2;% magnification
m=round (nn*row);% to find the maximum value of the transformed coordinates
n=round (nn*col);
B=zeros (m,n,3);% defines the transformed image for the
i=1:m for
j=1:n
x=round (I/NN);
Y=round (J/NN);% minimum proximity method for image interpolation
% processing edge
if x==0 x=1;end
if y==0 y=1;end
if X>row x=ro W;end
if Y>col y=col;end
B (i,j,:) =a (x,y,:);
End
-end b=uint8 (B);% converts the matrix to a 8-bit unsigned integer
figure;
Imshow (B);
Title (' Nearest proximity interpolation method amplification ');
Effect:
Two, bilinear interpolation algorithm thought & step:
The double linear interpolation method has a large amount of computation, but the image quality is high after zooming, and the pixel value is not discontinuous.
Code Demo:
I=imread (' E:\matlab\work\tiger.jpg ');
Figure,imshow (I);
Title (' Origin image ');
[Rows cols tongdao]=size (I);
I=double (I);
k=2;
X_new=rows*k;
y_new=cols*k;% Scale to K
-fold I_new=zeros (X_new,y_new,tongdao);
For Rgb=1:tongdao to
i=1:x_new for
j=1:y_new
X=i/k;a=floor (x);
Y=j/k;b=floor (y);% bilinear interpolation algorithm
if A>0&&b>0&&a<rows&&b<cols
cxb=i (a+1,b , RGB) * (x-a) +i (A,B,RGB) * (1+a-x);
Cxb1=i (A+1,B+1,RGB) * (x-a) +i (A,B+1,RGB) * (1+a-x);
I_new (I,J,RGB) =round (cxb1* (y-b) +cxb* (1+b-y));
End-end-end
figure,imshow (uint8 (i_new));
Title (' result image ');
Effect:
Ps:
In Matlab, a bilinear interpolation algorithm can be realized by using its own function imresize ().
The MATLAB source code of bilinear interpolation algorithm is:
A=imread (' ... ');
C=imresize (a,8, ' bilinear '); %8 is a multiplier of magnification.