Affine transform: Linear mapping preserving points, straight lines, and planes
Properties preserved:
Not preserved:
Examples: Translation, rotation, scaling, shearing
2×3 matrix format:
Transformation:
Components:
import cv2import numpy as np# define 3 points in source#ans: pts1 = np.float32([[50, 50], [200, 50], [50, 200]])# define 3 points in destination#ans: pts2 = np.float32([[10, 100], [200, 50], [100, 250]])# compute affine matrix#ans: M = cv2.getAffineTransform(pts1, pts2)#ans: M is 2x3 matrix# apply transformh, w = img.shape[:2]#ans: result = cv2.warpAffine(img, M, (w, h))
Perspective: Simulates 3D viewing angle
Properties:
Use case: Document scanning, bird's eye view, rectification
Matrix: 3×3 homography matrix
3×3 homogeneous matrix:
Need 4 point pairs to solve
# define 4 points in source (corners of document)#ans: pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])# define 4 points in destination (rectangle)#ans: pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])# compute perspective matrix#ans: M = cv2.getPerspectiveTransform(pts1, pts2)#ans: M is 3x3 homography matrix# apply transform#ans: result = cv2.warpPerspective(img, M, (300, 300))
Affine:
Perspective:
Rule: Use affine if parallelism preserved, perspective otherwise
Shear: Slants shape of image
Horizontal shear:
Vertical shear:
# horizontal shear#ans: M = np.float32([[1, 0.5, 0],#ans: [0, 1, 0]])#ans: sheared = cv2.warpAffine(img, M, (w + int(h*0.5), h))# vertical shearM = np.float32([[1, 0, 0], [0.5, 1, 0]])#ans: sheared = cv2.warpAffine(img, M, (w, h + int(w*0.5)))# combined shearM = np.float32([[1, 0.3, 0], [0.2, 1, 0]])#ans: sheared = cv2.warpAffine(img, M, (int(w*1.3), int(h*1.2)))
# what is affine transform?#ans: linear mapping preserving parallelism and ratios# affine matrix size?#ans: 2×3 matrix (6 parameters)# how many points to define affine?#ans: 3 point pairs# what is perspective transform?#ans: 3D projection, allows vanishing points# how many points for perspective?#ans: 4 point pairs
# what does affine preserve?#ans: parallelism, ratios of distances, straight lines# what does perspective NOT preserve?#ans: parallelism (parallel lines can converge)# when to use affine vs perspective?#ans: affine if parallelism kept, perspective for 3D effects# what is shearing?#ans: slanting transformation# perspective matrix size?#ans: 3×3 homography matrix (8 parameters)
# affine transform from 3 pointspts1 = np.float32([[50, 50], [200, 50], [50, 200]])pts2 = np.float32([[10, 100], [200, 50], [100, 250]])#ans: M = cv2.getAffineTransform(pts1, pts2)h, w = img.shape[:2]#ans: result = cv2.warpAffine(img, M, (w, h))
# perspective transform (document scan)pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])#ans: M = cv2.getPerspectiveTransform(pts1, pts2)#ans: result = cv2.warpPerspective(img, M, (300, 300))
# horizontal shear by 0.3#ans: M = np.float32([[1, 0.3, 0],#ans: [0, 1, 0]])h, w = img.shape[:2]#ans: sheared = cv2.warpAffine(img, M, (int(w + h*0.3), h))# combine rotation and scaling in affinecenter = (w // 2, h // 2)#ans: M = cv2.getRotationMatrix2D(center, 45, 1.5)#ans: result = cv2.warpAffine(img, M, (w, h))
Google tag (gtag.js)