Digital Image Processing (Part 1) - Image Interpolation (Python)

This is an automatically translated post by LLM. The original post is in Chinese. If you find any translation errors, please leave a comment to help me improve the translation. Thanks!

Digital images play a very important role in contemporary society. Image interpolation, as a basic image processing method, can effectively improve image resolution and enhance visual perception in many cases. There are three main interpolation methods for images:

  • Nearest Neighbor Interpolation
  • Bilinear Interpolation
  • Bicubic Interpolation

Nearest Neighbor Interpolation

As the name suggests, nearest neighbor interpolation fills the pixels of the image by finding the nearest pixel value to the image pixel point during interpolation. This interpolation method has fast computation speed and can quickly produce results. However, it has the disadvantage of poor interpolation effect. Since the pixel values of the nearest pixel points are directly used as the interpolation data, severe aliasing occurs in areas with color changes such as image edges and transitions. Nearest Neighbor Interpolation

The implementation code is as follows:

1
2
3
4
5
6
7
8
9
10
for i in range(2048):
for j in range(2048):
if i<=2044 and j<=2044:
result[i][j]=img[round(i/4)][round(j/4)][0]
else:
result[i][j]=img[math.floor(i/4)][math.floor(j/4)][0]
result=result.astype(np.uint8)
cv.namedWindow("test",cv.WINDOW_NORMAL)
cv.imshow("test",result)
cv.waitKey(0)

Bilinear Interpolation

Bilinear interpolation uses a linear method to interpolate the image. For a certain pixel point in the interpolated image, its pixel value is obtained by linear calculation based on the four nearest pixel values. The interpolation result is good, and the computational complexity is less than that of nearest neighbor interpolation, but much faster than bicubic interpolation. The final interpolated image result is comparable to that obtained by bicubic interpolation, except for some small details. It can be used to quickly interpolate a large number of images. Bilinear Interpolation

The implementation code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for i in range(2048):
for j in range(2048):
if i<2044 and j<2044:
l=i//4
k=j//4
a=i/4-i//4
b=j/4-j//4
result[i][j]=img[l][k][0]*(1-a)*(1-b)+img[l+1][k][0]*a*(1-b)+img[l][k+1][0]*(1-a)*b+img[l+1][k+1][0]*a*b
else:
result[i][j]=img[i//4][j//4][0]
result=result.astype(np.uint8)
cv.namedWindow("test",cv.WINDOW_NORMAL)
cv.imshow("test",result)
cv.waitKey(0)

Nearest neighbor interpolation is used at the image edges.

Bicubic Interpolation

Bicubic interpolation uses a non-linear calculation method to calculate the pixel values of the image using cubic functions. Each pixel value is calculated based on the pixel values of the surrounding 4x4, which is 16 pixel points. The interpolation quality is high, but the computation speed is slow. The interpolation method used by Photoshop is bicubic interpolation. \[ f_3(x,y)=\sum_{i=0}^3\sum_{j=0}^3a_{ij}x_iy_i \]

The implementation code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def weight(x):
a=-0.5
x=abs(x)
if x<=1:
return (a+2)*x**3-(a+3)*x**2+1
if x<2:
return a*x**3-5*a*x**2+8*a*x-4*a
return 0

for i in range(2048):
for j in range(2048):
if i>4 and i<2040 and j>4 and j<2040:
for k in range(4):
for w in range(4):
result[i][j]+=img[i//4+k-1][j//4+w-1][0]*weight(i/4-(i//4+k-1))*weight(j/4-(j//4+w-1))
else:
result[i][j]=img[i//4][j//4][0]
result=result.astype(np.uint8)
cv.namedWindow("test",cv.WINDOW_NORMAL)
cv.imshow("test",result)
cv.waitKey(0)

Conclusion

These three interpolation methods are traditional methods for image interpolation. The improvement of resolution is based on the comprehensive calculation of local pixels to obtain the pixel values of blank pixels. Such interpolation methods can meet the needs of most contemporary society. However, if better interpolation effects are pursued, or if the image resolution needs to be increased by tens of times, such interpolation methods cannot meet the requirements. In this case, other methods such as machine learning need to be used to improve image quality.