Welcome to DOFBOT-Ultra repository
10. Affine Transformation
Affine Transformation
Affine Transformation (or Affine Map) is a linear transformation from 2D coordinates (x, y) to 2D coordinates (u, v). Its mathematical expression is as follows:
The corresponding homogeneous coordinate matrix representation is:

Affine transformation preserves the "straightness" (straight lines remain straight after affine transformation) and "parallelism" (the relative positional relationship between lines remains unchanged, parallel lines remain parallel after affine transformation, and the order of points on a line does not change). Three non-collinear corresponding point pairs determine a unique affine transformation. Image rotation plus scaling is the image affine transformation. Affine transformation also requires an M matrix, but since affine transformation is relatively complex, it is generally difficult to find this matrix directly. OpenCV provides a function to automatically solve for M based on the correspondence of three points before and after transformation. This function is:
M=cv2.getAffineTransform(pos1,pos2)
Where the two positions are the corresponding positional relationships before and after transformation. The output is the affine matrix M. Then use the function cv2.warpAffine().
Code path:
xxxxxxxxxx~/dofbot_opencv/scripts/2.Transform/05_Radiation.ipynb
xxxxxxxxxximport cv2import numpy as npimport matplotlib.pyplot as plt img = cv2.imread('yahboom.jpg',1)img_bgr2rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)plt.imshow(img_bgr2rgb)plt.show()# cv2.waitKey(0)xxxxxxxxxximgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]#src 3->dst 3 (top-left, bottom-left, top-right)matSrc = np.float32([[0,0], [0,height-1], [width-1,0]])matDst = np.float32([[50,50], [300,height-200], [width-300,100]])#combinationmatAffine = cv2.getAffineTransform(matSrc,matDst)# mat 1 src 2 dstdst = cv2.warpAffine(img,matAffine,(width,height))img_bgr2rgb = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)plt.imshow(img_bgr2rgb)