NOTE / 4/12/2019
[MVG] Robust Estimation: RANSAC and Robust Kernels
In visual SLAM, we first establish 3D–3D, 3D–2D, or 2D–2D correspondences and then estimate camera motion from them. Perfect estimation requires perfect matches, but real matches often contain many errors. Their impact can be reduced by selecting correct matches for estimation (RANSAC) or by downweighting incorrect matches (robust kernels). This article introduces both.
1. RANSAC: Random Sample Consensus
The goal is to select correct data from all observations for estimation. The following definitions are useful:
Point: one datum; in SLAM, usually a matched point pair. Outlier: an incorrect datum. Inlier: a correct datum. Inlier set: the set of inliers. Outlier set: the set of outliers. Model: the parameters to estimate. : the minimum number of points needed to estimate the model. : the set formed by all points.
The inlier set is the correct data we seek. RANSAC randomly chooses points from , estimates a model, and tests whether the remaining points agree with it. If most points agree, this is likely a suitable model and its agreeing points are inliers. Because the sample is random, one trial may fail, so multiple trials are necessary.
RANSAC procedure:
- Randomly select points from the data set and estimate a model.
- Measure the distance from every point to the model and obtain inlier set .
- If , re-estimate a more accurate model using all inliers.
- If , repeat step 1.
- After trials, choose the largest inlier set and re-estimate the model using all of its points.
RANSAC has three important parameters: , , and .
1.1 Inlier threshold
For a model, every point has a squared distance . Threshold identifies a point as an inlier or outlier. The definition of depends on the model: line fitting commonly uses the squared point-to-line distance; fundamental-matrix estimation uses squared point-to-epipolar-line distance; homography and camera-pose estimation use squared point-to-point distance.
The sum of squares of independent standard normal variables follows a chi-squared distribution with degrees of freedom. Choose so that an inlier has probability . Then
Usually . The corresponding values are shown below.

1.2 Number of samples
How many samples should be drawn before stopping? Let the probability of an inlier be and that of an outlier be .
Define event as: among samples, at least one selected subset contains no outlier. Its complement is that every selected subset contains at least one outlier. This gives
Two points are worth noting:
- The sample count depends on the inlier/outlier ratio, not on the number of observations.
- It increases as the minimum number of points increases.
1.3 Inlier-count threshold
When enough inliers have been found, sampling can stop early. Given an outlier fraction and observations, a common choice is . Estimate this fraction conservatively.
1.4 Adaptive sample-count selection
Equation (1) shows that depends on inlier probability , but the true number of inliers is unknown in advance.
Initialize , which gives . After a sample produces inliers, update , then recompute . At every iteration, update when a larger inlier count is found:
w = 0
sample_count = 0
while sample_count < N
Randomly select a sample and calculate its inlier count n_i.
if n_i increases
w = n_i / n
Update N from w.
sample_count = sample_count + 1
end
1.5 Example: fitting a 2D line
Model
The general equation of a line is
Dividing both sides by gives
Two points and determine a line. Substituting them gives
Solving gives
Since , , and are defined up to scale, set
This is the general line equation determined by two points.
Inlier distance
Use the point-to-line distance:
The threshold is .
Code
The following repository contains code for generating data and testing RANSAC:
2. Robust kernels
RANSAC estimates a model in two stages: first select inliers, then optimize with them. Robust kernels optimize the model directly in one stage, reducing the influence of incorrect observations by downweighting outliers. The usual objective is
Outliers generally have large errors or distances , which can badly bias the final result. A robust kernel transforms these errors to reduce the influence of excessively large terms. For example, the Huber kernel changes the objective to
For small , the original quadratic cost remains. For large , it becomes linear, reducing the effect of incorrect data. For implementation, transform the Huber function into an error weight. Find such that
which gives
References
- Multiple View Geometry.
- g2o: A General Framework for (Hyper) Graph Optimization.
Related code
More SLAM articles