DIP+CV · Programming Exercise

EP02_10 — 🎯 Perspective Correction (Homography)

2.12.10 EP02_10 🎯 Perspective Correction (Homography)

In this activity, you must implement the perspective transformation, also known as homography. Unlike affine transformations, perspective does not preserve parallelism, allowing you to “rectify” tilted objects, such as documents or signs captured at oblique angles.

  • Read two integers L and C, representing the dimensions of the original matrix.
  • Read four coordinate pairs \((x, y)\) representing the corners of the source quadrilateral (distorted object).
  • Read four coordinate pairs \((x, y)\) representing the corners of the destination quadrilateral (where the object should be mapped).
  • Read the values of the original matrix.
  • Compute the \(3 \times 3\) homography matrix and apply the transformation.
  • Print the resulting matrix with the specified output dimensions.
  • See Figure 2.21 for a simulation of this EP.

📌 Important:

  • Degrees of Freedom: The homography has 8 degrees of freedom (the ninth element of the \(3 \times 3\) matrix is a normalization constant, usually 1), requiring at least 4 corresponding points to be computed.
  • Projection: After multiplying the coordinates by the matrix, you must divide the results \(x'\) and \(y'\) by the homogeneous component \(w\) to return to the 2D plane.
  • Use of Libraries: For this task, you may use the functions cv2.getPerspectiveTransform to obtain the matrix and cv2.warpPerspective to apply the transformation, or implement the linear system and inverse mapping manually for an extra challenge.
# Output dimensions: bounding box of destination points + 1
w = int(max(pts2[:, 0])) + 1; h = int(max(pts2[:, 1])) + 1
# M = cv2.getPerspectiveTransform(pts1, pts2)
# dst = cv2.warpPerspective(img, M, (w, h))
# or
dst = mm.perspective_transform(img, pts1, pts2, size=(w, h))

2.12.10.1 🧠 Non-affine Deformation

While affine transformations map parallelograms to parallelograms, the homography maps any quadrilateral to another quadrilateral. This is essential for computer vision:

Operation Characteristic Typical Application
Homography Plane projection Document correction, plate scanning.
Vanishing Point Line convergence 3D reconstruction from 2D images.
Warping Mesh deformation Video stabilization and panoramas (stitching).

2.12.10.2 📌 Examples

Input Output Observation
4 4
0 0
3 0
0 3
3 3
0 0
3 0
0 3
3 3
10 20 30 40
50 60 70 80
90 100 110 120
130 140 150 160
10 20 30 40
50 60 70 80
90 100 110 120
130 140 150 160
The first 4 lines after the dimensions are the source points; the following 4 are the destinations. With identical points, the perspective transformation is the identity and the image is preserved.
📐 Simulator EP02_10: Perspective Correction (3×3 Homography) p' = H · p

💡 Instructions: Drag the 4 markers at the corners of the distorted quadrilateral. Click Correct Perspective to map the projected region to an aligned 300×300 pixel rectangle.

Drag the red vertices to change the perspective projection. The homography computes the 3×3 H matrix that rectifies the region.
Figure 2.21: Simulator EP02_10: Perspective Correction (3×3 Homography Transformation)
%%writefile EP02_10.py
# Python code
Overwriting EP02_10.py
TestSuite("EP02_10.py").run()
✔️ EP02_10.cases already exists in casos/
📋 5 case(s) loaded from casos/EP02_10.cases

🔍 Testing Python: EP02_10.py
⚠️ EP02_10.py: Empty file (fewer than 3 lines). Tests skipped.