Algorithm Search⌘K

Problemintro

Feature matching

Also called keypoint matching, descriptor matching.

Given the features of two images, find the pairs that show the same scene point, usually by nearest neighbors in descriptor space and a filter of the doubtful ones.

Time
O(m n D) worst
Where
D = the length of a descriptor; m = the number of features of the first image; n = the number of features of the second image

Feature matching takes the features of two images, each a position and a descriptor, and returns pairs that show the same point of the scene. The true pairs are fixed by the geometry of the scene, which the matcher does not know, so in practice the problem is solved in two parts. First each feature gets candidates by nearest neighbor search in descriptor space. Then doubtful candidates are removed by filters such as Lowe's ratio test and the mutual check, and often by a geometric test with RANSAC. Exact search with brute force costs O(mnD)O(m n D); approximate search with a kd-forest is much faster for SIFT-sized descriptors. Learned matchers such as SuperGlue and LoFTR replace the hand-made filters with a network. Feature matching is the first step of image stitching, structure from motion and visual odometry.

In plain words

Take two photos of the same building from two places. A detector marks a few hundred small spots in each photo, such as corners of windows, and describes each spot by a list of numbers. Feature matching draws lines between spots of the two photos that show the same corner. The easy idea is to link each spot to the spot with the most similar numbers. That goes wrong when many windows look the same, or when a spot is hidden in the other photo, because a hidden spot still has some most similar spot. So matchers add checks: is the best partner clearly better than the second best, do the two spots pick each other, and do all lines agree with one camera motion?

Definition

Input. Two finite lists of features. The first image has mm features (xi,ai)(\mathbf x_i, a_i) for 0≤i<m0 \le i < m, and the second has nn features (yj,bj)(\mathbf y_j, b_j) for 0≤j<n0 \le j < n. Here xi,yj∈R2\mathbf x_i, \mathbf y_j \in \mathbb R^2 are image positions and ai,bj∈RDa_i, b_j \in \mathbb R^D are descriptors. A distance dd on descriptors is given, usually the Euclidean distance, or the Hamming distance for binary descriptors.

Output. A set M⊆{0,…,m−1}×{0,…,n−1}M \subseteq \{0, \dots, m-1\} \times \{0, \dots, n-1\} of pairs. Most methods return a partial matching: each ii and each jj is in at most one pair.

The goal. Let M∗M^* be the set of true pairs, those whose two features show the same scene point within a tolerance ε\varepsilon. For example, for a plane seen by two cameras, M∗={(i,j):∥π(Hx~i)−yj∥≤ε}M^* = \{ (i, j) : \lVert \pi(H \tilde{\mathbf x}_i) - \mathbf y_j \rVert \le \varepsilon \} for a homography HH, where x~\tilde{\mathbf x} adds a coordinate 1 and π\pi divides by the last coordinate. The matcher does not know HH or the 3D scene, so M∗M^* is not a function of the input, and a matcher is judged by its precision ∣M∩M∗∣/∣M∣\lvert M \cap M^* \rvert / \lvert M \rvert and its recall ∣M∩M∗∣/∣M∗∣\lvert M \cap M^* \rvert / \lvert M^* \rvert on data with known geometry.

The computable versions. The following are exact problems with a unique answer once a tie rule is fixed, for example "the smaller index wins":

  • Nearest neighbor matching: M={(i,f(i))}M = \{ (i, f(i)) \} with f(i)=arg⁡min⁡jd(ai,bj)f(i) = \arg\min_j d(a_i, b_j). This is the bichromatic all nearest neighbors problem.
  • Filtered matching: the pairs of nearest neighbor matching that pass a filter, such as d(ai,bf(i))<r⋅d2(i)d(a_i, b_{f(i)}) < r \cdot d_2(i), where d2(i)d_2(i) is the distance to the second nearest neighbor, or g(f(i))=ig(f(i)) = i, where gg is nearest neighbor matching in the other direction.
  • Optimal partial assignment: a partial matching MM that maximizes ∑(i,j)∈M(z−d(ai,bj))\sum_{(i, j) \in M} (z - d(a_i, b_j)) for a value z>0z > 0, the gain of a pair minus its distance. Pairs with d≥zd \ge z never help, so features without a good partner stay unmatched.

The number of partial matchings is ∑k=0min⁡(m,n)(mk)(nk)k!\sum_{k=0}^{\min(m, n)} \binom{m}{k} \binom{n}{k} k!, so the optimization version cannot be solved by trying them all, except on tiny inputs.

Variants

  • Sparse and dense. Sparse matching uses keypoints from a detector. Dense or detector-free matching, such as LoFTR, matches cells of a grid and refines the positions, which helps on surfaces with little texture.
  • One-to-many. Keep the kk nearest neighbors of each feature, for example to give RANSAC more candidates. The result is not a matching.
  • Guided matching. When a fundamental matrix or a homography is already known, search only near the epipolar line or the predicted position.
  • Matching against a database. Match the features of one image against those of many images, as in image retrieval, where the features are often quantized into visual words.
  • Binary descriptors. With ORB or BRIEF, the distance is the Hamming distance and hashing replaces kd-trees.

Examples

One-dimensional descriptors make a case that you can check by hand. Let a0=0a_0 = 0, a1=1a_1 = 1, a2=5a_2 = 5 and b0=0.1b_0 = 0.1, b1=1.2b_1 = 1.2, b2=1.22b_2 = 1.22, b3=9b_3 = 9, with d(a,b)=∣a−b∣d(a, b) = \lvert a - b \rvert. Suppose the true pairs are M∗={(0,0),(1,1)}M^* = \{(0, 0), (1, 1)\}, and a2a_2 has no true partner.

feature nearest d1d_1 second nearest d2d_2 d1/d2d_1 / d_2 picked back?
a0a_0 b0b_0 0.1 b1b_1 1.2 0.083 yes
a1a_1 b1b_1 0.2 b2b_2 0.22 0.909 yes
a2a_2 b2b_2 3.78 b1b_1 3.8 0.995 no, b2b_2 picks a1a_1
method output MM precision recall
nearest neighbor {(0,0),(1,1),(2,2)}\{(0,0), (1,1), (2,2)\} 2/3 1
ratio test, r=0.8r = 0.8 {(0,0)}\{(0,0)\} 1 1/2
mutual check {(0,0),(1,1)}\{(0,0), (1,1)\} 1 1
optimal partial assignment, z=1z = 1 {(0,0),(1,1)}\{(0,0), (1,1)\}, gain 0.9+0.8=1.70.9 + 0.8 = 1.7 1 1
full assignment of minimum total distance {(0,0),(1,1),(2,2)}\{(0,0), (1,1), (2,2)\}, total 4.084.08 2/3 1

The ratio test drops the true pair (1,1)(1, 1), because b1b_1 and b2b_2 are almost equally close to a1a_1: it cannot tell a repeated pattern from a true partner. The mutual check keeps (1,1)(1, 1) and drops (2,2)(2, 2), because b2b_2 prefers a1a_1. For the partial assignment, only the pairs (0,0)(0,0), (1,1)(1,1), (1,2)(1,2) and (1,0)(1,0) have d<1d < 1 and a positive gain; the best choice is (0,0)(0,0) with (1,1)(1,1), since (0,0)(0,0) with (1,2)(1,2) gives only 1.68. A full assignment must use a2a_2, so it adds the false pair (2,2)(2, 2). No method wins on every input; the table shows one input.

Known algorithms

  • Brute force. Compute all mnm n distances: O(mnD)O(m n D) time, exact. On a GPU this is often the fastest choice for a few thousand features.
  • kd-tree search. Exact nearest neighbors with the kd-tree search, fast in low dimension, but for D=128D = 128 it visits most of the leaves.
  • Best bin first. Approximate search in one kd-tree that stops after a fixed number of leaves, which bounds the time per query; Lowe (2004) used it for SIFT.
  • Randomized kd-forest. Several trees with random splits searched with one priority queue, a default of the FLANN library.
  • Locality-sensitive hashing. Approximate search by hashing, common for binary descriptors.
  • Filters. The ratio test and the mutual check cost one more nearest neighbor and one more search in the other direction. RootSIFT changes the descriptors before the search.
  • Global assignment. The optimal partial assignment is a maximum weight matching in a bipartite graph and can be solved with the Hungarian algorithm in O((m+n)3)O((m + n)^3) time.
  • Learned matchers. SuperGlue refines the descriptors with attention and solves an entropic partial assignment with Sinkhorn iterations; LoFTR matches a coarse grid without a detector and refines each match in a window.
  • Verification. RANSAC or PROSAC fits a homography or a fundamental matrix and keeps the pairs that agree with it, as in spatial verification.
AlgorithmTimeSpacePropertiesCode
BRIEF descriptorO(W H + k d) worst · average · bestO(W H + k d / w) extradeterministicexactheuristicnot rotation invariantnot scale invariant1 language
BRISK featuresO(W H + n q) worst · average · bestO(W H + n) extradeterministicnot exactheuristicrotation invariantscale invariant1 language
DAISY descriptorO(W H B Q (R + T)) worst · average · bestO(W H B Q) extradensedeterministicexactnot rotation invariant1 language
KAZE featuresO(N W H + n) worst · average · bestO(N W H + n) extradeterministicnot exactheuristicrotation invariantscale invariant1 language
LoFTRO(M N d + K w^2 d) worst · average · bestO(M N + w^2) extradetector freedeterministiclearnednetwork cost excludedsubpixelsymmetric coarse matches1 language
ORB featuresO(W H log(W H) + n m) worst · averageO(W H + n m) extradeterministicnot exactheuristicrotation invariantnot scale invariant1 language
SuperGlueO(T M N) worst · average · bestO(M N) extradeterministiclearnednetwork cost excludedpartial assignmentsymmetric1 language
Speeded up robust featuresO(W H + n) worst · average · bestO(W H + n) extradeterministicnot exactheuristicrotation invariantscale invariant1 language

Bounds

In the real RAM with O(1)O(1) cost per arithmetic operation, brute force gives O(mnD)O(m n D) for nearest neighbor matching and for both filters. Every exact method must read the input, so Ω((m+n)D)\Omega((m + n) D) is a trivial lower bound. In fixed low dimension, kd-trees and other point location structures answer each query in about logarithmic time on typical data, but descriptors have DD from 32 to 256. For such dimensions, results of fine-grained complexity under the Strong Exponential Time Hypothesis indicate that the exact bichromatic closest pair problem, and so also exact nearest neighbor matching, has no algorithm that is much faster than mnm n when DD grows like log⁡n\log n. This is why practical matchers use approximate search.

The goal M∗M^* has a different limit, which is not about time. Two scene points with equal descriptors give the same input whichever of them is the true partner, so no method that uses only the descriptors can be correct on both. Filters reduce this risk by dropping such features; geometry, as in RANSAC, or context, as in SuperGlue, can resolve it. Precision and recall of real methods are measured on benchmarks, not proved.

Reductions

  • To nearest neighbor search. Nearest neighbor matching is mm queries of nearest neighbor search against nn points, plus the cost to build the index. The ratio test needs two nearest neighbors per query, and the mutual check needs nn more queries in the other direction.
  • From bichromatic closest pair. The closest pair between the two lists is the nearest neighbor pair of smallest distance, which takes O(m)O(m) more time. So a fast exact matcher gives a fast exact bichromatic closest pair algorithm, which is the reason for the bound above.
  • To the assignment problem. Add a dummy partner for each feature with gain 0. The optimal partial assignment becomes an assignment problem on m+nm + n rows and columns.

Applications

References

  1. Richard Szeliski, Computer Vision: Algorithms and Applications, 2nd edition, Springer, 2022, Section 7.1.3, feature matching: matching strategies and error rates, efficient matching, and feature match verification
  2. David G. Lowe, Distinctive image features from scale-invariant keypoints, International Journal of Computer Vision 60(2), 2004, 91-110, Section 7, keypoint matching with the ratio of the two nearest distances, approximate search with best bin first, and verification by a pose
  3. Marius Muja and David G. Lowe, Scalable nearest neighbor algorithms for high dimensional data, IEEE Transactions on Pattern Analysis and Machine Intelligence 36(11), 2014, 2227-2240, the approximate nearest neighbor methods for descriptor matching and their measured speed and precision
  4. Paul-Edouard Sarlin, Daniel DeTone, Tomasz Malisiewicz and Andrew Rabinovich, SuperGlue, learning feature matching with graph neural networks, IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), 2020, the formulation of matching as a partial assignment with a dustbin for unmatched features

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a contract.yaml