In affine transformation (link, link2), all parallel lines in the original image will still be parallel in the output image. To find the transformation matrix, we need 3 points from input image and their corresponding locations in output image. Then **cv2.getAffineTransform** will create a 2×3 matrix which is to be passed to **cv2.warpAffine**. Affine transform can perform rotation, translation, resizing,

pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(img,M,(cols,rows))

For perspective transformation (see links above), you need a 3×3 transformation matrix. Straight lines will remain straight even after the transformation. To find this transformation matrix, you need 4 points on the input image and corresponding points on the output image. Among these 4 points, 3 of them should not be collinear. Then transformation matrix can be found by the function **cv2.getPerspectiveTransform**. Then apply **cv2.warpPerspective** with this 3×3 transformation matrix.

pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(300,300))

In summary,

- Affine transformation preserves lines and parallelism.
- Perspective transformation preserves lines. Affine transform is a special case of perspective transformation.
- PS, affine transformation does not preserve angle. Conformal transformation preserves angle.