Affine transformation, an affine mapping also known, refers to the geometry, a vector space once connected to a linear transformation and translation into another vector space. Affine transformation requires a matrix M, but due to the affine transformation is complicated and difficult to find directly to the matrix, opencv provides functions to automatically solve the M according to correspondence between the three points before and after the transformation, this function is:
M=cv2.GetAffineTransform(src, dst)
- Coordinates of the three points of the original image: src
- dst: three points corresponding to the transformed coordinates
- M: The three corresponding points obtained affine transformation matrix
and then using the function cv2.warpAffine () M obtained using the original image is converted to
from matplotlib import pyplot as plt
import cv2
import numpy as np
img = cv2.imread('aier.jpg')
rows,cols = img.shape[:2]
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,20],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)
#第三个参数:变换后的图像大小
res = cv2.warpAffine(img,M,(rows,cols))
plt.subplot(121)
plt.imshow(img[:,:,::-1])
plt.subplot(122)
plt.imshow(res[:,:,::-1])
plt.show()