Skip to the lesson
FixTheRobot Explore Robotics / Localization & SLAM / M6 Visual and Visual-Inertial SLAM / M6-T2

Localization & SLAM · Theory · M6

M6-T2 — Feature Detection and Tracking

Finding the same point in the next image is the step every visual SLAM system depends on, and the one that fails most quietly.

  • Lesson 2 of 9
  • Module 6: Visual and Visual-Inertial SLAM
  • Theory
  • About 9 minutes to read

Visual SLAM needs correspondences: pairs of pixels in different images that are projections of the same 3D point. There are two ways to get them. Detect features in both images and match their descriptors, or detect once and track each point with optical flow. Both end with a set of candidate pairs that still contains wrong ones, which must be rejected before they reach the estimator. This lesson covers both pipelines, the checks that make them reliable, and how they fail.

Learning objectives

After this lesson, you should be able to:

  • Compare detect-and-match with optical-flow tracking: cost, accuracy and failure modes.
  • Apply the ratio test and the forward-backward check and say what each rejects.
  • Solve the Lucas-Kanade equation for a small displacement and state its assumptions.
  • Compute how many RANSAC iterations a given inlier ratio requires.

The problem and the intuition

Imagine a photo album of a trip taken every second. To work out how you moved between two photos you pick a few landmarks (a church tower, a tree) and find them in both. There are two ways. You can write a description of each landmark and search the next photo for the closest description (match). Or, since the next photo was taken only a second later, you can look near where the landmark was and follow it (track). Matching can find a point after a long gap or a big motion; tracking is quicker and more precise for small motion, and does not need descriptors.

Both produce some wrong pairs: a repeated window, a leaf that moved, a reflection. Wrong pairs are far more damaging than noisy ones, because a least-squares estimator treats a gross error as data. Rejecting them (ratio test, back-check, geometric verification) is part of the front end, not an afterthought.

Correspondence quality decides visual SLAM quality. A hundred correct matches beat a thousand with ten percent wrong ones, unless the wrong ones are removed.

Detect-and-match versus trackingTwo pipelines. Detect and match: detect features in both images, compute descriptors, match by distance with a ratio test and verify geometrically. Tracking: detect once, then follow each feature with optical flow, check forward-backward consistency and re-detect when features are lost.Detect and match Detectboth imagesDescribeORB / SIFTMatchratio testVerifyRANSAC Track Detectfirst imageOptical flowKLT, pyramidBack-checkforward-backwardVerifyRANSAC when features run out, detect new ones in empty cells
Two ways to obtain correspondences, both ending with geometric verification.

The algorithms: matching, tracking, verification

Problem. Given images I1 and I2 of a static scene from nearby viewpoints, produce a set of pixel pairs (p1, p2) that observe the same 3D point, with as few wrong pairs as possible.

Assumptions. Brightness constancy (a point has the same intensity in both images), small motion between frames for tracking, a mostly static scene, and enough texture. The geometric verification assumes a rigid scene viewed by one camera.

Detect and match

  1. Detect and describe. Run a detector (FAST, Shi-Tomasi, ORB) in each image, with a grid so that points are spread out, and compute a descriptor for each (M6-T1).
  2. Nearest neighbour. For each descriptor in image 1, find the closest descriptor in image 2 by Hamming (binary) or Euclidean (float) distance. Brute force costs O(n1n2); approximate search or a restriction to a window around a predicted position makes it faster.
  3. Ratio test (Lowe, 2004). Accept the match only if d1 / d2 < ρ (typically 0.7 to 0.8), where d1 and d2 are the distances to the best and second-best candidates. A true match is much closer than any other; a point on a repeated texture has several nearly-equal candidates and fails the test.
  4. Cross-check. Optionally require that the match is mutual: the best match of the match is the original.

Optical-flow tracking (Lucas-Kanade)

Brightness constancy says I(p + d, t+1) = I(p, t). Linearise for small d: Ix dx + Iy dy = −It. One equation, two unknowns, so Lucas and Kanade (1981) assume the displacement is the same over a window W and solve the least-squares problem:

Lucas-Kanade normal equations
G d=eG\,d=e
G=∑W[Ix2Ix⋅IyIx⋅IyIy2]G=\sum _{W}\begin{bmatrix}\mathrm{Ix}^{2} & \mathrm{Ix}\cdot \mathrm{Iy} \\ \mathrm{Ix}\cdot \mathrm{Iy} & \mathrm{Iy}^{2}\end{bmatrix}
e=−∑W[Ix⋅It, Iy⋅It]⊤e=-\sum _{W}[\mathrm{Ix}\cdot \mathrm{It},\,\mathrm{Iy}\cdot \mathrm{It}]^{\top}

G is the same structure tensor as the Harris matrix: the tracker is solvable exactly where the feature is a good corner (both eigenvalues large). Large motion violates the linearisation, so the standard remedy is a pyramid (Bouguet, 2001): track coarsely at low resolution, then refine at each finer level, iterating the step several times per level.

Rejecting wrong correspondences

  • Forward-backward check. Track from image 1 to 2 and back to 1; keep the point if it returns within about 1 px of its start.
  • Status and error. Drop points that leave the image, whose window residual is large, or whose G has a small eigenvalue.
  • Geometric verification with RANSAC (Fischler and Bolles, 1981). Repeatedly draw the minimal sample needed for a model (5 points for the essential matrix, 8 for the fundamental, M6-T3), fit it, and count the pairs consistent with it. Keep the model with most inliers and discard the rest. The number of iterations N needed to draw at least one all-inlier sample with probability p when the inlier ratio is w and the sample size is s is N = log(1 − p) / log(1 − ws).
d
displacement of a feature between two images (pixels).
G
structure tensor over the window; the tracker is well-posed where its smaller eigenvalue is large.
ρ
ratio-test threshold on the best-to-second-best distance.
w, s, p
inlier ratio, RANSAC sample size, desired success probability.

Strengths, limitations, failure modes

Matching versus tracking
ApproachStrengthLimitation / failure mode
Detect and match (ORB)Handles larger viewpoint change; supports relocalisation and loop closure through the descriptorsMore expensive per frame; repeated texture and illumination change cause wrong matches; positions less precise
Optical-flow tracking (KLT)Fast, sub-pixel precise, no descriptors neededNeeds small motion and constant brightness; drifts on long tracks; cannot recognise a place after a gap
BothComplementary: track frame to frame, match for keyframes and loopsWrong correspondences must still be rejected geometrically

A worked example: ratio test, one Lucas-Kanade step, RANSAC iterations

  1. Ratio test. A feature has a best match at Hamming distance 28 and a second best at 52. Ratio 28 / 52 = 0.54 < 0.8: accept. Another has 40 and 46, ratio 0.87 > 0.8: reject, because two candidates are almost equally good (probably a repeated pattern).
  2. One Lucas-Kanade step. Over a window, suppose G = [[40, 10], [10, 30]] and e = (20, 5). det G = 40 × 30 − 10 × 10 = 1100, G⁻¹ = (1/1100) [[30, −10], [−10, 40]]. Then d = (1/1100) (30 × 20 − 10 × 5, −10 × 20 + 40 × 5) = (550, 0) / 1100 = (0.5, 0) px. The feature moved half a pixel to the right. If G were [[40, 0], [0, 0.1]] (an edge), the second equation would have e/G enormous and the vertical displacement would be unreliable: the aperture problem.
  3. Forward-backward error. Track the point 0.5 px forward and it returns 0.45 px back: the error is 0.05 px, accept. A point that jumps onto a neighbouring leaf and returns 6 px from its start is rejected.
  4. RANSAC iterations. To estimate the essential matrix (s = 5) with probability p = 0.99 of at least one clean sample: with 50% inliers, N = ln 0.01 / ln(1 − 0.55) = 4.605 / 0.0317 = 145 iterations. With 70% inliers, only 25. With the 8-point algorithm (s = 8) and 50% inliers it is 1177. That is why the 5-point solver is preferred and why the quality of the front end (the inlier ratio) directly sets the cost.
  5. Track loss. A robot turns quickly: the image moves 80 px between frames. A 21 × 21 window with one pyramid level cannot follow it, but with three levels the coarsest level sees only 10 px of motion and recovers. Add motion blur, and the gradients inside the window vanish; G becomes small and the tracker returns noise. This is the failure that the first lab reproduces.

Engineering implications and limitations

Engineering implications

  • Use a pyramid and a prior. Initialise the tracker from an IMU or constant-velocity prediction so that the search is short (M6-T6).
  • Replenish features. Tracks die. Keep a minimum count (for example 150) and detect new points in cells without any, so that coverage is maintained.
  • Always verify geometrically. RANSAC on an essential or fundamental matrix, or a PnP-based check against the map, before any match reaches the estimator.
  • Tune the ratio test to the scene. Textured outdoor scenes tolerate 0.8; repetitive indoor scenes need 0.6–0.7.
  • Watch exposure and blur. Log the number of tracked features per frame; a sudden drop is the first sign of a lighting change or fast motion.
  • Constrain the search. Projecting map points with the predicted pose and searching only a small window around the prediction is faster and avoids many wrong matches (ORB-SLAM does this; M6-T5).

Limits

Brightness constancy is false for specular surfaces, automatic exposure changes and shadows. Short-baseline tracking accumulates drift along long tracks. Matching cannot distinguish a true repeat from an aliased one without extra evidence. A learned front end (SuperPoint with SuperGlue or LightGlue) is more robust but needs a GPU and its failures are harder to predict.

Common misconceptions

“A match with the smallest distance is correct.”

The smallest distance can be wrong when several candidates are similar. The ratio to the second best is what tells.

“RANSAC removes all outliers.”

It removes those inconsistent with the model. Outliers consistent with the model (for example along the epipolar line) remain.

“Optical flow works for any motion.”

It assumes small displacement and constant brightness; pyramids extend the range but not without limit.

“Tracking and matching are alternatives.”

Real systems use both: tracking for the frame-to-frame front end, descriptors for keyframes, loops and relocalisation.

Engineering takeaways

  1. Correspondences come from matching descriptors or tracking with optical flow; both need outlier rejection.
  2. The ratio test rejects ambiguous matches; the forward-backward check rejects unstable tracks; RANSAC enforces the geometry.
  3. Lucas-Kanade solves G d = e over a window and is well-posed exactly where the window contains a corner.
  4. RANSAC cost grows steeply as the inlier ratio falls and as the sample size grows.
  5. Blur, textureless regions, repeated texture and brightness change are the main sources of failed correspondences.

References and further reading

Primary papers and authoritative documentation.

Knowledge check

Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

A descriptor has candidate matches at distances 30 and 33. Should it be accepted with a ratio threshold of 0.8, and what does the result suggest?

Explanation

The ratio is 30 / 33 = 0.91, above 0.8, so it is rejected. Two candidates are almost equally close, which suggests repeated texture; accepting the match would risk a wrong correspondence.

Question 2

Why can a Lucas-Kanade tracker follow a corner but not a point on a straight edge?

Explanation

On an edge the structure tensor G has one small eigenvalue, so the displacement along the edge is not determined (the aperture problem) and G is nearly singular. At a corner both eigenvalues are large and G d = e has a unique, stable solution.

Question 3

The inlier ratio drops from 70% to 50% when a robot enters a cluttered area. What happens to RANSAC with the 5-point solver and p = 0.99?

Explanation

The required iterations rise from about 25 to about 145: RANSAC needs six times more samples, and the chance that a fixed budget fails rises. If the budget is fixed (as in real-time systems), the estimate becomes less reliable, which is why improving the inlier ratio in the front end matters.

What’s next

With reliable correspondences in two images, the next lesson recovers the camera motion between them: epipolar geometry, the essential matrix, PnP, and the scale ambiguity that monocular visual odometry cannot escape.

Practice this lesson

Diagnose Feature-Tracking Failure

Tracked features vanish or drift: diagnose blur, low texture and bad thresholds and repair the tracker.

Next lesson

M6-T3 — Visual Odometry

Estimate relative pose from matched features with epipolar geometry and PnP, and explain scale and drift.

Nothing below is required to finish this lesson.