Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.


1. Theoretical Basis

Consider the transformation shown in Figure 1, where point P is mapped to point Q by an operation map f. Now let’s explain if we now know the coordinates of P and Q, how do we solve for the transformation f

Intuitively, write the algebraic equation:


{ x =   a 1 x + a 2 y + t x y =   b 1 x + b 2 y + t y \left\{ \begin{aligned} x’ & = \ a_1 x+a_2 y+t_x\\ y ‘& = \ b_1 x+b_2 y+t_y\\ \end{aligned} \right.

Write it as a matrix


[ x y 1 ] = [ a 1 a 2 t x b 1 b 2 t y 0 0 1 ] [ x y 1 ] \begin{bmatrix} x’\\y’\\1\end{bmatrix}=\begin{bmatrix} a_1 & a_2 &t_x\\ b_1 & b_2 &t_y\\0&0&1 \end{bmatrix}\begin{bmatrix} x\\y\\1\end{bmatrix}

The reason for the change in 2d coordinates using a 3d matrix is to add a dimension of the offset T, otherwise T cannot be represented in the mapping f. To understand the third dimension better, contrast it with linear transformations. The characteristics of linear transformation are:

① Before and after the transformation, the line is still a line

② Before and after transformation, the position of origin remains unchanged.

The transformation matrix of linear transformation is given:


[ x y ] = [ a 1 a 2 b 1 b 2 ] [ x y ] \begin{bmatrix} x’\\y’\\ \end{bmatrix}=\begin{bmatrix} a_1 & a_2 \\ b_1 & b_2 \\\end{bmatrix}\begin{bmatrix} x\\y\\\end{bmatrix}

I’m going to plug in 0,0 and obviously the origin doesn’t change. By substituting I ⃗\vec ii =(1,0) and j⃗\vec jj =(0,1), a new basis completely determined by the transformation matrix can be obtained. Therefore, the linear transformation is to transform the two coordinate axes (the basis), leaving the origin position unchanged, to obtain a new coordinate axis after transformation. All points of the original coordinate system are mapped to the corresponding points of the new coordinate system by this transformation matrix, and the transformation process after the addition of the third-dimensional offset may shift the origin of coordinates, so it may cover more transformations. Figure 3 summarizes the possible situation of the three-dimensional transformation.

2 Algorithm Implementation

In OpenCV, you can operate with cv2.warpaffine (). The parameter Matrix is the transformation Matrix, but the transformation Matrix provided by the user to Cv2.warpaffine () is only a 2×3 Matrix, that is, the Matrix containing the following parameters: [a1a2tXB1b2ty] begin{bmatrix} a_1&a_2&t_x \\ b_1&b_2&t_y \end{bmatrix}[a1b1a2b2txty] Because the third row of all transformation matrices is determined, this method can be added automatically.

Here is the test:

import cv2
import numpy as np

img = cv2.imread(r'C:\Users\91398\Desktop\test.jpg'.1)
imgHeight = img.shape[0]
imgWidth = img.shape[1]
mat = np.float32([[1.0.0], [0.1.0]])
dstImg = cv2.warpAffine(img,mat,(imgWidth,imgHeight))
cv2.imshow('src.jpg',dstImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

This code snippet gives the original image as shown in Figure 3(I), which can be seen from the coefficients of the 2×3 transformation matrix. Now turn it counterclockwise by 90° and modify the mat parameter. As shown in Figure 3(ii), it can be found that it is completely black. According to the theory of the algorithm, this is because the whole picture has been rotated outside the canvas. Add a little y component to the transformation matrix of 90° rotation, that is, shift the Y-axis by a distance after rotation, and you can see the image appears, which also proves that the previous black is because the image appears outside the canvas.



As can be seen from the above operation, although passing a transformation matrix to cv2.warpaffine () can indeed achieve all kinds of image transformation, but each operation may have to carry out horizontal or vertical axis translation, which is very inconvenient, so some new methods are introduced on this basis:

cv2.getRotationMatrix2D((x_center,y_center),theta,scaling=1)
Copy the code

This method can obtain a custom rotation center transformation matrix, without rotation + translation operation

cv2.getAffineTransform(pos1,pos2)
Copy the code

This method can get a transformation matrix of unknown relationship, because a large part of the transformation does not explicitly use figure 3 to implement some sort of transformation, but we already know before and after the transformation of a three point, you can according to this method and the transformation matrix, and applied to the whole image, make the whole image to realize the so-called transformation of the three groups. The three groups of points are emphasized here because the two-dimensional plane transformation matrix [a1a2tXB1b2ty]\begin{bmatrix} a_1&a_2&t_x \\ b_1&b_2&t_y \end{bmatrix}[a1b1a2b2txty] has six unknown parameters, One set of points can produce two equations, so three sets of points are needed. It can be generalized that four groups of points are needed to solve the matrix in a three-dimensional plane transformation.

3 summary

The key of image affine transformation is the solution of transformation matrix. For simple translation, rotation, etc., you can manually pass in a transformation matrix of nP. float32 format. Also can use OpenCV custom center method to achieve. For complex transformations with unknown relationships, three groups of points can be found, and the transformation matrix can be solved by using cv2.getaffineTransform (), and then passed in cv2.warpaffine ().


Computer vision basics tutorial outline

The chapter content

Color space and digital imaging

Fundamentals of computer geometry

2. Image enhancement, filtering, pyramid

3. Image feature extraction

4. Image feature description

5. Image feature matching

6 Stereovision

7 Actual Project

Welcome to my AI channel “AI Technology Club”.