Geometric Transforms

Affine Transformations

Affine transform: Linear mapping preserving points, straight lines, and planes

Properties preserved:

  • Parallelism (parallel lines stay parallel)
  • Ratios of distances along lines
  • Barycenters of weighted points

Not preserved:

  • Angles
  • Distances (except along parallel lines)

Examples: Translation, rotation, scaling, shearing

Affine Transform Matrix

2×3 matrix format:

Transformation:

Components:

  • [a b; c d]: Linear transformation (rotation, scale, shear)

Affine Transform in OpenCV

  1. import cv2
  2. import numpy as np
  3. # define 3 points in source
  4. #ans: pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
  5. # define 3 points in destination
  6. #ans: pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
  7. # compute affine matrix
  8. #ans: M = cv2.getAffineTransform(pts1, pts2)
  9. #ans: M is 2x3 matrix
  10. # apply transform
  11. h, w = img.shape[:2]
  12. #ans: result = cv2.warpAffine(img, M, (w, h))

Perspective Transform

Perspective: Simulates 3D viewing angle

Properties:

  • Straight lines remain straight
  • Parallel lines may converge (vanishing point)
  • Preserves collinearity

Use case: Document scanning, bird's eye view, rectification

Matrix: 3×3 homography matrix

Perspective Transform Matrix

3×3 homogeneous matrix:

Transformation:

Need 4 point pairs to solve

Perspective Transform in OpenCV

  1. # define 4 points in source (corners of document)
  2. #ans: pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
  3. # define 4 points in destination (rectangle)
  4. #ans: pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
  5. # compute perspective matrix
  6. #ans: M = cv2.getPerspectiveTransform(pts1, pts2)
  7. #ans: M is 3x3 homography matrix
  8. # apply transform
  9. #ans: result = cv2.warpPerspective(img, M, (300, 300))

Affine vs Perspective

Affine:

  • 2×3 matrix (6 parameters)
  • Need 3 point pairs
  • Preserves parallelism
  • Faster to compute

Perspective:

  • 3×3 matrix (8 parameters)
  • Need 4 point pairs
  • Allows vanishing points
  • More general

Rule: Use affine if parallelism preserved, perspective otherwise

Shearing

Shear: Slants shape of image

Horizontal shear:

Vertical shear:

Shearing in OpenCV

  1. # horizontal shear
  2. #ans: M = np.float32([[1, 0.5, 0],
  3. #ans: [0, 1, 0]])
  4. #ans: sheared = cv2.warpAffine(img, M, (w + int(h*0.5), h))
  5. # vertical shear
  6. M = np.float32([[1, 0, 0],
  7. [0.5, 1, 0]])
  8. #ans: sheared = cv2.warpAffine(img, M, (w, h + int(w*0.5)))
  9. # combined shear
  10. M = np.float32([[1, 0.3, 0],
  11. [0.2, 1, 0]])
  12. #ans: sheared = cv2.warpAffine(img, M, (int(w*1.3), int(h*1.2)))

Exercises - Part 1 (Concepts)

  1. # what is affine transform?
  2. #ans: linear mapping preserving parallelism and ratios
  3. # affine matrix size?
  4. #ans: 2×3 matrix (6 parameters)
  5. # how many points to define affine?
  6. #ans: 3 point pairs
  7. # what is perspective transform?
  8. #ans: 3D projection, allows vanishing points
  9. # how many points for perspective?
  10. #ans: 4 point pairs

Exercises - Part 2 (Concepts)

  1. # what does affine preserve?
  2. #ans: parallelism, ratios of distances, straight lines
  3. # what does perspective NOT preserve?
  4. #ans: parallelism (parallel lines can converge)
  5. # when to use affine vs perspective?
  6. #ans: affine if parallelism kept, perspective for 3D effects
  7. # what is shearing?
  8. #ans: slanting transformation
  9. # perspective matrix size?
  10. #ans: 3×3 homography matrix (8 parameters)

Exercises - Part 3 (Coding)

  1. # affine transform from 3 points
  2. pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
  3. pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
  4. #ans: M = cv2.getAffineTransform(pts1, pts2)
  5. h, w = img.shape[:2]
  6. #ans: result = cv2.warpAffine(img, M, (w, h))

Exercises - Part 4 (Coding)

  1. # perspective transform (document scan)
  2. pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
  3. pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
  4. #ans: M = cv2.getPerspectiveTransform(pts1, pts2)
  5. #ans: result = cv2.warpPerspective(img, M, (300, 300))

Exercises - Part 5 (Mixed)

  1. # horizontal shear by 0.3
  2. #ans: M = np.float32([[1, 0.3, 0],
  3. #ans: [0, 1, 0]])
  4. h, w = img.shape[:2]
  5. #ans: sheared = cv2.warpAffine(img, M, (int(w + h*0.3), h))
  6. # combine rotation and scaling in affine
  7. center = (w // 2, h // 2)
  8. #ans: M = cv2.getRotationMatrix2D(center, 45, 1.5)
  9. #ans: result = cv2.warpAffine(img, M, (w, h))

Google tag (gtag.js)