Linear interpolation 2: bilinear interpolation

For an image, if it needs to be zoomed in and zoomed out, especially how can the image not retain more of the nature of the original image and be clearer? Most of the processing methods are linear interpolation. This article introduces one of the most commonly used methods in linear interpolation – bilinear interpolation.

Before understanding bilinear interpolation, first understand the simplest single-line interpolation.

single linear interpolation

point(x,y) is the point( x 0 x_0x0y0 y_0y0) and points ( x 1 x_1x1y1y_1y1), if the figure on the left of the figure below, if x is known, how to find the left side of y can be easily solved only through the proportional relationship between the knowledge line segments in junior high school, as shown in the figure on the right.
insert image description here
Finally, y = x 1 − xx 1 − x 0 . y 0 + x − x 0 x 1 − x 0 . y 1 y = \frac{x_1 - x}{x_1 - x_0}.y_0 + \frac{x - x_0}{x_1 - x_0}.y_1y=x1x0x1x.y0+x1x0xx0.y1

bilinear interpolation

Two-line interpolation is logically similar to single-line interpolation, except that the picture plane is two-dimensional, and the natural coordinates should also be two-dimensional. The difference from a straight line is that a point should be determined by two straight lines.

As shown in the figure below, point P is the point to be interpolated. If you want to solve the y left side of point P, you need to solve R 1 and R 2 R_1 and R_2R1and R2coordinates, so you need to first R 1 R_1R1and R 2 R_2R2is interpolated, then R 1 R_1R1and R 2 R_2R2Perform interpolation, that is, perform linear interpolation twice in the x direction, and then perform a linear interpolation in the y direction.

insert image description here
The interpolation process is as follows:
insert image description here
In two-line interpolation, Q 11 , Q 12 , Q 21 , Q 22 Q_{11}, Q_{12}, Q_{21}, Q_{22}Q11Q12Q21Q22are four adjacent points, so x 2 − x 1 = 1 , y 2 − y 1 = 1 x_2-x_1=1,y_2-y_1=1x2x1=1,y2y1=1
The relationship between the target image and the original image:
D x = S x ∗ D w S w D y = S y ∗ D h S h D_x = S_x * \frac{D_w}{S_w} ~~~~~~~ ~~~~~~D_y = S_y * \frac{D_h}{S_h}Dx=SxSwDw             Dy=SyShDh
∴ S x = D x ∗ S w D w               S y = D y ∗ S h D h \therefore S_x = D_x * \frac{S_w} {D_w}~~~~~~~~~~~~~S_y = D_y * \frac{S_h} {D_h} Sx=DxDwSw             Sy=DyDhSh

However, there is currently a problem. If the origin of the target image and the original image are selected as the upper left corner or the lower left corner as the corresponding coincidence, the entire image will not correspond uniformly, so the geometric center should be selected as the coincidence point.

Geometric center derivation:
Let the original image be M ∗ MM*MMM , target imageN ∗ NN*NNN , the position of the target image in the original image coordinate system( x , y ) (x,y)(x,y ) , the original image coordinates( xm , ym ) (x_m, y_m)(xm,ym) , geometric center( x M − 1 2 , y M − 1 2 ) , (x_{\frac{M-1}{2}},y_{\frac{M-1}{2}}),(x2M1,y2M1) , target image coordinates( xn , yn ) (x_n, y_n)(xn,yn)

x = n M N x = n\frac{M}{N} x=nNM
几何中心相同 → M − 1 2 + Z = ( N − 1 2 + Z ) ∗ M N → Z ∗ ( 1 − M N ) = N − 1 2 N ∗ M − M − 1 2 N ∗ N → Z ∗ N − M N = N − M 2 N → Z = 1 2 \rarr \frac{M-1}{2} + Z = (\frac{N-1}{2} + Z) * \frac{M}{N} \\ \rarr Z*(1-\frac{M}{N}) = \frac{N-1}{2N}*M -\frac{M-1}{2N}*N \\ \rarr Z*\frac{N-M}{N} = \frac{N-M}{2N} \rarr Z = \frac{1}{2} 2M1+Z=(2N1+Z)NMZ(1NM)=2N _N1M2N _M1NZNNM=2N _NMZ=21
∴ Z = 1 2 \therefore Z= \frac{1}{2} Z=21When , the geometric centers of the original image and the target image coincide.
∴ S x = ( D x + 0.5 ) ∗ S w D w − 0.5 S y = ( D y + 0.5 ) ∗ S h D h − 0.5 \therefore S_x = (D_x + 0.5) * \frac{S_w} {D_w } -0.5 ~~~~~~~~~~~~ S_y = (D_y + 0.5) * \frac{S_h} {D_h} -0.5Sx=(Dx+0.5)DwSw0.5             Sy=(Dy+0.5)DhSh0.5

Two-line interpolation substitution, processing of four point coordinates:

Q 11 ( f l o o r ( S x ) , f l o o r ( S y ) ) … … ( x 1 , y 1 ) Q 12 ( f l o o r ( S x ) , c e i l ( S y ) ) … … ( x 1 , y 2 ) Q 21 ( f l o o r ( S x + 1 ) , f l o o r ( S y ) ) … … ( x 2 , y 1 ) Q 22 ( f l o o r ( S x + 1 ) , c e i l ( S y ) ) … … ( x 2 , y 2 ) \begin{array}{l} Q_{11} (floor(S_x), floor(S_y))……(x_1,y_1)\\ Q_{12} (floor(S_x),ceil(S_y))……(x_1,y_2)\\ Q_{21} (floor(S_x+1),floor(S_y))……(x_2,y_1)\\ Q_{22} (floor(S_x+1),ceil(S_y))……(x_2,y_2)\\ \end{array} Q11(floor(Sx),floor(Sy))……(x1,y1)Q12(floor(Sx),ce i l ( Sy))……(x1,y2)Q21(floor(Sx+1),floor(Sy))……(x2,y1)Q22(floor(Sx+1),ce i l ( Sy))……(x2,y2)

f ( x , y ) = ( x 2 − x ) f ( Q 11 ) + ( y − y 1 ) ( x 2 − x ) f ( Q 12 ) + ( x − x 1 ) f ( Q 22 ) f(x,y)=(x_2-x)f(Q_{11}) + (y-y_1)(x_2-x)f(Q_{12})+(x-x_1)f(Q_{22}) f(x,y)=(x2x)f(Q11)+(yy1)(x2x)f(Q12)+(xx1)f(Q22)

Bilinear interpolation handwritten code

def bilinear_interpolation(source_image, shape):
    src_h, src_w, channel = source_image.shape  # 原图片的高、宽、通道数
    dst_h, dst_w = shape[1], shape[0]  # 输出图片的高、宽
    if src_h == dst_h and src_w == dst_w:
        return source_image
    dst_image = np.zeros((dst_h, dst_w, 3), dtype=np.uint8)
    scale_x, scale_y = float(src_w) / dst_w, float(src_h) / dst_h
    for i in range(channel):
        for dst_y in range(dst_h):
            for dst_x in range(dst_w):
                # 源图像和目标图像几何中心的对齐
                src_x = (dst_x + 0.5) * scale_x - 0.5
                src_y = (dst_y + 0.5) * scale_y - 0.5
                # 计算在源图上四个近邻点的位置
                src_x0 = int(np.floor(src_x))
                src_y0 = int(np.floor(src_y))
                src_x1 = min(src_x0 + 1, src_w - 1)
                src_y1 = min(src_y0 + 1, src_h - 1)

                # 双线性插值
                temp0 = (src_x1 - src_x) * source_image[src_y0, src_x0, i] + (src_x - src_x0) * source_image[
                    src_y0, src_x1, i]
                temp1 = (src_x1 - src_x) * source_image[src_y1, src_x0, i] + (src_x - src_x0) * source_image[
                    src_y1, src_x1, i]
                dst_image[dst_y, dst_x, i] = int((src_y1 - src_y) * temp0 + (src_y - src_y0) * temp1)

    return dst_image

bilinear interpolation opencv code

cv2.resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None) is a bilinear interpolation
as a parameter interpolation=INTER_LINEAR(默认).

The calculation amount of bilinear interpolation method is much more complicated than that of the nearest neighbor interpolation method, but it avoids the disadvantage of gray discontinuity, and the image is smoother.

Guess you like

Origin blog.csdn.net/Zeus_daifu/article/details/128774540