Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Ask two points, then count the votes
Suppose half your points are junk. Any fit that listens to everyone is ruined. RANSAC plays a guessing game instead: pick two points at random, draw the line through them, and count how many other points lie close to it. A line through two junk points gets few votes; a line through two good points gets many. Repeat a few dozen times, keep the line with the most votes, and refit it using only its voters.
RANSAC is everywhere in robotics and vision: finding the ground plane in lidar scans, estimating camera motion from feature matches (where many matches are wrong), fitting lines to lane markings, and aligning star-tracker measurements to a catalogue. It is built into OpenCV, PCL and scikit-learn.
- 1. RANSAC picks the hypothesis with…
- 2. To fit a line, each RANSAC trial samples…
- 3. RANSAC can work with…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.One trial by hand
Points: (0, 1), (1, 3), (2, 5), (3, 7), (4, 0), (5, 20). The true line is y = 1 + 2x; the last two are outliers. Threshold 0.5.
| Sampled pair | Line | Points within 0.5 | Votes |
|---|---|---|---|
| (0, 1), (2, 5) | y = 1 + 2x | (0,1), (1,3), (2,5), (3,7) | 4 |
| (1, 3), (4, 0) | y = 4 − x | (1,3), (4,0) | 2 |
| (3, 7), (5, 20) | y = −12.5 + 6.5x | (3,7), (5,20) | 2 |
The first pair wins with 4 votes; the refit on its 4 inliers is exactly y = 1 + 2x. Least squares on all six points gives slope ≈ 2.51 and intercept ≈ −0.29: the two outliers tilt it.
How many trials? With inlier fraction w and sample size s, one trial is all-inlier with probability wˢ. For a 99% chance of at least one clean sample:
N = log(0.01) / log(1 − wˢ).
With w = 0.5 and s = 2: N = log 0.01 / log 0.75 ≈ 16.0, so 17 trials.
The steps in plain English:
- Draw a random minimal sample (2 points for a line, 3 for a plane).
- Fit the model exactly to it.
- Count the points whose residual is below the threshold (the consensus set).
- Keep the model if its consensus set is the largest so far; update the number of trials needed from the inlier fraction it implies.
- Stop after enough trials, then refit by least squares on the best consensus set.
- 1. Trials for 99% confidence, w = 0.5, s = 3?
- 2. The final RANSAC model is usually…
- 3. Minimal sample size for a plane z = ax + by + c?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.The algorithm
Input: data, a model that can be fitted from s points, a threshold t, a confidence p (e.g. 0.99), and a maximum number of trials.
best = None; N = max_trials; k = 0
while k < N:
k += 1
S = random sample of s points
θ = fit(S) # exact fit to the minimal sample
C = {i : |residual_i(θ)| ≤ t} # consensus set
if |C| > |best_C|:
best_C = C
w = |C| / n
N = min(max_trials, log(1 − p) / log(1 − w^s)) # adaptive stopping
θ_final = least_squares_fit(best_C)Threshold t. Set it from the inlier noise level: for Gaussian residuals with σ known, t ≈ 2–3σ. If σ is unknown, use k × MAD of the residuals from a preliminary robust fit. scikit-learn’s default is the MAD of y.
Adaptive stopping. The inlier fraction is unknown in advance. Each time a larger consensus set is found, the estimate of w goes up and the required N goes down. Exercise 1 needs 11 trials at 45% outliers.
from sklearn.linear_model import RANSACRegressor, LinearRegression
model = RANSACRegressor(LinearRegression(), residual_threshold=0.6, max_trials=1000, stop_probability=0.99, random_state=0).fit(X, y)
print(model.estimator_.coef_, model.inlier_mask_.sum(), model.n_trials_)
# Vision: cv2.findHomography(src, dst, cv2.RANSAC, 3.0); point clouds: Open3D segment_plane(distance_threshold=0.05, ransac_n=3)Use it when a large or unknown share of the data are gross errors (including bad x values), the inlier noise scale is roughly known, and the model has few parameters. Avoid it when s is large (wˢ becomes tiny), or the inliers do not form one clearly dominant group.
- 1. A larger threshold t…
- 2. Adaptive stopping lowers N when…
- 3. Why does RANSAC suffer when s is large?
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.Analysis and variants
Trial count. P(no clean sample in N trials) = (1 − wˢ)ᴺ. Setting it to 1 − p gives N = log(1 − p)/log(1 − wˢ) ≈ −log(1 − p)/wˢ for small wˢ. For w = 0.3 and s = 3, N = 169; for s = 8 (the eight-point algorithm), N ≈ 70 000.
Cost. Each trial fits s points and scores n residuals, so the total is O(N(f(s) + n)). Scoring dominates; early termination tests (Chum and Matas’s T(d,d) test) score a random subset first.
Breakdown. RANSAC can handle more than 50% outliers, as long as the outliers do not form a larger consistent structure than the inliers. Its answer is random, and for finite N it fails with probability 1 − p.
Variants:
- MSAC scores each point by min(r², t²) instead of 0/1, so better-fitting hypotheses win ties.
- MLESAC maximises a mixture likelihood (Gaussian inliers plus uniform outliers).
- PROSAC samples high-quality matches first.
- LO-RANSAC runs a local optimisation (inner RANSAC or IRLS) on each new best model.
- Preemptive RANSAC scores a fixed set of hypotheses in parallel for real-time use.
- MAGSAC removes the hard threshold by marginalising over noise scales.
Comparison:
| Least squares | Huber (M-estimator) | RANSAC | |
|---|---|---|---|
| Breakdown point | 0 | 0 (leverage) | can exceed 50% |
| Needs threshold | no | δ in units of scale | yes (absolute) |
| Deterministic | yes | yes | no |
| Efficiency on clean data | best | 95% | lower before refit |
Interview questions.
- How many trials for 50% outliers and a homography (s = 4) at 99%? log 0.01 / log(1 − 0.0625) ≈ 71.
- Why refit at the end? The minimal-sample model uses only s points and is noisy; least squares on all inliers is much more precise.
- 1. MSAC differs from RANSAC by…
- 2. N for w = 0.3, s = 3, p = 0.99?
- 3. RANSAC fails when…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Finding the ground in a cluttered scan
History. Fischler and Bolles introduced RANSAC in 1981 to solve the location-determination problem, computing a camera’s pose from landmarks with some false matches. Torr and Zisserman (2000) introduced MSAC and MLESAC. Chum, Matas and Kittler (2003) introduced LO-RANSAC, and Chum and Matas (2005) introduced PROSAC. Raguram et al. (2013) unified the variants as USAC.
Exercise 1. 200 points on y = 2 + 0.5x with σ = 0.2, of which 45% are replaced by random points (x in 0–30, y in −10–20). Threshold 0.6.
- RANSAC gives β = (1.951, 0.507) after only 11 trials (adaptive stopping).
- Least squares gives slope 0.042.
- The final consensus set has 121 points, only 3 of them outliers.
Exercise 2: ground-plane detection. Setup: a lidar scan of 600 points on the ground z = 0.3 + 0.08x − 0.04y (σ = 2 cm). A fraction of points hit 6 boxes up to 1.5 m tall. Fit by least squares, Huber regression (δ = 1.345) and RANSAC (threshold 5 cm), 30 scans per fraction.
| Obstacle fraction | Least squares | Huber | RANSAC | RANSAC trials |
|---|---|---|---|---|
| 0% | 0.02° (100%) | 0.02° (100%) | 0.04° (100%) | 3 |
| 20% | 0.65° (87%) | 0.08° (100%) | 0.05° (100%) | 9 |
| 40% | 1.18° (30%) | 0.44° (83%) | 0.04° (100%) | 25 |
| 60% | 1.56° (33%) | 1.29° (37%) | 0.05° (100%) | 78 |
| 70% | 2.04° (13%) | 1.93° (30%) | 0.08° (100%) | 185 |
The cells show the median tilt error of the estimated plane, with the share of scans within 1° in brackets.
- RANSAC stays within 0.1° up to 70% obstacles. Least squares and Huber are pulled up by the boxes.
- Huber helps at moderate contamination (20–40%), but at 60% and above the obstacles are the majority and it fails.
- The adaptive trial count grows with the contamination, as the formula predicts: about 185 trials at 70% versus 169 from theory.
- On clean data RANSAC is slightly less precise (0.04° versus 0.02°): its threshold excludes a few good points, and the result depends on the random samples.
RANSAC in AI, robotics and aerospace
- Ground and obstacle segmentation in lidar point clouds for rovers and autonomous cars.
- Visual odometry and SLAM: essential-matrix and pose (PnP) estimation from feature matches.
- Star trackers and terrain-relative navigation: matching observed features to a catalogue or map despite false detections.
- Image stitching and mapping: homographies between overlapping images.
References
- M. A. Fischler, R. C. Bolles (1981). “Random sample consensus: a paradigm for model fitting with applications to image analysis and automated cartography.” Communications of the ACM 24(6).
- P. H. S. Torr, A. Zisserman (2000). “MLESAC: a new robust estimator with application to estimating image geometry.” Computer Vision and Image Understanding 78(1).
- O. Chum, J. Matas, J. Kittler (2003). “Locally optimized RANSAC.” DAGM Symposium.
- R. Raguram, O. Chum, M. Pollefeys, J. Matas, J.-M. Frahm (2013). “USAC: a universal framework for random sample consensus.” IEEE TPAMI 35(8).
- R. Hartley, A. Zisserman (2004). Multiple View Geometry in Computer Vision, 2nd ed., section 4.7. Cambridge University Press.