# Interpolation algorithm of image scaling function

## brief introduction

In Matlab, the image is zoomed 0.5 times and 3 times by three algorithms: nearest neighbor interpolation, bilinear interpolation and cubic interpolation. The zooming effect of these three algorithms is compared with the original image.

## Nearest neighbor interpolation algorithm

The nearest neighbor interpolation algorithm, also known as zero order interpolation, is to make the gray value of the transformed pixel equal to the gray value of the nearest input pixel.
The nearest neighbor interpolation algorithm is simple and can get satisfactory results in many cases, but when the gray level of the pixel in the image changes slightly,
This method will produce artificial processing trace in the image.

```function[im_n] = myNearest(im,ratio)
%Nearest neighbor interpolation, ratio Is the proportion

%Get the number of rows, columns, and dimensions per pixel of the image (prevent three images)
[line,row,v]=size(im);
%New rows and columns,Pre allocated memory
line_n = round(ratio * line);
row_n = round(ratio * row);
%loop
x_n = 1 : line_n;
y_n = 1 : row_n;
%Prevent spillage
x = round(x_n * (line - 1) / (line_n - 1) + (line_n - line) / (line_n - 1) );
y = round(y_n * (row - 1) / (row_n - 1) + (row_n - row) / (row_n - 1) );
im_n(x_n,y_n) = im(x,y);
end
```

## bilinear interpolation

Bilinear interpolation algorithm is also called bilinear interpolation. After being magnified or reduced several times, the source image coordinates (x * m/m'y * n/n ') corresponding to the Im(x y) point coordinates of the target image are usually floating-point numbers. Suppose it is a P-point, use p (I)_ x + u_x i_y + u_y) Represents, where i_x，i_y represents the integer part, u_x，u_y represents the fractional part. The gray value of P-point is calculated by the linear relationship of gray value of four adjacent points, that is, the gray value of P-point is determined by the four adjacent points. The closer to P-point, the greater the influence factor is, otherwise, the smaller the influence factor is_ x i_y) Point, u in X direction_ The larger the value of X is, the smaller the influence factor is_ U in Y direction_ The larger the value of Y, the smaller the influence factor, so (i_x i_y) The influence value of the point is Im(i_x i_y) ∗ (1 − u_x) ∗ (1 − u_y) , the other three points are similar.
Calculation formula of gray value of P point:
P (x y) = (1 − u_x) ∗ (1 − u_y) ∗ Im(i_x i_y) + (1 − u_x) ∗ u_y ∗ Im(i_x i_y + 1) + u_x ∗ (1 − u_y) ∗ Im(i_x + 1 i_y) + u_x ∗ u_y ∗ Im(i_x + 1 i_y + 1)

```function [im_b] = myBilinear(im, ratio)
%Bilinear interpolation algorithm

%Get the number of rows, columns, and dimensions per pixel of the image (prevent three images from appearing)
[line,row,v]=size(im);
%Convenient for later calculation
im = double(im);
%New rows and columns,Pre allocated memory
line_n = round(ratio * line);
row_n = round(ratio * row);
%Vectorization cycle,Prevent spillage
x_n = 1 : line_n - 1;
y_n = 1 : row_n - 1;
%Projection back to
x = x_n * (line - 1) / (line_n - 1) + (line_n - line) / (line_n - 1);
y = y_n * (row - 1) / (row_n - 1) + (row_n - row) / (row_n - 1);
% x = x_n * (line - 1) / (line_n) + (line_n - line + 1) / (line_n);
% y = y_n * (row - 1) / (row_n) + (row_n - row + 1) / (row_n);
%Rounding original coordinates
i_x = floor(x);
i_y = floor(y);
u_x = x - i_x;
u_y = y - i_y;
w_x = 1 - u_x;
w_y = 1 - u_y;
im_b(x_n,y_n) = w_x' * w_y .* im(i_x,i_y) + w_x' * u_y .* im(i_x,i_y + 1)...
+ u_x' * w_y .* im(i_x + 1,i_y) + u_x' * u_y .* im(i_x + 1,i_y + 1);
%im_b(x_n,y_n) = u_x' .* (im(i_x + 1,i_y)-im(i_x,i_y)) ...
%   + u_y .* (im(i_x ,i_y + 1)-im(i_x,i_y))...
%   +u_x'* u_y .*(im(i_x+1,i_y+1) + im(i_x,i_y) - im(i_x,i_y+1) - im(i_x+1,i_y))...
%   + im(i_x,i_y);
im_b = uint8(im_b);
end
```

## Bicubic interpolation algorithm

Cubic interpolation, also known as cubic convolution interpolation, uses the gray value of 16 points around P to perform cubic interpolation, which can get a higher magnification effect close to the high-resolution image, and also lead to a sharp increase in the amount of computation. The algorithm needs to select interpolation basis function to fit the data. The most commonly used expression of interpolation basis function is as follows
y(x) =
1 − 2|x|2 + x3 |x| < 1
4 − 8|x| + 5|x|2 − |x|3 1 < |x| < 2
0 |x| > 2
I n the same way, the coordinates of the source image (x * m/m'y * n/n ') corresponding to the coordinates of the target image Im(x y) point are usually floating-point numbers, assuming that they are p-points, and using P (I)_ x + u_x i_y + u_y) Means, where i_x，i_y represents the integer part, u_x，u_y represents the fractional part. Here, we need to calculate the coefficients of 16 points around point P respectively, and get them by weighting
Gray value of P point.
The coefficients corresponding to the row and column are calculated for the coordinate points as follows: the distance between the four points on the X-axis of the row and the P-point is 1 + U respectively_ X,u_x,1 − u_x,2 − u_x. The distance between the four points on the Y-axis of the column and the P-point is 1 + U respectively_ y,u_y,1 − u_y,2 − u_y. From the operation of interpolation basis function, Im(i_x i_y) The coefficient corresponding to the point line is y(1 + u_x) , the corresponding coefficient of the column is y (1 + u_y) The coefficient of this point is K00 = y(1 + u_x) ∗ y(1 + u_y). Other points are similar to calculation.

```function [im_c] = myBicubic(im,ratio)
%Bicubic interpolation algorithm

%Get the number of rows, columns, and dimensions per pixel of the image (prevent three images from appearing)
[line,row,v]=size(im);
im = im(:,:,1);
%Convenient for later calculation,Expand four rows and four columns
a=im(1,:);%take im Of1That's ok
c=im(line,:);%take im Of m That's ok
%Expand two rows and two columns before and after the image matrix to be interpolated,Four rows and four columns in total
b=[im(1,1),im(1,1),im(:,1)',im(line,1),im(line,1)];
d=[im(1,row),im(1,row),im(:,row)',im(line,row),im(line,row)];
a1=[a;a;im;c;c];
b1=[b;b;a1';d;d];
im=b1';im=double(im);
%New rows and columns,Pre allocated memory
line_c = round(ratio * line);
row_c = round(ratio * row);
im_c = zeros(line_c,row_c);
%loop,Prevent spillage
for x_c = 1 : line_c
x = (x_c - 1) * (line - 1) / (line_c - 1) + 3;%Projection back to,Projected back point,Only project on the original image position
i_x = floor(x);u_x = x - i_x; %Rounding original coordinates
g_x = [GetWeight(1+u_x),GetWeight(u_x),GetWeight(1-u_x),GetWeight(2-u_x)];
for y_c = 1 : row_c
y = (y_c - 1) * (row - 1) / (row_c - 1) + 3;
i_y = floor(y);u_y = y - i_y; %Rounding original coordinates
g_y = [GetWeight(1+u_y),GetWeight(u_y),GetWeight(1-u_y),GetWeight(2-u_y)];
Need_c=[ im(i_x-1,i_y-1)   im(i_x-1,i_y) im(i_x-1,i_y+1)   im(i_x-1,i_y+2);
im(i_x,i_y-1)     im(i_x,i_y)   im(i_x,i_y+1)     im(i_x,i_y+2);
im(i_x+1,i_y-1)   im(i_x+1,i_y) im(i_x+1,i_y+1)   im(i_x+1,i_y+2);
im(i_x+2,i_y-1)   im(i_x+2,i_y) im(i_x+2,i_y+1)   im(i_x+2,i_y+2)];
im_c(x_c,y_c)=(g_x*Need_c*g_y');
end
end
im_c=uint8(im_c);
end

function [key] = GetWeight(num)
%Get the weight of each point
if num<1 && num>-1
key = 1 - 2 * num^2 + abs(num)^3;
else
key = 4 - 8 * abs(num) + 5 * num^2 - abs(num)^3;
end
end
```

## test result

The original image used in this experiment is three gray-scale images, which are respectively 0.5 times (see Figure 1, figure 3, figure 5) and 3 times zoomed (see Figure 2, figure 4, Figure 6) by three algorithms of nearest neighbor interpolation, bilinear interpolation and cubic interpolation, and the zooming effect is compared.

From the above operation results, the nearest neighbor interpolation algorithm has a serious sawtooth phenomenon in the enlarged image. The bilinear interpolation algorithm still has the defects of image accuracy degradation and image quality degradation due to the ill considered calculation model. However, compared with the nearest neighbor interpolation algorithm, the bilinear interpolation algorithm has only a small amount of improvement in operation, but the amplification effect is significantly improved. It can be widely used in practice when the image quality requirements are not high. The third interpolation has the best effect. It can get more close to the magnification effect of high-resolution image and smoother image edge, but it also leads to a sharp increase in the amount of calculation. It can be applied to some professional mapping software, digital cameras or printers.

## Conclusion

In Matlab, the nearest neighbor interpolation, bilinear interpolation and cubic interpolation are used to scale the image 0.5 times and 3 times respectively, and the scaling effects of the three algorithms are compared. Among them, the third interpolation has the best effect, but the calculation cost is the largest, and the nearest neighbor interpolation has the least running cost, but the sawtooth phenomenon is serious. In practice, an appropriate interpolation algorithm can be selected according to different scaling requirements and machine performance. In addition, this experiment is on the gray-scale image. For the color image, the algorithm is similar. The gray value can be changed to the RGB value for corresponding calculation.

Tags: MATLAB

Posted on Thu, 18 Jun 2020 05:42:19 -0400 by roze