Affine transformation, also known as affine mapping, is the transformation of a vector space into another vector space by a linear transformation followed by a translation in geometry. Affine transformation can maintain the “flatness” of the image, including rotation, scaling, translation, and tangent operation. Generally speaking, if the affine transformation matrix is 23, the elements in the third column play the role of translation, the first two columns are scaled on the diagonal, and the rest are rotated or miscut. Affine transformation is a linear transformation from two dimensional coordinates (x, y) to two dimensional coordinates (u, v). The mathematical expression is as follows:
The corresponding homogeneous coordinate system is as follows
Affine transformation preserves the “flatness” and “parallelism” of a two-dimensional graph (a straight line is still a straight line after affine transformation) (the relative position relation between lines remains unchanged, parallel lines are still parallel after affine transformation, and the position order of points on the line does not change). The three noncollinear pairs of corresponding points determine a unique affine transformation.
API
Compute the rotation matrix
publicstatic Mat getRotationMatrix2D(Point center, double angle, double scale) Copy the code
Parameter 1: Center, the central position of image rotation
Parameter 2: Angle, the Angle of image rotation, in degrees, and the positive value is counterclockwise rotation
Parameter 3: scale, the scale factor of two axes, can realize image scaling in the rotation process, without scaling input 1
The generated rotation matrix is related to rotation Angle and rotation center.
For example, if scale is 2 and Angle is 90 degrees, m1 is the rotation matrix
Calculate affine matrix (three points)
publicstatic Mat getAffineTransform(MatOfPoint2f src, MatOfPoint2f dst) Copy the code
Parameter 1: SRC, three pixel coordinates in the original image
Parameter two: DST, three pixel coordinates in the target image
Return value: 2*3 transformation matrix. Perspective transformation is a 3 by 3 matrix, affine is a 2 by 3 matrix
Affine transformation
publicstaticvoidwarpAffine(Mat src, Mat dst, Mat M, Size dsize, int flags, int borderMode, Scalar borderValue) Copy the code
Parameter 1: SRC, original image
Parameter 2: DST, output image after perspective transformation, same data type as SRC, but same size as Dsize