Invariant features & image alignment
This page shows how to find the same physical spot in two photos. It then shows how to compute the geometric transformation (translation, rotation, affine, homography) that aligns the photos. The page covers the feature pipeline, the matching metrics (Euclidean, SSD, SAD, NCC, Hamming) and the ratio test. It also covers the full 2D transformation hierarchy with matrices and degrees of freedom.
Feature matching workflow
Image correspondence means finding points or features in two images that show the same physical location or scene element. An example is the same corner of a building seen from two viewpoints. The standard pipeline has three stages:
- Feature detection: find the interesting points (keypoints).
- Feature description: represent each point with a vector that summarises the local patch.
- Feature matching: find which feature in image 1 matches which feature in image 2. Compare the descriptors with Euclidean distance, SSD, NCC, or Hamming distance.
Compare the pipeline to finding a friend in a crowd photo. Detection = spotting the faces. Description = noting the features of each face (glasses, beard, hair colour). Matching = comparing your notes between two photos.
The simplest idea is to scan with a template and compute the SSD or correlation of raw pixel intensities. To make candidate matches, take the patches with the most similar appearance (lowest SSD).
Patch matching and descriptors
Patch matching compares small image regions around a point of interest. A patch is a small window of pixel intensities. Compare a patch from image 1 with the candidate patches in image 2 with a matching metric. Select the best candidate.
To compare patches, convert each patch into a descriptor. Flatten the patch row by row:
Slide example (flattening). Patch 1 and patch 2:
| 10 | 20 | 30 |
| 20 | 40 | 50 |
| 30 | 50 | 60 |
| 12 | 22 | 32 |
| 22 | 42 | 52 |
| 32 | 52 | 62 |
$D_1=[10,20,30,20,40,50,30,50,60]$ and $D_2=[12,22,32,22,42,52,32,52,62]$.
Extension (our own). Every entry of $D_2$ is the entry of $D_1$ $+2$. Euclidean distance $=\sqrt{9\times 2^2}=6$; SSD $=9\times4=36$; SAD $=9\times2=18$. But NCC $=1$ exactly, because $D_2$ is a brightness-shifted copy of $D_1$ (see NCC below).
Keypoints and descriptors
Keypoints are distinctive points that we can identify across different images. A keypoint gives only a location $(x,y)$, which is not enough for matching. The descriptor is a numerical representation of the local appearance around the keypoint:
Keypoint $\rightarrow$ local neighbourhood (patch) $\rightarrow$ descriptor. Similar features must produce similar descriptors.
- Detectors from the slides: Harris corner detector (finds corners) and SIFT (finds distinctive keypoints across different scales).
- Descriptors: Census, HOG, ORB, BRIEF, BRISK, FREAK, SIFT.
- Distance functions to compare descriptors: Euclidean, SSD, SAD, NCC, Hamming (for binary descriptors).
The detector says where. The descriptor says what it looks like. The distance function says how alike two descriptors are. Exam question: "differentiate detection, description and matching".
Harris corners and SIFT in a nutshell (standard background)
Why corners? A flat region looks the same when you slide a window in any direction, so it is not useful. An edge looks the same when you slide along the edge, so it is ambiguous. A corner changes in every direction, so we can localise it exactly.
$I_x,I_y$: image gradients; $W$: window with weights $w$; $k\approx0.04$ to $0.06$. $R$ large positive: corner; $R$ large negative: edge; $|R|$ small: flat. Equivalent: both eigenvalues of $M$ large means corner; one large means edge.
Practice. $k=0.05$. (a) $M=\begin{pmatrix}10&2\\2&6\end{pmatrix}$: $\det=60-4=56$, $\operatorname{tr}=16$, $R=56-0.05\times256=43.2>0$, a corner. (b) $M=\begin{pmatrix}100&0\\0&1\end{pmatrix}$: $\det=100$, $\operatorname{tr}=101$, $R=100-0.05\times10201=-410<0$, an edge.
SIFT (Scale-Invariant Feature Transform) has three steps:
- Find the extrema of the Difference of Gaussians across scale space. This gives keypoints with a location and a scale.
- Assign a dominant orientation from a gradient histogram (rotation invariance).
- Build a descriptor from gradient-orientation histograms in a $4\times4$ grid of cells with 8 bins each. This gives a $4\times4\times8=128$-dimensional vector. Normalise the vector for robustness to illumination.
Compare SIFT descriptors with Euclidean distance.
Harris is rotation invariant but not scale invariant. SIFT is invariant to scale and rotation. It is robust to moderate illumination and viewpoint change.
Brute-force matching and nearest neighbour
The naive strategy assumes that the detected points and their descriptors are known. Compare each feature in $I_1$ with all features in $I_2$ ($N^2$ comparisons for $N$ features per image). Pick the pair with the minimum distance, that is, the closest descriptor.
$D_1$: descriptor from image 1, $D_j$: candidates in image 2, $d$: distance (Euclidean for SIFT). This is nearest-neighbour (NN) matching.
Euclidean distance
Euclidean distance is the straight-line distance between two descriptor vectors. A smaller distance means more similar. The pair with the smallest distance is the closest match.
$n$ is the descriptor length; $D_{1i}$, $D_{2i}$ are the $i$-th entries.
Slide example. Compare $(2,3,5)$ with $(3,4,5)$: $D=\sqrt{(2-3)^2+(3-4)^2+(5-5)^2}=\sqrt{1+1+0}=\sqrt2=1.414$.
Slide example (selecting the best candidate). Query descriptor $Q=(10,20,30)$. Candidates (from the worked distances on the slides): $C_1=(11,21,30)$, $C_2=(14,24,33)$, $C_3=(9,20,31)$.
Step 1: $d(Q,C_1)=\sqrt{(10-11)^2+(20-21)^2+(30-30)^2}=\sqrt{1+1+0}=\sqrt2\approx1.414$.
Step 2: $d(Q,C_2)=\sqrt{(10-14)^2+(20-24)^2+(30-33)^2}=\sqrt{16+16+9}=\sqrt{41}\approx6.403$.
Step 3: $d(Q,C_3)=\sqrt{(10-9)^2+(20-20)^2+(30-31)^2}=\sqrt{1+0+1}=\sqrt2\approx1.414$.
| Candidate | Descriptor | Euclidean distance | Match |
|---|---|---|---|
| $C_1$ | (11,21,30) | 1.414 | Close |
| $C_2$ | (14,24,33) | 6.403 | Far |
| $C_3$ | (9,20,31) | 1.414 | Close |
Nearest neighbour is ambiguous here: $C_1$ and $C_3$ tie at the same minimum. Euclidean distance alone cannot separate them. The ratio test solves this problem.
Ratio test
The ratio test rejects ambiguous matches. Sometimes the closest candidate is almost as close as the second-closest. Then the match is unreliable, because the descriptor is not distinctive. A good match must be much closer than the runner-up.
$d_1$: distance to the nearest neighbour, $d_2$: distance to the second-nearest. Common threshold $0.8$: if $r\lt0.8$ accept; if $r\ge0.8$ reject as ambiguous.
Slide example 1. Sorted distances: 1st nearest $C_1$ (1.414), 2nd nearest $C_3$ (1.414), 3rd $C_2$ (6.403). $r=1.414/1.414=1$. Since $1>0.8$, reject the match. $C_1$ and $C_3$ are the closest, but they are equally close to the query. The ratio test shows this ambiguity.
Slide example 2. Distances $1.414,\ 6.403,\ 8.660$ ($8.660=\sqrt{75}$). Here $d_1=1.414$, $d_2=6.403$, so $r=1.414/6.403=0.221$. Since $0.221\lt0.8$, accept. The nearest neighbour is much closer than the next best, so the match is distinctive.
Practice. Distances $5.0$ and $5.5$: $r=0.909\ge0.8$, reject. Distances $2.0$ and $4.0$: $r=0.5$, accept.
Lowe's SIFT paper used $0.8$. It reported that this threshold removes about 90% of false matches and discards only about 5% of correct ones. A lower threshold is stricter: it gives fewer but more reliable matches.
Patch similarity metrics: SSD, SAD, NCC
In the slides, $H$ and $F$ denote the left and right patches (or descriptors). The pair $(u,v)$ indexes the pixels in a $(2k+1)\times(2k+1)$ window. The metrics apply to 2D patches or 1D descriptors.
Sum of squared differences (SSD)
Always $\ge0$; exactly $0$ only if $H$ and $F$ are identical. Lower SSD means a better match.
Advantages: it is simple and computationally efficient. Limitation: it is sensitive to changes in brightness and contrast.
Sum of absolute differences (SAD)
Always $\ge0$; $0$ only if identical. Cheaper than SSD (no multiplications) and less dominated by single large errors.
Normalized cross correlation (NCC)
$\bar H,\bar F$: mean intensity of each patch. Ranges between $-1$ and $+1$; exactly $1$ if identical (or one is a scaled/shifted copy of the other). Close to $+1$: strong positive similarity; close to $0$: weak linear correlation; close to $-1$: strong negative correlation. Higher NCC means a better match.
Advantage: NCC is more robust than SSD to additive brightness changes, because it subtracts the mean. The normalisation also handles contrast scaling. Limitation: viewpoint, rotation, scale and large appearance changes still affect it.
| SSD | SAD | NCC | |
|---|---|---|---|
| Best match when | smallest | smallest | largest (near $+1$) |
| Range | $[0,\infty)$ | $[0,\infty)$ | $[-1,1]$ |
| Brightness offset | sensitive | sensitive | robust |
| Cost | low | lowest | higher (means, normalisation) |
Disadvantage of patch-based methods (slide): if the warping (rotation, scale, viewpoint) is not estimated accurately, even very small errors change the matching score a lot. Two patches of the same object that are slightly rotated or scaled give a large SSD. Use invariant descriptors such as SIFT to avoid this problem.
Worked example: SSD, SAD and NCC on the same data
Query patch $Q$ and three candidates (all slide data):
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 10 |
$C_1$
| 2 | 3 | 4 |
| 5 | 6 | 7 |
| 8 | 9 | 10 |
$C_2$
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 12 |
$C_3$
SAD. $C_1$: eight zeros and $|9-10|=1$, so SAD $=1$. $C_2$: all 9 pixels differ by 1, so SAD $=9$. $C_3$: eight zeros and $|9-12|=3$, so SAD $=3$. The smallest SAD is $C_1$: best match.
SSD. $C_1$: $1^2=1$. $C_2$: $9\times1^2=9$. $C_3$: $3^2=9$. The smallest SSD is $C_1$ ($=1$): best match. SSD gives a tie between $C_2$ and $C_3$ at 9, but SAD separates them (9 vs 3). SSD punishes the single large error more.
NCC. $\bar Q=5$, and $\sum(Q_i-\bar Q)^2=16+9+4+1+0+1+4+9+16=60$.
$C_2$: mean $=6$; the deviations equal those of $Q$ exactly, so numerator $=60$, denominator $=\sqrt{60}\sqrt{60}=60$, NCC $=\mathbf{1.000}$.
$C_1$: entries $1,2,3,4,5,6,7,8,10$, mean $=46/9=5.111$. Numerator $=\sum(Q_i-5)(C_{1i}-5.111)=\sum(Q_i-5)C_{1i}=-4-6-6-4+0+6+14+24+40=64$. Denominator term $\sum(C_{1i}-5.111)^2=304-9(5.111)^2=68.889$. NCC $=64/\sqrt{60\times68.889}=64/64.29=\mathbf{0.995}$.
$C_3$: entries $1,\dots,8,12$, mean $=48/9=5.333$. Numerator $=-4-6-6-4+0+6+14+24+48=72$. $\sum(C_{3i}-5.333)^2=348-9(5.333)^2=92$. NCC $=72/\sqrt{60\times92}=72/74.30=\mathbf{0.969}$.
| Candidate | SAD | SSD | NCC |
|---|---|---|---|
| $C_1$ | 1 | 1 | 0.995 |
| $C_2$ | 9 | 9 | 1.000 |
| $C_3$ | 3 | 9 | 0.969 |
Conclusion. SAD and SSD pick $C_1$. NCC picks $C_2$, because $C_2$ is $Q$ plus 1 (a pure brightness shift), which NCC ignores. Higher NCC is better. NCC measures the similarity of the pattern of intensity variation.
Note: the slide's per-candidate table for $C_1$ shows numerator 64 and denominator 68.89. Its final summary table lists 60 and 60.889 (NCC 0.993). The correct figure from the full calculation is $0.995$. The ranking is the same.
Binary descriptors and Hamming distance
Binary descriptors (BRIEF, ORB, BRISK, FREAK, Census) store each feature as a string of bits. Each bit is the result of a simple intensity comparison. The descriptors are tiny and very fast to compare.
$\oplus$ is XOR (1 when the bits differ). So the Hamming distance is the number of bit positions at which two descriptors differ. To get it, do an XOR, then count the 1s. $d_H=0$ means identical; smaller means more similar (better match); larger means less similar.
Slide example. Query $10110110$ against three candidates:
| Candidate | Query | Candidate bits | Differing positions | Hamming distance |
|---|---|---|---|---|
| A | 10110110 | 10100110 | 4th bit | 1 |
| B | 10110110 | 11110010 | 2nd, 6th bits | 2 |
| C | 10110110 | 10011100 | 3rd, 5th, 7th bits | 3 |
Best match: A (distance 1). Check B via XOR: $10110110\oplus11110010=01000100$, which has two 1s, so $d_H=2$. Check C: $10110110\oplus10011100=00101010$, three 1s, so $d_H=3$.
Note: the printed slide lists $3$ and $4$ for the last two rows. A recount of the bits gives $2$ and $3$. The ranking A < B < C is the same.
Match the metric to the descriptor type. SIFT (float vector) uses Euclidean distance. ORB/BRIEF (binary) use Hamming distance. Raw intensity patches use SSD/SAD/NCC.
Image transformation and alignment
A grayscale image is a 2D function $f(\mathbf x)$. Here $\mathbf x=(x,y)^T$ is the spatial position, $f(\mathbf x)$ is the intensity at that position, and the domain $D$ is the set of all valid positions.
There are two kinds of image transformation:
| Filtering | Warping | |
|---|---|---|
| Formula | $G(\mathbf x)=h\{F(\mathbf x)\}$ | $G(\mathbf x)=F(h\{\mathbf x\})$ |
| What changes | the range (pixel values) | the domain (pixel positions) |
| Example | blur, contrast, colour change | rotate, scale, shear, perspective |
A geometric transformation changes the position, orientation or shape of pixels with a mathematical rule. The rules are rotation, translation, uniform scaling, non-uniform scaling, reflection and shearing.
The alignment problem
The input is a set of matched feature points $\{\mathbf x_i,\mathbf x_i'\}$ (a point in one image and its partner in the other). A transformation function $\mathbf x'=f(\mathbf x;\mathbf p)$ with parameters $\mathbf p$ is also given. Find the best estimate of the parameters $\mathbf p$. This means: fit the parameters of a transformation to a set of matching feature pairs (correspondences). The choice of $f$ (translation, affine, homography...) depends on how the two views are related.
Parametric (global) warping
A transformation $T$ is a coordinate-changing machine: $\mathbf p'=T(\mathbf p)$. It is global if it is the same for every point and a few numbers (parameters) describe it. Examples: translation, rotation, aspect change, affine, perspective. A matrix represents it: $\mathbf p'=M\mathbf p$.
2x2 linear transformations
Properties of any $2\times2$ transform: the origin maps to the origin, lines map to lines, parallel lines remain parallel. Basic ones: scaling, shearing, rotation (this form cannot express translation).
Scaling
Scaling multiplies each coordinate by a scalar. Uniform: the same scalar for all components ($a=b$, e.g. $\times2$ enlarges the whole house). Non-uniform: different scalars (e.g. $x\times2$, $y\times0.5$ makes the house wide and flat).
Shearing
Shearing slants the shape. It shifts one coordinate in proportion to the other. It changes the shape of an object, but parallel lines remain parallel.
$sh_x$, $sh_y$ are the horizontal and vertical shear factors. The general form with both is $\begin{bmatrix}1&sh_x\\sh_y&1\end{bmatrix}$.
Rotation about the origin
$\theta$: counter-clockwise rotation angle.
Derivation from polar coordinates (slide). Write $x=r\cos\phi$, $y=r\sin\phi$. After the rotation by $\theta$: $x'=r\cos(\phi+\theta)$, $y'=r\sin(\phi+\theta)$. Expand with trig identities: $x'=r\cos\phi\cos\theta-r\sin\phi\sin\theta$, $y'=r\sin\phi\cos\theta+r\cos\phi\sin\theta$. Substitute $x=r\cos\phi$, $y=r\sin\phi$ to get the formulas above.
Worked numerical (rotation). $\theta=30^\circ$: $R=\begin{bmatrix}0.866&-0.5\\0.5&0.866\end{bmatrix}$ (slide). Rotate $P=(2,0)$: $x'=2(0.866)-0=1.732$, $y'=2(0.5)+0=1.0$. So $P'=(1.732,\ 1)$. Length check: $\sqrt{1.732^2+1}=2$, so the length is preserved.
Practice (shear and scale). $P=(2,3)$, horizontal shear $sh_x=0.5$: $x'=2+0.5\times3=3.5$, $y'=3$. Non-uniform scale $a=2,b=0.5$ on $(4,6)$: $(8,3)$.
Translation and homogeneous coordinates
Translation by $(t_x,t_y)$: $x'=x+t_x$, $y'=y+t_y$. Example from the slide: $P=(2,3)$ translated by $(4,2)$ gives $P'=(2+4,3+2)=(6,5)$.
This operation needs the addition of a constant. A general $2\times2$ product gives $x'=ax+by$, $y'=cx+dy$. So $x',y'$ depend only on $x,y$ and have no constant term. Hence we cannot write translation as a $2\times2$ matrix.
Fix: homogeneous coordinates. Add a third coordinate: the point $(x,y)$ becomes $(x,y,1)^T$. To convert back from $(x,y,w)^T$, divide by $w$: $(x/w,\ y/w)$.
The translation sits in the rightmost column. The third coordinate stays 1.
All basic transforms as 3x3 matrices
Translation, scaling, rotation, shearing.
| Transformation | 2x2 (ordinary coordinates) enough? | Homogeneous coordinates |
|---|---|---|
| Scaling | Yes | Optional |
| Rotation about origin | Yes | Optional |
| Shearing | Yes | Optional |
| Translation | No | Needed for a unified matrix representation |
To combine scaling, rotation, shearing and translation in one matrix, use homogeneous coordinates.
Matrix composition and order
Do not apply the transformations one after another, $P'=T_2(T_1P)$. Combine them: $P'=(T_2T_1)P$, so the combined matrix is $T=T_2T_1$. If $T_1$ is applied first and $T_2$ second, the later transform is on the left. Example chain: $\mathbf p'=\text{translation}(t_x,t_y)\cdot\text{rotation}(\theta)\cdot\text{scale}(s,s)\cdot\mathbf p$. It scales first, then rotates, then translates. Order matters: $T_2T_1\ne T_1T_2$ in general, because matrix multiplication is not commutative.
Slide example. Scale the image by 2, then translate by $(3,2)$.
$S=\begin{bmatrix}2&0&0\\0&2&0\\0&0&1\end{bmatrix}$, $T=\begin{bmatrix}1&0&3\\0&1&2\\0&0&1\end{bmatrix}$. Scaling first means $M=TS$:
$M=\begin{bmatrix}1&0&3\\0&1&2\\0&0&1\end{bmatrix}\begin{bmatrix}2&0&0\\0&2&0\\0&0&1\end{bmatrix}=\begin{bmatrix}2&0&3\\0&2&2\\0&0&1\end{bmatrix}$.
Apply to $P=(1,1)$, i.e. $(1,1,1)^T$: $x'=2\cdot1+3=5$, $y'=2\cdot1+2=4$, giving $(5,4)$. Check by steps: scale $(1,1)\to(2,2)$, then translate $\to(5,4)$. ✓
Reverse order (translate first, then scale) gives $ST=\begin{bmatrix}2&0&6\\0&2&4\\0&0&1\end{bmatrix}$ and $(1,1)\to(8,6)$. The result is different. This shows that order matters.
Practice: rotation about a point. Rotate $(3,2)$ by $90^\circ$ about the centre $c=(2,2)$. Rotation matrices act about the origin. So translate by $-c$, rotate, and translate back, i.e. $M=T(c)\,R(90^\circ)\,T(-c)$.
Step 1: $(3,2)-(2,2)=(1,0)$. Step 2: rotate $90^\circ$: $(x',y')=(1\cdot0-0\cdot1,\ 1\cdot1+0)=(0,1)$. Step 3: add $c$: $(2,3)$. So $P'=(2,3)$.
Degrees of freedom and the transformation hierarchy
The degree of freedom (DOF) is the number of independent parameters that define a transformation.
- General $2\times2$ linear transformation $\begin{bmatrix}a&b\\c&d\end{bmatrix}$: 4 independent parameters, so 4 DOF. It represents combinations of scaling, rotation and shearing.
- Scaling $\begin{bmatrix}s_x&0\\0&s_y\end{bmatrix}$: 2 parameters, 2 DOF.
- Rotation: one parameter $\theta$ fixes all four entries (for $30^\circ$: $0.866,-0.5,0.5,0.866$), so 1 DOF.
- Translation: $(t_x,t_y)$, 2 DOF.
Affine transformation
An affine transformation combines a general linear transformation (scaling, rotation, shearing) with a translation. It has the form below, with bottom row $(0,0,1)$.
DOF $=4$ (linear part) $+2$ (translation) $=\mathbf{6}$.
Properties of affine transforms (slide):
- The origin does not necessarily map to the origin.
- Lines map to lines.
- Parallel lines map to parallel lines.
- Ratios (of lengths along a line, and of areas) are preserved.
- Compositions of affine transforms are affine.
- A square becomes a parallelogram.
Projective transformation (homography)
A projective transformation extends the affine transformation with perspective effects. It is also called a homography. It has the form below, with a full $3\times3$ matrix. The last row is not restricted.
The matrix does not change if all entries are multiplied by the same non-zero constant, so the overall scale is arbitrary. Normalise by dividing by $h_{33}$ (set $h_{33}=1$). Then there are 9 entries $-$ 1 redundant scale $=\mathbf{8}$ DOF.
Write $H=\begin{bmatrix}a&b&t_x\\c&d&t_y\\g&h&1\end{bmatrix}$. The affine part is translation, rotation, scaling and shearing. The perspective part $(g,h)$ lets parallel lines converge and introduces perspective distortion. $H$ becomes affine when $g=h=0$.
Properties of projective transforms (slide):
- The origin does not necessarily map to the origin.
- Lines map to lines.
- Parallel lines do not necessarily remain parallel. They can meet at a vanishing point, like railway tracks that are parallel in the world but converge in the image.
- Compositions of projective transforms are projective.
| Transformation | Matrix | DOF | Min. point pairs | Preserves |
|---|---|---|---|---|
| Translation | $\begin{bmatrix}I&\mathbf t\\0&1\end{bmatrix}$ | 2 | 1 | orientation, lengths, angles, parallelism |
| Rigid / Euclidean | $\begin{bmatrix}R&\mathbf t\\0&1\end{bmatrix}$ | 3 | 2 | lengths, angles, areas, parallelism |
| Similarity | $\begin{bmatrix}sR&\mathbf t\\0&1\end{bmatrix}$ | 4 | 2 | angles, length ratios, parallelism |
| Affine | $\begin{bmatrix}A&\mathbf t\\0&1\end{bmatrix}$ | 6 | 3 | parallelism, ratios of lengths along a line, area ratios |
| Projective (homography) | $H$ ($3\times3$) | 8 | 4 | straight lines (collinearity), cross-ratio |
The rigid and similarity rows are standard background. The slides cover scaling 2, rotation 1, linear 4, affine 6, projective 8. Each point pair $(\mathbf x,\mathbf x')$ gives 2 equations. So the minimum number of correspondences is DOF$/2$ rounded up.
| Property | Affine | Projective |
|---|---|---|
| Bottom row of matrix | $(0,0,1)$ | general $(h_{31},h_{32},h_{33})$ |
| DOF | 6 | 8 |
| Parallel lines stay parallel | yes | no (can meet at a vanishing point) |
| Lines map to lines | yes | yes |
| Origin maps to origin | not necessarily | not necessarily |
| Ratios preserved | yes | no (only cross-ratio) |
| Closed under composition | yes | yes |
| Typical use | planar patch, weak perspective | plane viewed from different camera positions, panorama stitching, rectification |
Counting rules that examiners like: linear $2\times2$ = 4 DOF; affine = 4 + 2 = 6; homography = 9 - 1 = 8. Check: the matrix $\begin{bmatrix}a&b&t_x\\c&d&t_y\\g&h&1\end{bmatrix}$ has $a,b,c,d,t_x,t_y,g,h$, which are eight free numbers.
Estimating the transformation from correspondences
This section is standard background. It completes the "alignment" story from the slides.
Affine from point pairs (least squares)
Each pair $(x,y)\to(x',y')$ gives two linear equations in the 6 unknowns:
$$\begin{bmatrix}x&y&1&0&0&0\\0&0&0&x&y&1\end{bmatrix}\begin{bmatrix}a\\b\\t_x\\c\\d\\t_y\end{bmatrix}=\begin{bmatrix}x'\\y'\end{bmatrix}$$
Three non-collinear pairs give an exact solution. More pairs give an over-determined system. Solve it by least squares.
Worked example. $(0,0)\to(1,1)$, $(1,0)\to(2,2)$, $(0,1)\to(0,2)$.
Step 1: from the origin, $t_x=1$, $t_y=1$.
Step 2: from $(1,0)\to(2,2)$: $a+t_x=2\Rightarrow a=1$
$c+t_y=2\Rightarrow c=1$.
Step 3: from $(0,1)\to(0,2)$: $b+t_x=0\Rightarrow b=-1$
$d+t_y=2\Rightarrow d=1$.
Result: $\begin{bmatrix}1&-1&1\\1&1&1\\0&0&1\end{bmatrix}$. Check on a new point: $(1,1)\to(1-1+1,\ 1+1+1)=(1,3)$.
Homography from 4 point pairs (DLT)
Set $h_{33}=1$. A pair $(x,y)\to(x',y')$ then means $x'=\dfrac{h_{11}x+h_{12}y+h_{13}}{h_{31}x+h_{32}y+1}$, and similarly for $y'$. Multiply out to get two linear equations in the eight unknowns:
$$h_{11}x+h_{12}y+h_{13}-h_{31}xx'-h_{32}yx'=x'$$ $$h_{21}x+h_{22}y+h_{23}-h_{31}xy'-h_{32}yy'=y'$$
Four pairs give 8 equations for 8 unknowns. With more pairs, stack the equations into $A\mathbf h=\mathbf 0$ (no $h_{33}=1$ assumption). Take the SVD smallest singular vector (Direct Linear Transform, DLT). With outlier matches, use RANSAC around this method: fit on 4 random pairs, repeat, and keep the model with the most inliers.
Worked example. Pairs: $(0,0)\to(0,0)$, $(1,0)\to(0.5,0)$, $(0,1)\to(0,1)$, $(1,1)\to(0.5,0.5)$.
Step 1, $(0,0)\to(0,0)$: both equations give $h_{13}=0$ and $h_{23}=0$.
Step 2, $(1,0)\to(0.5,0)$: first equation $h_{11}-0.5h_{31}=0.5$; second $h_{21}=0$.
Step 3, $(0,1)\to(0,1)$: first $h_{12}=0$; second $h_{22}-h_{32}=1$.
Step 4, $(1,1)\to(0.5,0.5)$: first $h_{11}-0.5h_{31}-0.5h_{32}=0.5$. Subtract the step-2 equation: $h_{32}=0$, so $h_{22}=1$. Second: $h_{22}-0.5h_{31}=0.5\Rightarrow h_{31}=1$, so $h_{11}=1$.
Result: $H=\begin{bmatrix}1&0&0\\0&1&0\\1&0&1\end{bmatrix}$. Test on $(2,2)$: $w=1\cdot2+1=3$, so $(x',y')=(2/3,\ 2/3)$. The bottom row is non-zero, so this is a genuine projective map: $x'=x/(x+1)$, $y'=y/(x+1)$. Parallel horizontal lines $y=c$ map to lines $y'=c(1-x')$ that all pass through $(1,0)$, which is their vanishing point.
Homogeneous coordinates: $(x,y,w)$ and $(kx,ky,kw)$ are the same point for any $k\ne0$; $w=0$ is a point at infinity. After you apply a homography, you must divide by $w'$ to get pixel coordinates. For affine maps $w'=1$ always, so you do not need to divide.
Quick self-test
1. Describe the three stages of feature matching and give an example for each.
Detection finds the interesting points (Harris corners, SIFT keypoints). Description represents the local patch as a vector (SIFT, ORB, BRIEF, flattened patch). Matching compares the descriptors with Euclidean/SSD/NCC/Hamming and selects the nearest neighbour.
2. Query $Q=(10,20,30)$ and candidate distances are $1.414$, $1.414$, $6.403$. Should the best match be accepted under the ratio test with threshold 0.8?
No. $r=d_1/d_2=1.414/1.414=1>0.8$, so reject. The two best candidates are equally close, so the match is ambiguous. If the distances were $1.414, 6.403, 8.660$, then $r=0.221\lt0.8$ and the match is accepted.
3. Compute SAD, SSD and NCC-type conclusions for $Q=[1,2,3,4]$, $C=[2,3,4,5]$ (flattened patches).
SAD $=4\times1=4$; SSD $=4$. For NCC, both patches have deviations from their means equal to $(-1.5,-0.5,0.5,1.5)$, so NCC $=1$. SAD/SSD penalise the brightness shift. NCC does not.
4. Find the Hamming distance between $11001010$ and $10101110$, and say what it means.
XOR $=01100100$, which has three 1s, so $d_H=3$. The descriptors differ in 3 of 8 bit positions. A smaller distance is better.
5. Why can translation not be written as a $2\times2$ matrix? How do homogeneous coordinates fix it?
A $2\times2$ product gives $x'=ax+by$, $y'=cx+dy$ with no constant term, but translation needs $+t_x,+t_y$. Use $(x,y,1)^T$ and a $3\times3$ matrix with $t_x,t_y$ in the right column. This gives $x'=x+t_x$, $y'=y+t_y$.
6. Scale an image by 3 and then translate by $(2,5)$. Give the single matrix $M$ and the image of $(1,2)$.
$M=TS=\begin{bmatrix}1&0&2\\0&1&5\\0&0&1\end{bmatrix}\begin{bmatrix}3&0&0\\0&3&0\\0&0&1\end{bmatrix}=\begin{bmatrix}3&0&2\\0&3&5\\0&0&1\end{bmatrix}$. $(1,2)\to(3+2,\ 6+5)=(5,11)$. (Translate first then scale gives $(9,21)$: order matters.)
7. Rotate the point $(1,0)$ by $90^\circ$ about the origin, then translate by $(2,3)$. Write the composite matrix.
$R(90^\circ)=\begin{bmatrix}0&-1&0\\1&0&0\\0&0&1\end{bmatrix}$, $T=\begin{bmatrix}1&0&2\\0&1&3\\0&0&1\end{bmatrix}$, $M=TR=\begin{bmatrix}0&-1&2\\1&0&3\\0&0&1\end{bmatrix}$. $(1,0)\to(0\cdot1+2,\ 1+3)=(2,4)$.
8. Compare affine and projective transformations (DOF, form, properties). Which has parallel lines converging?
Projective transformations make parallel lines converge. Affine: bottom row $(0,0,1)$, 6 DOF, parallelism and ratios preserved, a square becomes a parallelogram. Projective (homography): general $3\times3$ matrix, defined up to scale so 8 DOF, only straight lines preserved. Parallel lines can converge at a vanishing point (railway tracks). Affine is the special case $g=h=0$.
9. How many point correspondences are needed to estimate (a) a translation, (b) an affine transform, (c) a homography? Why?
Each correspondence gives 2 equations. (a) 2 DOF gives 1 pair; (b) 6 DOF gives 3 pairs (non-collinear); (c) 8 DOF gives 4 pairs (no three collinear). For robustness, use more pairs with least squares/SVD and RANSAC.
10. State the difference between filtering and warping, and between a global and a local transformation.
Filtering $G(\mathbf x)=h\{F(\mathbf x)\}$ changes the pixel values (range). Warping $G(\mathbf x)=F(h\{\mathbf x\})$ changes the pixel positions (domain). A global transformation is the same function for every point and a few parameters describe it (e.g. a matrix $M$). A local transformation varies across the image.
- Feature pipeline: detect (Harris, SIFT) then describe (patch, SIFT, ORB/BRIEF) then match (distance + nearest neighbour). Brute force is $N^2$ comparisons.
- Euclidean distance $\sqrt{\sum(D_{1i}-D_{2i})^2}$ is for float descriptors. Hamming distance (XOR then count 1s) is for binary descriptors. Smaller means better.
- Ratio test $r=d_1/d_2$: accept if $r\lt0.8$, reject if $r\ge0.8$ (ambiguous). A tie between the two nearest gives $r=1$, so reject.
- SSD $=\sum(H-F)^2$, SAD $=\sum|H-F|$ (lower is better, 0 if identical, sensitive to brightness).
- NCC is in $[-1,1]$ (higher is better, mean-subtracted, robust to additive brightness). The slide example: SAD/SSD pick $C_1$, NCC picks $C_2$.
- Filtering changes range, warping changes domain. Alignment = fit transformation parameters $\mathbf p$ in $\mathbf x'=f(\mathbf x;\mathbf p)$ to matched pairs.
- $2\times2$ transforms: scale $\mathrm{diag}(a,b)$, shear $\begin{bmatrix}1&sh_x\\sh_y&1\end{bmatrix}$, rotation $\begin{bmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{bmatrix}$. The origin is fixed and parallel lines stay parallel. Translation needs homogeneous coordinates.
- Composition: $T=T_2T_1$ (first transform rightmost); order matters. Example: scale 2 then translate $(3,2)$ gives $\begin{bmatrix}2&0&3\\0&2&2\\0&0&1\end{bmatrix}$.
- DOF: scaling 2, rotation 1, linear 4, translation 2, affine 6, projective (homography) 8 ($9-1$ scale).
- Affine preserves parallelism. Projective does not (vanishing points). Affine is projective with $g=h=0$.
- Estimation: affine needs 3 pairs (least squares). Homography needs 4 (DLT, SVD, RANSAC for outliers). Divide by $w'$ after you apply a homography.