0% found this document useful (0 votes)
2 views19 pages

Week 4 Study Guide

This study guide covers the fundamentals of homography in computer vision, including its definition, properties, and applications such as image stitching and augmented reality. It details estimation methods like DLT and RANSAC, and compares homography with affine transformations. Additionally, it provides a workflow for image stitching and includes practical coding examples using OpenCV.

Uploaded by

tamphutranthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views19 pages

Week 4 Study Guide

This study guide covers the fundamentals of homography in computer vision, including its definition, properties, and applications such as image stitching and augmented reality. It details estimation methods like DLT and RANSAC, and compares homography with affine transformations. Additionally, it provides a workflow for image stitching and includes practical coding examples using OpenCV.

Uploaded by

tamphutranthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

COMPUTER VISION

Week 4 — Study Guide


Homography: Fundamentals, Methods & Applications
Topics: Homography Matrix | Homogeneous Coordinates | Estimation Methods (DLT, RANSAC) | Image
Stitching | AR & Object Detection

1. Summary of Main Topics


1.1 What is Homography?
A homography is a transformation that maps points from one plane to another. It is represented by
a 3×3 matrix H and operates on points expressed in homogeneous coordinates. The relationship
between a source point and its transformed destination is:

x’ = Hx

Core properties:
• H is a 3×3 matrix with 8 degrees of freedom (9 elements − 1 for scale).
• x and x’ are points in homogeneous coordinates — a 2D point (x, y) is written as [x,
y, 1].
• Requires a minimum of 4 point correspondences to solve (each pair contributes 2
equations).
• Valid when the scene is planar or the camera undergoes pure rotation.

1.2 Homogeneous Coordinates


Regular 2D Cartesian points (x, y) are extended by adding a scale factor w, giving the triplet [x, y,
w]. For image points, w = 1 by convention.

Symbol Role Dimension

x Source point (input) 3×1 vector

x’ Destination point (output) 3×1 vector

H Homography matrix 3×3 matrix

w Homogeneous scale factor Scalar (set to 1 for 2D points)


Key rule about w:
After applying H, divide the result by the output w’ to recover Cartesian coordinates:
x_out = x’/w’ and y_out = y’/w’. Points at infinity are represented when w = 0.

1.3 When to Use Homography


Homography is applicable in two scenarios:
• Planar surfaces — walls, floors, flat objects, document pages.
• Pure rotation — the camera rotates around its optical centre with no translation.

Scenario Why Homography Works

Reading a poster at an angle Flat surface → perspective correction

Aerial / satellite imagery Camera views a flat ground plane

Document scanning Paper is a planar surface

Panoramic image stitching Camera rotates around its optical centre

AR marker tracking Physical plane maps to the screen plane

1.4 RANSAC
RANSAC (Random Sample Consensus) is a robust estimation algorithm designed to fit a model in
the presence of outliers (mismatched feature points). It is the standard method for computing
homography from real-world image data.

RANSAC — 4-Step Process


1. Randomly sample the minimum subset — 4 point pairs for homography.
2. Fit a model — compute H from this subset.
3. Count inliers — points that agree with H within a pixel-distance threshold.
4. Repeat — return the H with the most inliers after N iterations.

RANSAC vs. Least Squares:


Standard least squares is sensitive to all outliers. RANSAC explicitly discards them,
making it far more robust for real-world images where feature matching is never perfect.
1.5 Affine vs. Homography
Property Affine Transform Homography

Matrix size 2×3 3×3

Degrees of freedom 6 DoF 8 DoF

Minimum point pairs 3 pairs 4 pairs

Preserves parallelism? Yes No

Models perspective? No Yes

OpenCV warp function [Link]() [Link]()

1.6 Homography Estimation Methods


Six principal methods are used to estimate H. Method choice is driven by (1) data noise level and
(2) required precision.

# Method Best For Key Characteristic

0 Direct Linear Transform Clean data / education Builds matrix A from point matches; solves
1 (DLT) Ah = 0 via SVD

0 Normalized DLT High-resolution images Centers & scales coords before DLT;
2 prevents numerical instability

0 Non-Linear Least Squares High-precision tasks Minimises reprojection error; uses


3 Levenberg–Marquardt optimizer

0 RANSAC Real-world data with Iterative, robust; ignores mismatched


4 outliers features

0 Eigenvalue / Eigenvector Theoretical study Solves Ah = 0 via eigenvectors of AᵀA


5

0 Geometric / Algebraic Specialised geometry Works directly in projective geometry


6 tasks

1.7 Image Stitching Workflow


The complete pipeline to create a panorama using homography. Images must overlap by 30–50 %
for reliable matching.

5-Step Stitching Pipeline


5. Feature Detection — Detect keypoints in both images using SIFT or ORB.
6. Feature Matching — Match descriptors; filter good matches using Lowe’s ratio test
(threshold 0.75).
7. Compute Homography — Call [Link] with RANSAC to compute H.
8. Warp Perspective — Apply H to source image using [Link].
9. Blend & Crop — Overlay images; remove black borders using boundingRect.

1.8 Applications at a Glance


Application Input Output

Image Stitching / Panorama Overlapping photos Seamless wide-angle panorama

Augmented Reality (AR) Camera frame + surface plane Virtual objects placed on real
plane

Document Scanning Tilted document photo Flat, top-down corrected scan

Planar Object Detection Reference image + scene frame Bounding box in scene frame
Image Registration Two different views Geometrically aligned images
2. Key Concepts & Definitions
2.1 Homography (H)
A projective transformation between two planes, represented as a non-singular 3×3 matrix. Applied
as x’ = Hx where all points are in homogeneous coordinates. Defined only up to scale, giving 8
degrees of freedom.

2.2 Homogeneous Coordinates


A coordinate system that extends 2D Cartesian (x, y) by appending a scale factor w to form
[x, y, w]. Image points use w = 1. Points at infinity have w = 0. They allow projective transformations
(including perspective) to be expressed as matrix multiplication.

2.3 Degrees of Freedom (DoF)


The number of independent parameters in a model. The homography matrix H has 9 entries;
dividing out the scale factor leaves 8 DoF. Each point correspondence provides 2 equations (one
for x, one for y), so at least 4 pairs are needed to uniquely determine H.

2.4 Direct Linear Transform (DLT)


A linear method that reformulates homography estimation as a homogeneous linear system Ah = 0
where A is constructed from point correspondences and h is the vectorised H. Solved via Singular
Value Decomposition (SVD) — the solution is the last column of V in A = UΣVᵀ.

2.5 Normalised DLT


An improved version of DLT that first normalises coordinates to prevent numerical instability from
large raw pixel values. Normalisation: (1) translate points so the centroid is at the origin; (2) scale
so the mean distance to the origin is √2. OpenCV and MATLAB apply this automatically even when
method = 0.

2.6 Non-Linear Least Squares


Refines an initial H by minimising the geometric reprojection error — the sum of squared Euclidean
distances between projected and actual destination points. Uses the Levenberg–Marquardt
algorithm. Implemented via [Link].least_squares (not directly in OpenCV).

2.7 RANSAC
Random Sample Consensus. A robust iterative algorithm that estimates a model (e.g., H) from data
containing outliers. It repeatedly samples a minimal subset, fits the model, and counts how many
other points agree (inliers). The best model (most inliers) is returned. Non-deterministic and
computationally heavier with high outlier ratios.

2.8 Inliers vs. Outliers


Inliers: feature matches that are consistent with the estimated homography (reprojection error below
the threshold, e.g., 3–5 px). Outliers: mismatched feature pairs that would corrupt a direct least-
squares solution. RANSAC identifies and excludes outliers.
2.9 Reprojection Error
The Euclidean distance between a projected source point H·x and the actual destination point x’.
Used as the quality metric in non-linear optimisation and as the inlier test in RANSAC.

2.10 Inverse Warping


The preferred method for applying H to an image. Instead of mapping every source pixel to the
destination (forward warping, which leaves holes), inverse warping iterates over every destination
pixel and samples the source using H⁻¹. This guarantees every output pixel is filled.

2.11 Cross-Ratio
The only geometric invariant preserved under a general projective homography. Given four collinear
points, their cross-ratio is unchanged after any projective transformation. Length, area, angles, and
parallelism are NOT preserved.
3. Formulas & Equations
3.1 Core Transformation
x' = Hx

3.2 Expanded Matrix Form


[ x' ] [ h11 h12 h13 ] [ x ]
[ y' ] = [ h21 h22 h23 ] [ y ]
[ w' ] [ h31 h32 h33 ] [ 1 ]

3.3 Converting Back to Cartesian


x_out = x' / w' y_out = y' / w'

3.4 Degrees of Freedom


DoF = 9 elements − 1 (scale) = 8
Min point pairs = ceil(8 / 2) = 4

3.5 DLT Linear System


For each point pair (x → x’), two rows are appended to A:

A · h = 0 (h = vectorised H, length 9)
Solved via SVD: A = U Σ Vᵀ
Solution h = last column of V

3.6 Reprojection Error


E(H) = Σ d( x'_i , H · x_i )²

Where d(·,·) is Euclidean distance. Levenberg–Marquardt minimises this sum.


3.7 Output Size After Convolution (no padding)
Output size = N − k + 1 (for N×N image, k×k kernel)

3.8 Identity Homography


H = I = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] → Image
unchanged
4. Code Reference (Python / OpenCV)
4.1 Key OpenCV & NumPy Functions
Function Purpose

[Link](src, dst, method) Compute 3×3 H matrix from matched keypoints

[Link](img, H, (w,h)) Apply H to warp an image

cv2.ORB_create() Create ORB feature detector / descriptor

[Link](img, None) Detect keypoints and extract descriptors

[Link]().knnMatch(d1, d2, k=2) Brute-force k-nearest-neighbour matching


[Link](thresh) Find bounding rectangle for cropping

[Link](A) Eigenvalues & eigenvectors (NumPy)

[Link].least_squares() Non-linear optimisation (Levenberg–Marquardt)

4.2 Basic Homography (DLT)


import cv2
import numpy as np

# Define 4+ corresponding point pairs


pts_src = [Link]([[141, 131], [480, 159], [493, 630], [64, 601]])
pts_dst = [Link]([[0, 0], [400, 0], [400, 300], [0, 300]])

# Compute homography matrix (DLT, method=0)


H, status = [Link](pts_src, pts_dst)

# Apply transformation to warp source image


result = [Link](img, H, (width, height))

4.3 Homography with RANSAC


H, mask = [Link](
src_pts, # Source points Nx1x2 float32
dst_pts, # Destination points
[Link], # Robust estimation method
ransacReprojThreshold=3.0 # Max reprojection error (pixels)
)

# mask: 0/1 array marking inliers (1) and outliers (0)


# method=0 -> DLT [Link] -> Least Median of Squares
4.4 Full Image Stitching Pipeline
import cv2, numpy as np

# STEP 1 -- Feature Detection


orb = cv2.ORB_create()
kp1, des1 = [Link](img1, None)
kp2, des2 = [Link](img2, None)

# STEP 2 -- Feature Matching (Lowe ratio test)


matches = [Link]().knnMatch(des1, des2, k=2)
good = [m for m, n in matches if [Link] < 0.75 * [Link]]

# STEP 3 -- Extract point arrays & estimate H with RANSAC


src_pts = np.float32([kp1[[Link]].pt for m in good]).reshape(-1,1,2)
dst_pts = np.float32([kp2[[Link]].pt for m in good]).reshape(-1,1,2)
M, mask = [Link](src_pts, dst_pts, [Link], 5.0)

# STEP 4 -- Warp source image to match destination perspective


height, width, _ = [Link]
dst = [Link](img1, M, (width, height))

# STEP 5 -- Blend & crop (remove black borders)


dst[0:[Link][0], 0:[Link][1]] = img2
gray = [Link](dst, cv2.COLOR_BGR2GRAY)
_, thresh = [Link](gray, 1, 255, cv2.THRESH_BINARY)
x, y, w, h = [Link](thresh)
panorama = dst[y:y+h, x:x+w]

4.5 Non-Linear Refinement (scipy)


from [Link] import least_squares

# 1. Get initial estimate from DLT


H_dlt, _ = [Link](src_pts, dst_pts, method=0)
h_init = H_dlt.flatten() # 9-element vector

# 2. Residual function: reprojection error per point


def compute_residuals(h, src, dst):
H = [Link]((3, 3))
src_h = [Link]([src, [Link]((len(src), 1))])
proj = (H @ src_h.T).T
proj /= proj[:, 2:3] # divide by w'
return (proj[:, :2] - dst).ravel()

# 3. Minimise using Levenberg-Marquardt


res = least_squares(compute_residuals, h_init,
method='lm', args=(src, dst))
H_opt = [Link]((3, 3))
4.6 Common Kernels (NumPy)
import numpy as np

# Sobel X (horizontal edges)


sobel_x = [Link]([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])

# Sobel Y (vertical edges)


sobel_y = [Link]([[-1, -2, -1],
[ 0, 0, 0],
[ 1, 2, 1]])

# Laplacian
laplacian = [Link]([[0, 1, 0],
[1, -4, 1],
[0, 1, 0]])

# NOTE: In practice use [Link]() and [Link]()


5. Practice Questions
Work through these questions before checking the answers. They mirror the style of MCQs seen in
the lecture slides.

Section A — Homography Fundamentals


Q1. What is the minimum number of point correspondences required to compute a unique
homography?
A. 2
B. 3
C. 4
D. 8

Answer: C. 4
Homography has 8 DoF. Each point pair contributes 2 equations, so 4 pairs provide exactly 8 — the
minimum to solve the system.

Q2. Which type of matrix is used to represent a homography?


A. 2×2
B. 3×3
C. 4×4
D. 8×8

Answer: B. 3×3
A homography acts on homogeneous coordinates using a 3×3 matrix. The full expansion maps [x, y,
1] to [x’, y’, w’].

Q3. Which of the following can be represented by a homography?


A. Rotation only
B. Translation only
C. Perspective transformation only
D. All of the above

Answer: D. All of the above


Homography is a general projective transformation that encompasses rotation, translation, scaling,
shear, and perspective distortion.

Q4. In OpenCV, which function computes the homography matrix from point
correspondences?
A. [Link]()
B. [Link]()
C. [Link]()
D. [Link]()

Answer: B. [Link]()
[Link]() accepts matched source and destination point arrays and returns the 3×3 H
matrix alongside an inlier mask.

Q5. Homography can accurately relate images only if the scene is:
A. Three-dimensional
B. Planar
C. Spherical
D. Cylindrical

Answer: B. Planar
Homography mathematically relates perspective views of a flat plane. It is not valid for general 3D
scenes with depth variation.

Section B — Estimation Methods


Q6. What algorithm is most commonly used to robustly estimate homography in the
presence of outliers?
A. Dijkstra
B. RANSAC
C. K-means
D. Levenberg–Marquardt

Answer: B. RANSAC
RANSAC iteratively samples random point subsets, fits H, and counts inliers. It returns the best H
while discarding outlier matches.

Q7. What is the main advantage of Normalised DLT over basic DLT?
A. Faster computation
B. Reduces numerical error
C. Requires fewer points
D. Preserves image colour

Answer: B. Reduces numerical error


Normalising coordinates prevents large raw pixel values (e.g., 1920, 1080) from causing numerical
instability in the linear system.

Q8. What is the goal in non-linear least squares homography refinement?


A. Minimise algebraic error
B. Minimise pixel value differences
C. Minimise geometric reprojection error
D. Maximise number of matched points

Answer: C. Minimise geometric reprojection error


Reprojection error measures the actual geometric distance between projected and true destination
points — a physically meaningful metric.

Q9. Which method is NOT typically used for refining a homography estimate?
A. RANSAC
B. SVD
C. Gradient Descent
D. Levenberg–Marquardt

Answer: B. SVD
SVD is used in the initial DLT estimation step (to solve Ah = 0), not in non-linear refinement.
Refinement methods minimise reprojection error iteratively.

Q10. In the DLT algorithm, which mathematical tool solves the linear system Ah = 0?
A. Fourier Transform
B. Singular Value Decomposition (SVD)
C. Convolution
D. Gradient Descent

Answer: B. Singular Value Decomposition (SVD)


SVD decomposes A = UΣVᵀ. The solution h is the last column of V (eigenvector corresponding to
the smallest singular value of A).

Section C — Properties & Applications


Q11. Which function would you use in OpenCV to warp an image using a homography?
A. [Link]()
B. [Link]()
C. [Link]()
D. [Link]()

Answer: B. [Link]()
[Link]() uses a 2×3 matrix for affine transformations only. [Link]() uses the
full 3×3 homography matrix.

Q12. Does a homography transformation preserve parallel lines?


A. Yes, always
B. No, not necessarily
C. Only for affine transforms
D. Only for rigid transforms
Answer: B. No, not necessarily
Perspective transformations — a type of homography — can cause parallel lines to converge at a
vanishing point. Parallelism is not preserved.

Q13. Which geometric property IS preserved under a general projective homography?


A. Length
B. Area
C. Cross-ratio of four collinear points
D. Angles

Answer: C. Cross-ratio of four collinear points


While length, area, angles, and parallelism can all change, the cross-ratio of four collinear points is
invariant under projective transformation.

Q14. What do you obtain if you project a square through a homography transformation?
A. A square
B. A rectangle
C. Any quadrilateral
D. A parallelogram

Answer: C. Any quadrilateral


Homography preserves straight lines but not angles or parallelism. Any four-sided polygon is a
possible result.

Q15. What is the primary application of homography in creating panoramas?


A. Colour correction
B. Image stitching / alignment
C. Noise reduction
D. Edge detection

Answer: B. Image stitching / alignment


Homography aligns overlapping images into a common coordinate system, enabling seamless
panoramic stitching.

Q16. If the Homography matrix H is the identity matrix, what happens to the image?
A. Rotated 90°
B. Scaled by 2
C. Unchanged
D. Inverted

Answer: C. Unchanged
The identity matrix maps every input coordinate to the exact same output coordinate, so the image
is completely unchanged.

Q17. Which coordinate system is required to multiply a 2D image point by the 3×3
homography matrix?
A. Cartesian coordinates
B. Polar coordinates
C. Homogeneous coordinates
D. Cylindrical coordinates

Answer: C. Homogeneous coordinates


2D points (x, y) must be augmented to (x, y, 1) — homogeneous form — to be multiplied by the 3×3
H matrix.

Q18. Why is inverse warping generally preferred over forward warping when applying a
homography?
A. It is faster
B. It avoids holes / gaps in the output image
C. It requires less memory
D. It works without a matrix

Answer: B. It avoids holes / gaps in the output image


Inverse warping iterates over each destination pixel and samples the source via H⁻¹, guaranteeing
every output pixel has a value.

Q19. How many degrees of freedom does a 3×3 homography matrix have?
A. 9
B. 8
C. 6
D. 4

Answer: B. 8
The 3×3 matrix has 9 entries, but it is defined only up to a scale factor (multiplying H by any scalar
leaves x’ = Hx unchanged), reducing DoF to 8.

Q20. If two images are related by pure camera rotation (no translation), are they related by a
homography?
A. Yes, always
B. No, never
C. Only if the scene is planar
D. Only if focal length is infinite

Answer: A. Yes, always


Pure rotation around the camera’s optical centre induces a homography regardless of scene depth.
This is why panoramic stitching works even for non-planar scenes.
6. Quick Reference Cheat Sheet
Key Numbers
Fact Value / Detail

Homography matrix size 3×3

Degrees of freedom 8 DoF

Minimum point correspondences 4 pairs

Equations per point pair 2 (one for x, one for y)

Affine transform DoF 6 DoF (3 pairs needed)


Core formula x’ = Hx (homogeneous)

OpenCV DLT flag method=0

OpenCV RANSAC flag [Link]

Recommended image overlap (stitching) 30–50 %

What Homography Preserves / Does Not Preserve


Preserved ✓ NOT Preserved ✗

Straight lines (collinearity) Length / distances

Cross-ratio of four collinear points Angles

Incidence (point on a line stays on the line) Area

— Parallelism

Common Pitfalls
Watch out for:
• Fewer than 4 point pairs — under-constrained system; H cannot be uniquely
solved.
• Collinear points — degenerate case; H is undefined even with 4+ pairs.
• High outlier ratio — RANSAC needs more iterations, increasing compute time.
• Skipping normalisation — raw high-res pixel values cause numerical instability in
DLT.
• Using [Link] instead of [Link] — warpAffine only
accepts a 2×3 matrix and will fail with H.
• Forward warping — leaves holes in the output; always prefer inverse warping.

You might also like