[Digital Image Processing] Geometric transformation of images


Two images containing the same content may have completely different appearances due to geometric distortion caused by imaging angle, perspective relationships, or even the lens itself.
Through appropriate geometric transformation, the negative effects of these geometric distortions can be eliminated to the greatest extent, which is beneficial to focusing on the content of the image itself in subsequent processing and recognition work.

General ideas of image geometric transformation

A geometric transformation requires two parts of the operation:

The first is the operations required for spatial transformation, such as translation, rotation, and mirroring, which need to be used to represent the mapping relationship between the output image and the input image;

In addition, a grayscale interpolation algorithm needs to be used to calculate according to this transformation relationship. The pixels of the output image may be mapped to non-integer coordinates of the input image.

Assume the original image f ( x 0 , y 0 ) f(x_0, y_0)f(x0,y0) The target image generated by geometric transformation isg (x 1, y 1) g(x_1, y_1)g(x1,y1) , then the spatial transformation mapping relationship can be expressed as:
x 1 = s ( x 0 , y 0 ) (1) x_1 = s(x_0, y_0) \tag{1}x1=s(x0,y0)(1)

y 1 = t ( x 0 , y 0 ) (2) y_1 = t(x_0, y_0) \tag{2} y1=t(x0,y0)(2)

Among them, s ( x 0 , y 0 ) s(x_0, y_0)s(x0,y0) t ( x 0 , y 0 ) t(x_0, y_0) t(x0,y0) is given byf ( x 0 , y 0 ) f(x_0, y_0)f(x0,y0) g ( x 1 , y 1 ) g(x_1, y_1) g(x1,y1) coordinate transformation function.

For example, x 1 = s ( x 0 , y 0 ) = 2 x 0 x_1 = s(x_0, y_0) = 2 x_0x1=s(x0,y0)=2x _0 y 1 = t ( x 0 , y 0 ) = 2 y 0 y_1 = t(x_0, y_0) = 2 y_0 y1=t(x0,y0)=2 y0When, the transformed image g (x 1, y 1) g(x_1, y_1)g(x1,y1) just simply atxxxyyIn the two spatial directions of y , f (x 0, y 0) f(x_0, y_0)f(x0,y0) is doubled in size.

Mastered the transformation function s ( x 0 , y 0 ) s(x_0, y_0)s(x0,y0) t ( x 0 , y 0 ) t(x_0, y_0) t(x0,y0) , you can follow the steps below to implement geometric transformation.

Algorithm 4.1 steps:

  • According to the mapping relationship of the spatial transformation, determine the size of the transformed target image, that is, the row and column range, because some transformations may change the image size;
  • Calculate the inverse transformation s − 1 ( j 1 , i 1 ) s^{-1} (j_1, i_1)s1(j1,i1) t − 1 ( j 1 , i 1 ) t^{-1} (j_1, i_1) t1(j1,i1)
  • Progressively scan the target image g ( x 1 , y 1 ) g(x_1, y_1)g(x1,y1) , forg ( x 1 , y 1 ) g(x_1, y_1)g(x1,y1) in each point( j 0 , j 0 ) (j_0, j_0)(j0,j0)
    • According to the mapping relationship of spatial transformation, it is calculated:
      • j 0 ′ = s − 1 ( j 1 , i 1 ) ; j_0' = s^{-1}(j_1, i_1); j0=s1(j1,i1) ; // The abscissa calculated directly through the mapping relationship may not be an integer;
      • i 0 ′ = t − 1 ( j 1 , i 1 ) ; i_0' = t^{-1}(j_1, i_1); i0=t1(j1,i1) ; // The vertical coordinate calculated directly through the mapping relationship may not be an integer;
    • Depending on the chosen interpolation method:
      • ( j 0 , i 0 ) = i n t e r p ( j 0 ′ , i 0 ′ ) ; (j_0, i_0) = interp(j_0', i_0'); (j0,i0)=in t er p ( j0,i0) ; // for non-integer coordinates( j 0 ′ , i 0 ′ ) (j_0', i_0')(j0,i0) requires interpolation
      • i f if if ( j 0 , i 0 ) (j_0, i_0) (j0,i0) in imageffwithin f
        • Copy the corresponding pixel: g ( j 1 , i 1 ) = f ( j 0 , i 0 ) ; g(j_1, i_1) = f(j_0, i_0);g(j1,i1)=f(j0,i0);
      • else elseelse
        • g ( j 1 , i 1 ) = 255 ; g(j_1, i_1) = 255; g(j1,i1)=255;

The restoration (correction) process of geometrically distorted images is exactly the inverse process of the above transformation.
x 0 = s − 1 ( x 1 , y 1 ) (3) x_0 = s^{-1} (x_1, y_1) \tag{3}x0=s1(x1,y1)(3)

y 0 = t − 1 ( x 1 , y 1 ) (4) y_0 = t^{-1}(x_1, y_1) \tag{4} y0=t1(x1,y1)(4)

Equations (3) and (4) represent the corresponding g (x 1, y 1) g(x_1, y_1)g(x1,y1) tof ( x 0 , y 0 ) f(x_0, y_0)f(x0,y0) inverse transformation. At this time, the image g ( x 1 , y 1 ) g(x_1, y_1)distorted by geometric transformationg(x1,y1) is the object to be restored, the original imagef ( x 0 , y 0 ) f(x_0, y_0)f(x0,y0) is the goal of recovery.

When image normalization is used to eliminate changes in image appearance caused by geometric factors, it is called image geometric normalization. It can eliminate the differences in geometric relationships between objects and find those geometric invariants in the image, thereby knowing these objects. Are originally the same or belong to the same category.


image pan

Image translation is to move all points in the image horizontally or vertically according to the specified translation amount.
Let (x 0, y 0) (x_0, y_0)(x0,y0) is a point on the original image, and the horizontal translation amount of the image isT x T_xTx, the vertical translation amount is T y T_yTy,
the point coordinates after translation become (x 1, y 1) (x_1, y_1)(x1,y1) , becomes
x 1 = x 0 + tx x_1 = x_0 + t_xx1=x0+tx
y 1 = y 0 + ty y_1 = y_0 + t_yy1=y0+ty

Define the function
[ x 1 y 1 1 ] = [ x 0 y 0 1 ] [ 1 0 0 0 1 0 T x T y 1 ] (5) \begin{bmatrix} x_1 & y_1 & 1 \end{bmatrix}; = \begin{bmatrix}x_0&y_0&1\end{bmatrix}\begin{bmatrix}1&0&0\\0&1&0\\T_x&T_y&1\end{bmatrix}\tag{5}[x1y11]=[x0y01] 10Tx01Ty001 ( 5 )
Invert the transformation matrix to get the inverse transformation:
[ x 0 y 0 1 ] = [ x 1 y 1 1 ] [ 1 0 0 0 1 0 − T x − T y 1 ] (6) \begin{ bmatrix} x_0 & y_0 & 1 \end{bmatrix} = \begin{bmatrix}x_1 & y_1 & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ -T_x & - T_y & 1 \end{bmatrix} \tag{6}[x0y01]=[x1y11] 10Tx01Ty001 (6)

{ x 0 = x 1 − T x y 0 = y 1 − T y (7) \left \{ \begin{matrix} x_0 = x_1 - T_x \\ y_0 = y_1 - T_y \end{matrix} \right. \tag{7} { x0=x1Txy0=y1Ty( 7 )
From the above formula, we can get that every point in the translated target image can find the corresponding point in the original image.

For example, for (i, j) (i,j) in the new graph(i,j ) pixel, into the formula you can get its corresponding pixel in the original image( i − T x , j − T y ) (i-T_x, j-T_y)(iTx,jTy) . At this time, ifT x > i T_x > iTx>i T y > j T_y > j Ty>j,点 ( i − T x , j − T y ) (i-T_x, j-T_y) (iTx,jTy) is beyond the scope of the original image, and its pixel values ​​can be directly set to0 00 or255 255255

There are usually two processing methods for the points in the original image that have been moved out of the image display area. They can be discarded directly, or the width of the newly generated image can be increased by T x T_x by appropriately increasing the size of the target image.Tx, the height increases T y T_yTyThe method enables these points to be included in the new image.

imtransformFunctions are used to complete general two-dimensional space transformations.
Function prototype:B = imtransform(A, TFORM, method);

  • A is the image to be geometrically transformed;
  • TFORM specifies the specific transformation type;
  • method allows you to select the interpolation method for the imtransform function. The optional interpolation methods are: bicubic, bilinear, nearest

TFORM structures can be created in two ways, using maketformfunctions and cp2tformfunctions.
cp2tform is a data fitting function that requires the corresponding point pairs between the original image and the target image as input to determine the geometric transformation relationship based on the control point pair.

maketform function calling form:T = maketform(transformtype, Matrix);

  • The parameter transformtype specifies the type of transformation;
  • The parameter Matrix is ​​the corresponding affine transformation matrix. For example, for translation transformation, the matrix is ​​[1,0,0;0,1,0;T_x,T_y,1]

image mirroring

Image mirroring transformation is divided into horizontal mirroring and vertical mirroring. Horizontal mirroring converts the left and right halves of the image with the vertical central axis of the image as the central axis; vertical mirroring converts the upper and lower half of the image with the horizontal central axis of the image as the central axis.

Determine the range of the equations:
[ x 1 y 1 1 ] = [ x 0 y 0 1 ] [ − 1 0 0 0 1 0 W idth 0 1 ] = [ W idth − x 0 y 0 1 ] (8) \begin {bmatrix} x_1 & y_1 & 1 \ end {bmatrix} = \begin {bmatrix} x_0 & y_0 & 1 \end {bmatrix} \begin{bmatrix} -1 & 0 & 0 \\ 0 & 1 & 0 \\ Width &0&1\end{bmatrix} = \begin{bmatrix}Width-x_0&y_0&1\end{bmatrix}\tag{8}[x1y11]=[x0y01] 10Width010001 =[Widthx0y01]( 8 )
Specific graph:
[ x 0 y 0 1 ] = [ x 1 y 1 1 ] [ − 1 0 0 0 1 0 W idth 0 1 ] = [ W idth − x 1 y 1 1 ] (9 ) \begin {bmatrix} x_0 & y_0 & 1 \ end {bmatrix} = \begin {bmatrix} x_1 & y_1 & 1 \end {bmatrix} \begin{bmatrix} -1 & 0 & 0 \\ 0 & 1 & \\Width&0&1\end{bmatrix} = \begin{bmatrix} Width - x_1&y_1&1\end{bmatrix}\tag{9}[x0y01]=[x1y11] 10Width010001 =[Widthx1y11]( 9 )
The vertical mirror transformation relationship can be formally described as follows:
[ x 1 y 1 1 ] = [ x 0 y 0 1 ] [ 1 0 0 0 − 1 0 0 H eight 1 ] (10) \begin{bmatrix }x_1 & y_1 & 1 \end{bmatrix} = \begin{bmatrix} x_0 & y_0 & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & -1 & 0 \\ 0 & Height & 1 \end{bmatrix} \tag{10}[x1y11]=[x0y01] 10001Height001 ( 10 )
Define:
[ x 0 y 0 1 ] = [ x 1 y 1 1 ] [ 1 0 0 0 − 1 0 0 H eight 1 ] (11) \begin{bmatrix}x_0 & y_0 & 1 \end {bmatrix} = \begin{bmatrix}x_1&y_1&1\end{bmatrix}\begin{bmatrix}1&0&0\\0&-1&0\\0&Height&1\end{bmatrix}\ tag{11}[x0y01]=[x1y11] 10001Height001 (11)


image transpose

Image transpose is to transform the xx of image pixelsx coordinate andyyThe y coordinates are swapped, the size of the image will change after transposition, and the height and width will be swapped.

The transpose transformation formula is as follows:
[ x 1 y 1 1 ] = [ x 0 y 0 1 ] [ 0 1 0 1 0 0 0 0 1 ] (12) \begin{bmatrix}x_1 & y_1 & 1 \end{bmatrix} = \begin{bmatrix}x_0 & y_0 & 1 \end{bmatrix} \begin{bmatrix} 0 & 1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{bmatrix} \tag{12}[x1y11]=[x0y01] 010100001 ( 12 )
The inverse matrix of the transposed matrix is ​​still itself.


Image zoom

Image scaling means that the image is enlarged or reduced according to a specified ratio.

Assume image xxThe scaling ratio in the x- axis direction isS x S_xSxyyThe scaling ratio in the y- axis direction isS y S_ySy, the corresponding transformation expression is:
[ x 1 y 1 1 ] = [ x 0 y 0 1 ] [ S x 0 0 0 S y 0 0 0 1 ] = [ x 0 ⋅ S xy 0 ⋅ S y 1 ] ( 13) \begin{bmatrix} x_1 & y_1 & 1 \end{bmatrix} = \begin{bmatrix} x_0 & y_0 & 1 \end{bmatrix} \begin{bmatrix} S_x & 0 & 0 \\ 0 & S_y & 0 \\ 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix}x_0 \cdot S_x & y_0 \cdot S_y & 1 \end{bmatrix} \tag{13}[x1y11]=[x0y01] Sx000Sy0001 =[x0Sxy0Sy1]( 13 )
Derive the equation:
[ x 0 y 0 1 ] = [ x 1 y 1 1 ] [ 1 S x 0 0 0 1 S y 0 0 0 1 ] = [ x 1 S xy 1 S y 1 ] ( 14) \begin {bmatrix} x_0 & y_0 & 1 \end{bmatrix} = \begin{bmatrix}x_1 &y_1&1\end{bmatrix}\begin{bmatrix}\frac{1}{S_x}&0&0 \\0&\frac{1}{S_y}&0\\0&0&1\end{bmatrix}=\begin{bmatrix}\frac{x_1}{S_x}&\frac{y_1}{S_y}& 1 \ end{bmatrix} \tag{14}[x0y01]=[x1y11] Sx1000Sy10001 =[Sxx1Syy11]( 14 )
In the target image calculated directly according to the scaling formula, some mapping source coordinates may not be integers, so the corresponding pixel position cannot be found. For example, when the image is magnified 2 times, that is,S x = S y = 2 S_x = S_y = 2Sx=Sy=2 , enlarge the pixels in the image(0, 1) (0,1)(0,1 ) Corresponds to the pixel (0, 0.5) (0, 0.5)in the original image(0,0.5 ) , which is not an integer coordinate position and its grayscale value cannot be extracted.

Some kind of approximation has to be done, a simple strategy is to just use the nearest nearest integer coordinate position ( 0 , 0 ) (0,0)(0,0 ) or( 0 , 1 ) (0,1)(0,The pixel gray value at 1 ) , which is also the nearest neighbor interpolation algorithm.


image rotation

Rotation generally refers to rotating an image by a certain angle around a specified point. Rotation also usually changes the size of the image. As shown below

Insert image description here

P ( x 0 , y 0 ) P(x_0, y_0)P(x0,y0) rotates counterclockwise around the origin through an angleθ \thetaθ toP 1 ( x 1 , y 1 ) P1(x_1, y_1)P1(x1,y1),令L = ∣ OP ∣ = x 2 + y 2 L = |OP| = \sqrt{x^2 + y^2}L=OP=x2+y2 ,有: sin α = y 0 / L sin \alpha = y_0/Ls in α=y0/L c o s α = x 0 / L cos \alpha = x_0/L cosα _=x0/L

Arrive at P 1 P1P 1 ,equation:
sin ( α + θ ) = y 1 / L = cos θ sin α + sin θ cos α cos ( α + θ ) = x 1 / L = cos θ cos α − sin θ sin α ( 15) sin(\alpha + \theta) = y_1 / L = cos \theta sin \alpha + sin \theta cos \alpha \tag{15} \\ cos(\alpha + \theta) = x_1/L = cos \ theta cos \alpha - sin \theta sin \alphas in ( a+i )=y1/L=cos θ s in α+s in θ cos acos ( a+i )=x1/L=cosθcosαs in θ s in a( 15 )
Denote:
x 1 = cos θ x 0 − sin θ y 0 y 1 = cos θ y 0 + sin θ x 0 (16) x_1 = cos \theta x_0 - sin \theta y_0 \tag{16}\ \y_1 = cos \theta y_0 +sin \theta x_0x1=cosθx0sinθy0y1=cosθy0+sinθx0( 16 )
Let us derive the matrix:
[ x 1 y 1 1 ] = [ x 0 y 0 1 ] [ cos θ sin θ 0 − sin θ cos θ 0 0 0 1 ] (17) \begin{bmatrix} x_1 & 1 . y_1 & 1 \end {bmatrix} = \begin{bmatrix} x_0 & y_0 & 1 \end{bmatrix}\begin{bmatrix} cos \theta & sin \theta & 0 \\ -sin \theta & cos \theta & \\ 0&0&1\end{bmatrix}\tag{17}[x1y11]=[x0y01] cosθsinθ0sinθcosθ0001 ( 17 )
Specifies:
[ x 0 y 0 1 ] = [ x 1 y 1 1 ] [ cos θ − sin θ 0 sin θ cos θ 0 0 0 1 ] (18) \begin{bmatrix} x_0 & y_0 &1 \end{bmatrix} = \begin{bmatrix}x_1&y_1&1\end{bmatrix}\begin{bmatrix} cos\theta&-sin\theta&0\\sin\theta&cos\theta&0\ \0&0&1\end{bmatrix}\tag{18}[x0y01]=[x1y11] cosθsinθ0sinθcosθ0001 ( 18 )
The above discussion is based on the origin, which is the lower left corner of the image, as the center. How to rotate around any specified point?

Just combine the translation and rotation operations. First translate the coordinate system, then rotate around the new coordinate origin, and then translate the new origin back to the origin of the original coordinate system. This process can be summarized into the following 3 steps:

  1. Change coordinate system I to II;
  2. Rotate the point clockwise θ \thetaθ angle;
  3. Change coordinate system II back to I;

Taking rotation around the center of the image as an example, as shown in the figure below, coordinate system I has the origin of the upper left corner of the image, and xx to the right.The positive direction of the x- axis, downward isyyThe positive direction of the y- axis; while the coordinate system II takes the center of the image as the origin, andxxThe positive direction of the x- axis, downward isyyPositive y- axis direction. The conversion relationship between coordinate system I and coordinate system II is as follows:

Insert image description here

Assume that the width of the image is www , high bithhh ,denote by:
[ x II y II 1 ] = [ x II y II 1 ] [ 1 0 0 0 − 1 0 0.5 w 0.5 h 1 ] (19) \begin {bmatrix} x_in \\ y_in \\ 1 \ end{bmatrix} = \begin {bmatrix} x_in \\ y_in \\ 1 \end{bmatrix} \begin {bmatrix} 1 & 0 & 0 \\ 0 & -1 & 0 \\ 0.5w & 0.5h & 1 \ end{bmatrix}\tag{19} xy1 = xy1 100.5w010.5 h001 ( 19 )
The corresponding inverse transformation is:
[ x Ⅱ y Ⅱ 1 ] = [ x Ⅰ y Ⅰ 1 ] [ 1 0 0 0 − 1 0 − 0.5 w 0.5 h 1 ] (20) \begin{bmatrix} x_Ⅱ \\ y_Ⅱ \\ 1 \end{bmatrix} = \begin{bmatrix} x_Ⅰ \\ y_Ⅰ \\ 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & -1 & 0 \\ -0.5w & 0.5h & 1 \end{bmatrix} \tag{20} xy1 = xy1 100.5w010.5 h001 ( 20 )
Steps 1 and 3 of the above three steps have been implemented here. Together with the rotation transformation of step 2, the final transformation matrix rotated around the center point of the image is obtained. This matrix is ​​actually a cascade of three transformation matrices used in each of the three transformation steps. In the formula,W old W_{old}Wold H o l d H_{old} Hold W n e w W_{new} Wnew H n e w H_{new} Hnewrepresent the width and height of the original image and the new image respectively.

[ x 1 and 1 1 ] = [ x 0 and 0 1 ] [ 1 0 0 0 − 1 0 − 0.5 W old 0.5 H old 1 ] [ cos θ − sin θ 0 sin θ cos θ 0 0 0 − 1 0 0.5 W new 0.5 H new 1 ] = [ x 0 y 0 1 ] [ cos θ sin θ 0 − sin θ cos θ 0 0.5 ⋅ ( − W old ⋅ cos θ + H old ⋅ sin θ + W new ) 0.5 ( − W old ⋅ sin θ − H old ⋅ cos θ + H new ) 1 ] \begin{align} \begin{bmatrix} x_1 \\ y_1 \\ 1 \end{bmatrix} &= \begin{ bmatrix} x_0\\y_0\\1\end{bmatrix}\begin{bmatrix}1&0&0\\0&-1&0\\-0.5W_{old}&0.5H_{old}&1\end {bmatrix} \begin{bmatrix} cos \theta & -sin \theta & 0 \\ sin \theta & cos \theta & 0 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & -1 & 0 \\ 0.5W_{new} & 0.5 H_{new} & 1 \end{bmatrix}\nonumber\&= \begin{bmatrix}x_0\y_0\\1\end{ bmatrix} \begin{bmatrix} cos \theta & sin \theta & 0 \\ -sin \theta & cos \theta & 0 \\ 0.5 \cdot(-W_{old}\cdot cos \theta + H_{old}\ cdot sin \theta + W_{new} ) & 0.5 ( - W_{old} \cdot sin \theta - H_{old} \cdot cos \theta + H_{new}) & 1 \end{bmatrix}\tag{2 } \end{align} x1y11 = x0y01 100.5Wold010.5H _old001 cosθsinθ0sinθcosθ0001 100.5Wnew010.5H _new001 = x0y01 cosθsinθ0.5(Woldcosθ+Holdsinθ+Wnew)sinθcosθ0.5(WoldsinθHoldcosθ+Hnew)001 (21)

The inverse transformation of the above formula is:

[ x 0 y 0 1 ] = [ x 1 y 1 1 ] [ 1 0 0 0 − 1 0 − 0.5 W new 0.5 H new 1 ] [ cos θ sin θ 0 − sin θ cos θ 0 0 0 − 1 0 0.5 W old 0.5 H old 1 ] = [ x 1 y 1 1 ] [ cos θ − sin θ 0 sin θ cos θ 0 − 0.5 W new ⋅ cos θ − 0.5 H new ⋅ sin θ + W old 0.5 W new ⋅ sin θ − 0.5 H new ⋅ cos θ + 0.5 H old 1 ] \begin{align} \begin{bmatrix} x_0 \\ y_0 \\ 1 \end{bmatrix} &= \begin{bmatrix}; x_1 \\ y_1 \\ 1 \ end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & -1 & 0 \\ -0.5W_{new} & 0.5 H_{new} & 1 \end{bmatrix} } \begin {bmatrix} cos \theta & sin \theta & 0 \\ -sin \theta & cos \theta & 0 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \ \ 0 & -1 & 0 \\ 0.5W_{old} & 0.5 H_{old} & 1 \ end{bmatrix} \nonumber \\ &= \begin{bmatrix} x_1 \\y_1 \\ 1 \end{bmatrix} \begin{bmatrix} cos \theta & -sin \theta & 0 \\ sin \theta & cos \theta & 0 \\ -0.5 W_{new} \cdot cos \theta - 0.5 H_{new} \cdot sin \theta + 0.5 W_{old} & 0.5 W_{new} \cdot sin \theta - 0.5 H_{new} \cdot cos \theta + 0.5 H_{old} & 1 \end{bmatrix}\tag{22}\end{align } } x0y01 = x1y11 100.5Wnew010.5H _new001 cosθsinθ0sinθcosθ0001 100.5Wold010.5H _old001 = x1y11 cosθsinθ0.5Wnewcosθ0.5H _newsinθ+0.5Woldsinθcosθ0.5Wnewsinθ0.5H _newcosθ+0.5H _old001 (22)

According to the inverse transformation formula, the rotation transformation around the center of the image can be implemented as described in Algorithm 4.1. Similarly, the rotation transformation centered on any point can be further obtained.


interpolation algorithm

When implementing geometric operations, there are two methods: forward mapping and backward mapping.

  • The principle of the forward mapping method is to transfer the grayscale of the input image to the output image sequentially according to pixels, that is, the target image coordinates are calculated from the original image coordinates: g ( x 1 , y 1 ) = f ( a ( x 0 , y 0 ) , b ( x 0 , y 0 ) ) g(x_1, y_1) = f(a(x_0, y_0), b(x_0, y_0))g(x1,y1)=f(a(x0,y0),b(x0,y0)) . For example, operations such as image translation and mirroring can use this method.
  • The backward mapping method is the inverse of the forward mapping transformation, that is, the output pixels are mapped back to the input image in turn. If an output pixel is mapped to a pixel point that is not at the integer coordinates of the sampling grid of the input image, then its gray value It is necessary to make inferences based on the grayscale values ​​​​of integer coordinates, which is interpolation. The backward mapping method generates the output image pixel by pixel without causing computational waste. This method is often used in operations such as scaling and rotation.

Commonly used interpolation algorithms include: nearest neighbor interpolation, bilinear interpolation, and high-order interpolation.

nearest neighbor interpolation

Nearest neighbor interpolation algorithm, the value of the output pixel is the pixel value of its nearest sampling point in the input image.

Nearest neighbor interpolation can be expressed as
f ( x , y ) = g ( round ( x ) , round ( y ) ) (23) f(x,y) = g(round(x), round(y)) \tag{ twenty three}f(x,y)=g(round(x),round(y))( 23 )
The calculation of nearest neighbor interpolation is simple, and the output effect is acceptable in most cases. However, nearest neighbor interpolation can produce artifacts in the image.

bilinear interpolation

Bilinear interpolation, also known as first-order interpolation, is an application of linear interpolation extended to two dimensions and can be obtained through a series of first-order linear interpolation.

Linearity refers to the proportional and straight-line relationship between quantities. Mathematically, it can be understood as a function whose first-order derivative is a constant.

Linear interpolation refers to linearly determining the value of a point on the line connecting the two points based on the values ​​of the two points.

The value of the output pixel is the 2 × 2 2 \times 2 closest to it in the input image2×2 Weighted average of pixel gray value of sampling points in the neighborhood.

Assume that the vertex coordinates of the known unit square are f (0, 0), f (1, 0), f (0, 1), f (1, 1) f(0,0), f(1,0) , f(0,1), f(1,1)f(0,0),f(1,0),f(0,1),f(1,1 ) , as shown in the figure below, any pointf (x, y) f(x,y)f(x,y ) value.

First, linearly interpolate the two points at the upper end to obtain f (x, 0) f(x,0)f(x,0) f ( x , 0 ) = f ( 0 , 0 ) + x [ f ( 1 , 0 ) − f ( 0 , 0 ) ] f(x,0) = f(0,0) + x [f(1,0) - f(0,0)] f(x,0)=f(0,0)+x[f(1,0)f(0,0)]

Then perform linear interpolation on the two lower vertices to obtain f (x, 1) f(x,1)f(x,1) f ( x , 1 ) = f ( 0 , 1 ) + x [ f ( 1 , 1 , ) − f ( 0 , 1 ) ] f(x,1) = f(0,1) + x[f(1,1,) - f(0,1)] f(x,1)=f(0,1)+x[f(1,1,)f(0,1)]

Finally, linear interpolation is performed in the vertical direction to obtain f ( x , y ) f(x,y)f(x,y) f ( x , y ) = f ( x , 0 ) + y [ f ( x , 1 ) − f ( x , 0 ) ] f(x,y) = f(x,0) + y [f(x,1) - f(x,0)] f(x,y)=f(x,0)+y[f(x,1)f(x,0)]

Arrange to get f ( x , y ) f(x,y)f(x,y)

f ( x , y ) = [ f ( 1 , 0 ) − f ( 0 , 0 ) ] x + [ f ( 0 , 1 ) − f ( 0 , 0 ) ] y + [ f ( 1 , 1 ) + f ( 0 , 0 ) − f ( 0 , 1 ) − f ( 1 , 0 ) ] x y + f ( 0 , 0 ) (24) f(x,y) = [f(1,0) - f(0,0)] x + [f(0,1) - f(0,0)]y + [f(1,1) + f(0,0) - f(0,1) - f(1,0)] xy + f(0,0) \tag{24} f(x,y)=[f(1,0)f(0,0)]x+[f(0,1)f(0,0)]y+[f(1,1)+f(0,0)f(0,1)f(1,0)]xy+f(0,0)(24)

Insert image description here

The smoothing effect of bilinear interpolation can degrade image detail, while discontinuities in its slope can lead to undesirable transformation results.

higher order interpolation

The hot spots of bilinear interpolation can be compensated by high-order interpolation, which is usually implemented using convolution.

The value of the output pixel is the closest 4 × 4 4 \times 4 in the input image.4×The weighted average of the pixel values ​​of the sampling points in the 4 neighborhoods.

Taking cubic interpolation as an example, it uses the following cubic polynomial to approximate the theoretical optimal interpolation function sinc (x) sinc(x)sinc(x)
S ( x ) = { 1 − 2 ∣ x ∣ 2 + ∣ x ∣ 3 , 0 ≤ ∣ x ∣ < 1 4 − 8 ∣ x ∣ + 5 ∣ x ∣ 2 − ∣ x ∣ 3 , 1 ≤ ∣ x ∣ < 2 0 ∣ x ∣ ≥ 2 (25) S(x) = \left \{ \begin{matrix} \tag{25} 1 - 2 |x|^2 + |x|^3 , & 0 \le | x| < 1 \\ 4 - 8 |x| + 5 |x|^2 - |x|^3, & 1 \le |x| < 2 \\ 0 & |x| \ge 2 \end{matrix} \right. S(x)= 12∣x2+x3,48∣x+5∣x2x3,00x<11x<2x2( 25 )
In the above formula∣ x ∣ |x|x is the surrounding pixels alongxxThe distance between the x direction and the origin. Pixel to be found(x, y) (x,y)(x,The gray value of y ) is obtained by weighted interpolation of the gray values ​​of 16 points around it. The calculation formula is as follows:
f (x, y) = f (i + u, j + v) = ABC (26) f(x,y) = f(i+u, j+v) = ABC \tag{26}f(x,y)=f(i+u,j+v)=ABC(26)
其中
A = [ S ( 1 + v ) S ( v ) S ( 1 − v ) S ( 2 − v ) ] T C = [ S ( 1 + u ) S ( u ) S ( 1 − u ) S ( 2 − u ) ] B = [ f ( i − 1 , j − 1 ) f ( i − 1 , j ) f ( i − 1 , j + 1 ) f ( i − 1 , j + 2 ) f ( i , j − 1 ) f ( i , j ) f ( j , j + 1 ) f ( i , j + 2 ) f ( i + 1 , j − 1 ) f ( i + 1 , j ) f ( i + 1 , j + 1 ) f ( i + 1 , j + 2 ) f ( i + 2 , j − 1 ) f ( i + 2 , j ) f ( i + 2 , j + 1 ) f ( i + 2 , j + 2 ) ] A = \begin{bmatrix} S(1+v) \\ S(v) \\ S(1-v) \\ S(2-v) \end{bmatrix} ^T \nonumber \\ C = \begin{bmatrix} S(1+u) \\ S(u) \\ S(1-u) \\ S(2-u) \end{bmatrix} \nonumber \\ B = \begin{bmatrix} f(i-1, j-1) & f(i-1, j) & f(i-1,j+1) & f(i-1, j+2) \nonumber \\ f(i,j-1) & f(i, j) & f(j, j+1) & f(i, j+2) \nonumber \\ f(i+1, j-1) & f(i+1, j) & f(i+1, j+1) & f(i+1, j+2) \nonumber \\ f(i+2, j-1) & f(i+2, j) & f(i+2, j+1) & f(i+2, j+2) \end{bmatrix} A= S(1+v)S(v)S(1v)S(2v) TC= S(1+u)S(u)S(1u)S(2u) B= f(i1,j1)f(i,j1)f(i+1,j1)f(i+2,j1)f(i1,j)f(i,j)f(i+1,j)f(i+2,j)f(i1,j+1)f(j,j+1)f(i+1,j+1)f(i+2,j+1)f(i1,j+2)f(i,j+2)f(i+1,j+2)f(i+2,j+2)
The cubic interpolation method is usually used in raster displays, which allows scaling operations at any ratio while maintaining image details well.

To be continued~

Guess you like

Origin blog.csdn.net/sinat_41752325/article/details/132988188