Search

Search papers and lessons

Feature Matching

Level 3 · 8 min read

Feature matching pairs descriptors between two images by nearest neighbour, keeps only the unambiguous pairs, and then uses geometry with RANSAC to discard the rest.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Computer vision
  • Discrete math
  • Linear algebra
  • Architectures
  • Sign in to see your progress

TL;DR

Feature matching takes the descriptors from two images and pairs each descriptor with its most similar partner in the other image. Most of those pairs are wrong, so the pairs are filtered twice: first by a ratio test and mutual agreement on descriptor distance, then by fitting a geometric model with RANSAC and keeping only the pairs that agree with it. What survives is a set of correspondences between the two images.

Where you'll see it: the first step of structure-from-motion, where correspondences between photos are triangulated into 3D points, and image stitching, where they define how one photo overlaps the next.

Intuition

Local features give you two bags of descriptors, one per image. Matching asks: which point in image B is the same physical point as this point in image A? Descriptors that look alike are candidates, so the natural first answer is the nearest one in descriptor space.

That answer is noisy. A repeated window on a building front gives many descriptors that look alike, and some points visible in A are not visible in B at all. So the nearest neighbour is often a wrong point that happens to look similar. The fix is to demand more evidence than "closest". A match should be clearly better than the alternatives, and it should agree with the geometry of the scene: if two points really are the same, the way the two images are related must map one onto the other.

The first filter uses only descriptors. The second uses the fact that a camera pair imposes rules on where matches can lie. Because some matches are still wrong, a robust fitting method is used rather than least squares.

flowchart LR
  A[Descriptors in image A] --> N[Nearest neighbour<br/>in image B]
  N --> R[Ratio test<br/>and mutual check]
  R --> G[Fit a geometric model<br/>with RANSAC]
  G --> C[Inlier correspondences]

The math

Nearest neighbour

Let ai\mathbf{a}_i be a descriptor in image A and {bj}\{\mathbf{b}_j\} the descriptors in image B. The nearest neighbour is

j∗=arg⁡min⁡j∥ai−bj∥2j^\ast = \arg\min_j \lVert \mathbf{a}_i - \mathbf{b}_j \rVert_2

where ∥⋅∥2\lVert \cdot \rVert_2 is the Euclidean distance. The index j∗j^\ast is the candidate match. Finding it exactly means comparing against every descriptor in B, which is linear in the number of points. Approximate search structures, covered in Approximate Nearest Neighbor Search, trade a small chance of missing the true nearest neighbour for a large speedup.

Ratio test

Let d1≤d2d_1 \leq d_2 be the distances from ai\mathbf{a}_i to its two nearest neighbours in B. Accept the match only when

d1d2<τ\frac{d_1}{d_2} < \tau

with τ\tau around 0.8. The reason is that a correct match is typically much closer than any wrong one, so its ratio is small. A repeated structure gives two almost equally close candidates, a ratio near 1, and the test rejects it. Lowe introduced this test in the SIFT paper.

Mutual check

Also search in the other direction: find the nearest neighbour in A for each bj\mathbf{b}_j. Keep a pair only if each point is the other's nearest neighbour. This is a cheap way to remove many-to-one assignments.

Geometric verification with RANSAC

The surviving pairs still contain outliers. Suppose the scene is roughly planar, or the two views differ by a pure rotation, so the relation between the images is a homography, a 3 by 3 matrix HH with xB∼HxA\mathbf{x}_B \sim H \mathbf{x}_A in homogeneous coordinates. For a general scene with a moving camera the right model is the fundamental matrix FF from Epipolar Geometry, which satisfies xB⊤FxA=0\mathbf{x}_B^\top F \mathbf{x}_A = 0 for each true match.

A least-squares fit to all pairs would be pulled off by the outliers. RANSAC (random sample consensus, Fischler and Bolles, 1981) avoids this:

  1. Draw a minimal sample of ss pairs. The minimum is 4 for a homography and 8 for the eight-point method for FF.
  2. Fit the model to the sample.
  3. Count the inliers: pairs whose residual is below a threshold ϵ\epsilon.
  4. Keep the model with the most inliers, and refit to all of them.

The number of random draws needed to find an all-inlier sample with probability pp depends on the inlier ratio ww:

N=ln⁡(1−p)ln⁡(1−ws)N = \frac{\ln(1 - p)}{\ln(1 - w^s)}

With p=0.99p = 0.99, a low inlier ratio drives NN up quickly, because the chance that a random sample is all inliers is wsw^s.

Worked examples

Example
RANSAC iteration count, homography

Take p=0.99p = 0.99, a homography with s=4s = 4, and an inlier ratio w=0.5w = 0.5. These numbers are illustrative.

  1. Chance that one sample of 4 is all inliers: ws=0.54=0.0625w^s = 0.5^4 = 0.0625.
  2. Chance that one sample is not all inliers: 1−0.0625=0.93751 - 0.0625 = 0.9375.
  3. ln⁡(1−p)=ln⁡(0.01)=−4.6052\ln(1 - p) = \ln(0.01) = -4.6052.
  4. ln⁡(0.9375)=−0.06454\ln(0.9375) = -0.06454.
  5. N=−4.6052/−0.06454=71.35N = -4.6052 / -0.06454 = 71.35, so draw 72 samples.

Now the same scene with w=0.8w = 0.8.

  1. w4=0.4096w^4 = 0.4096, so ln⁡(1−0.4096)=ln⁡(0.5904)=−0.5270\ln(1 - 0.4096) = \ln(0.5904) = -0.5270.
  2. N=4.6052/0.5270=8.74N = 4.6052 / 0.5270 = 8.74, so draw 9 samples.

Interpretation: a drop in inlier ratio from 0.8 to 0.5 multiplies the work by about eight, which is why the descriptor-level filters matter. Each outlier removed earlier makes the geometric step cheaper.

Example
Ratio test and mutual check on a tiny set

Image A has descriptors a1\mathbf{a}_1 and a2\mathbf{a}_2. Image B has b1\mathbf{b}_1 and b2\mathbf{b}_2. Distances (illustrative):

b1\mathbf{b}_1b2\mathbf{b}_2
a1\mathbf{a}_10.200.50
a2\mathbf{a}_20.310.33
  1. For a1\mathbf{a}_1: d1=0.20d_1 = 0.20, d2=0.50d_2 = 0.50, ratio 0.400.40. Below 0.8, candidate a1→b1\mathbf{a}_1 \to \mathbf{b}_1.
  2. For a2\mathbf{a}_2: d1=0.31d_1 = 0.31, d2=0.33d_2 = 0.33, ratio 0.940.94. Above 0.8, rejected as ambiguous.
  3. Mutual check on a1→b1\mathbf{a}_1 \to \mathbf{b}_1: the nearest neighbour of b1\mathbf{b}_1 in A is a1\mathbf{a}_1 (distance 0.20 against 0.31 for a2\mathbf{a}_2). The pair survives.

Interpretation: a2\mathbf{a}_2 looks like both b1\mathbf{b}_1 and b2\mathbf{b}_2 equally well, so no match is safe. The ratio test rejects it without any geometry.

Tradeoffs and failure modes

SituationProblemBetter choice
Repeated texture or symmetric structureAmbiguous nearest neighboursStricter ratio, then geometric verification
Wide baseline or large viewpoint changeFew correct initial pairs, low inlier ratioAffine-invariant features, or learned matchers
Scene that is nearly planarFundamental matrix is poorly constrainedUse a homography model
Many thousands of descriptorsExact matching is slowApproximate nearest-neighbour search
Very low inlier ratioRANSAC needs huge iteration countsBetter descriptors, or prior information about motion

A correspondence can be wrong and still look good under the descriptor test, if the scene has a repeated pattern. Geometric verification is what catches that, but only if the model is correct for the scene. If the wrong model is fitted, RANSAC will confidently keep the wrong matches.

History

Nearest-neighbour matching with descriptors became standard after Lowe's 2004 SIFT paper, which introduced the ratio test in the form used here. RANSAC was introduced by Fischler and Bolles in 1981 for fitting models in the presence of gross outliers, and it became a standard tool for geometric verification in structure from motion and panorama stitching. The combination of descriptor matching plus RANSAC is the standard recipe still used in most classical pipelines.

Check yourself

QuestionWhy does the ratio test reject a match whose two nearest neighbours are almost equally close, even when the nearest one is the correct point?Show answer

If the second-nearest neighbour is almost as close, the descriptor alone cannot tell the candidates apart, and a wrong point could be the true one. A correct match in a distinctive region typically has a clearly smaller distance than any alternative, so its ratio is small. The test accepts only those, so the rejected cases are those where the data gives no clear winner. Some correct matches are lost, but the surviving set has far fewer wrong ones.

QuestionWith p=0.99p = 0.99 and s=8s = 8, compute the number of RANSAC samples for an inlier ratio of 0.8.Show answer

Compute w8=0.88=0.16777w^8 = 0.8^8 = 0.16777. Then 1−w8=0.832231 - w^8 = 0.83223 and ln⁡(0.83223)=−0.18365\ln(0.83223) = -0.18365. With ln⁡(0.01)=−4.6052\ln(0.01) = -4.6052, N=4.6052/0.18365=25.08N = 4.6052 / 0.18365 = 25.08, so draw 26 samples. The eight-point method needs eight pairs per sample, so each draw is more expensive, and this is why the homography case with four pairs is often preferred when the scene allows it.

QuestionWhy is a least-squares fit to all candidate correspondences a bad way to verify geometry?Show answer

Least squares minimises the total squared residual, so a few large residuals from wrong matches pull the fitted model toward them. The fit then serves none of the matches well. RANSAC instead fits to small random samples and scores each model by its inlier count, so the wrong matches do not influence the chosen model. The final refit uses only the inliers, which is when least squares is appropriate.

QuestionYou run feature matching on two photos of a flat facade taken from one side of the building. Which geometric model would you fit, and what would you expect the inlier count to show?Show answer

A flat facade seen from different positions is related by a homography, so fitting HH with four-point samples is appropriate. A fundamental matrix can also be fitted to planar points, but many matrices then explain the same matches, so the fit is not well determined. If the inlier count is much lower than the number of matches that look correct, either the threshold ϵ\epsilon is too tight for the image resolution, or the surface is not as planar as assumed, which means the homography is the wrong model.

QuestionWhy does the mutual check catch some errors that the ratio test misses?Show answer

The ratio test looks only at the distances from one descriptor in A to its neighbours in B. Two descriptors in A can both pick the same descriptor in B, which the ratio test does not see because each choice looks clear from A's side. The mutual check asks whether the choice holds in the other direction, so a many-to-one assignment fails it for at least one of the two points.

Sources

  • Fischler and Bolles (1981). Random Sample Consensus: A Paradigm for Model Fitting. Random sample consensus
  • Lowe (2004). Distinctive Image Features from Scale-Invariant Keypoints. DOI