TL;DR
RANSAC, short for random sample consensus, fits a model such as a line or a plane to data that contain many outliers. It repeatedly picks the smallest random sample that can define the model, fits the model to that sample, and counts how many other points agree with it. The model with the largest agreeing set wins, and it is then refitted to all of its agreeing points. It matters because a few wild points can ruin an ordinary least-squares fit, while RANSAC ignores them by design.
Builds on: Random Sampling, Linear Regression, Binomial Distribution · Leads to: Epipolar Geometry, Visual SLAM
Where it's used
Camera motion from matched features. A visual SLAM system matches small image patches between two video frames. Repeated textures and moving objects produce many false matches. RANSAC fits the epipolar geometry, which describes how points move between two views, to the matches and keeps the consistent ones. Visual SLAM needs this step before it can build a map, and the ORB-SLAM system uses RANSAC during initialisation to estimate the homography and the fundamental matrix.
Stitching panoramas. Two overlapping photos are related by a homography, a 3 by 3 Matrix that maps each point in one photo to where it lies in the other. Feature matches include wrong pairs, and RANSAC estimates the homography from the consistent ones, so the photos line up without a visible seam.
Finding the ground in laser scans. A lidar on a vehicle returns a cloud of points from the road, from parked cars, from poles and from noise. Fitting a plane with RANSAC picks out the road, because most points lie on one flat surface and the rest are outliers. The same idea finds walls, floors and tables in indoor scans.
How it works
Take six points. Four lie on the line : A (0, 1), B (1, 3), C (2, 5) and D (3, 7). Two are outliers: E (2, 0) and F (4, 4). Set the threshold . A point is an inlier if its vertical distance to the line is at most 0.5.
-
Pick the smallest sample. A line needs two points, so draw two at random. Suppose the draw is E and F. Check: any two distinct points define exactly one line, so two is the minimum.
-
Fit a model to the sample. The line through E (2, 0) and F (4, 4) has slope and passes through (2, 0). So its equation is . Check: at the line gives , the height of F.
-
Count the points that agree. For each point, find the vertical distance to . For A it is , and B, C and D are also 5 away. Only E and F agree, so this trial has a consensus set of size 2. Check: a distance of 5 is ten times the threshold, so those points are clearly outliers for this model.
-
Keep the best and repeat. Record that the best consensus size so far is 2. Draw a new random sample, this time A and D.
-
Fit again. The line through A (0, 1) and D (3, 7) has slope and passes through (0, 1). So its equation is . Check: both points satisfy the equation.
-
Count again. A, B, C and D have distance 0. The distance for E is , and for F it is . The consensus set is {A, B, C, D}, of size 4. Check: four points agree, against two for the earlier trial, so this model is kept.
-
Refit on the consensus set. The four inliers lie exactly on , so an ordinary least-squares fit to them returns slope 2 and intercept 1. E and F never influence the answer. Check: with noisy real data the inliers do not lie exactly on the line, so the refit is close to the truth rather than exact.
The loop in one picture:
flowchart LR
A[Draw a minimal random sample] --> B[Fit the model to the sample]
B --> C[Count inliers within the threshold]
C --> D{Largest consensus so far?}
D -- yes --> E[Keep this model]
D -- no --> A
E --> F{Enough trials?}
F -- no --> A
F -- yes --> G[Refit by least squares on the inliers]
- best model, y = 2x + 1
- bad trial, y = 2x - 4
- inliers
- outliers
The math
Given data points , a model with parameters, and a threshold , RANSAC runs trials. In each trial it draws points at random, fits the model to them, and computes for each point a residual , the distance from the point to the fitted model. The inlier set is . The trial's score is the size of . The model with the largest is kept, and the final model is the least-squares fit to its inliers.
For a line, has parameters, and the vertical residual of point is .
Step 3 in symbols: the consensus set
For the bad trial, and . The residual of A is . Because , A is not an inlier of that model. The consensus set is , with .
Step 4 in symbols: how many trials are needed
Let be the inlier fraction, the share of all points that are inliers. In the example . A sample of points is clean when every point in it is an inlier. It is clean with probability , because each point is drawn independently.
Each trial is independent, so the probability that all trials fail to draw a clean sample is . We want this to be at most , where is the chance of success we are willing to accept.
Taking logarithms gives
Both logarithms are negative, so the ratio is positive. The number of clean samples in trials is a binomial count with success probability , and the failure probability above is the chance that this count is zero.
Step 5 in symbols: the final refit
The final model is an ordinary Linear Regression fit, but only the inliers enter. It minimises the sum of squared residuals over :
Outliers are excluded from this sum, so they cannot pull the final line toward them.
Worked examples
Take , and . How many trials are enough?
- The chance one sample of two is clean is .
- The chance one trial fails is .
- We need . Taking logarithms, and .
- The ratio is . Round up, so .
- Check: , which is just above 0.01, and , which is below it.
Interpretation: with half the points as outliers, 17 random pairs give a 99 percent chance that at least one pair is clean.
Take , and .
- The chance one pair is clean is .
- The chance one trial fails is .
- Compute .
- Round up to .
- Check: , which is above 0.01, so four trials are not enough. , which is below it.
Interpretation: when most points are inliers, a handful of trials is enough, and the cost of RANSAC is small.
A panorama tool matches image features between two photos. It fits a homography, which needs points, because a homography has 8 free numbers and each point supplies 2 equations. Suppose only of the matches are correct, and we want .
- A clean sample of four has probability .
- The chance one trial fails is .
- Compute .
- Round up to .
- Check: , just above 0.01, and , just below it.
Interpretation: at 30 percent correct matches, 567 trials are needed. The cost comes from both the low inlier fraction and the sample size of four. Better matching, which raises , cuts the number of trials sharply. At , the same calculation gives 72 trials.
Try it: the same 30 percent of correct matches, but fitting a line, which needs . Find for .
Answer: , so . Then and , so . The sample size dominates: halving from 4 to 2 cuts the trials from 567 to 49.
Common mistakes and tradeoffs
It is tempting to think RANSAC finds the model that fits all the data best. In fact it finds the model with the largest consensus set, the model that agrees with the most points within the threshold. Only the final refit uses least squares, and it uses only the inliers.
It is tempting to think more trials always give a better answer. In fact the number of trials only controls the chance of drawing a clean sample. The threshold controls what counts as agreement. A threshold that is too large lets outliers count as inliers, and one that is too small rejects true inliers that have noise. Choosing from the known noise level of the sensor is usually more important than adding trials.
It is tempting to think the inlier fraction is the outlier fraction. In fact is the share of inliers, so a dataset that is 70 percent outliers has . Using the wrong one changes by a large factor.
It is tempting to think RANSAC gives the same answer every time. In fact it is randomised. With the chosen , the chance of a wrong answer from a single run is at most , but two runs can return slightly different models when the data are noisy.
When to use it: when outliers are common, the model has few parameters, and the noise level can be estimated so that a threshold makes sense. When not to use it: when outliers are rare and least squares is already stable, or when the model has many parameters, because the number of trials grows quickly with . Alternatives include robust M-estimators, which down-weight large residuals instead of discarding them, and the Hough transform, which votes for model parameters and suits low-dimensional lines in images.
History
Before RANSAC, the standard tool for finding lines and shapes in images was the Hough transform, which lets each point vote for every model it could belong to. Its voting grows expensive as the number of model parameters rises. In 1981 Martin Fischler and Robert Bolles, working at SRI International, published RANSAC in Communications of the ACM. They proposed fitting models to small random samples and counting agreement, which kept the cost manageable for the models of the time and tolerated a large share of outliers.
RANSAC caught on because it is simple to state, works with little knowledge of where the outliers are, and fits well with the random sampling that computer vision and robotics later relied on. It became a standard step in camera geometry and remains one, with many later variants that score models differently.
Recap
- RANSAC fits a model to a small random sample, counts the points that agree within a threshold, and repeats.
- The best model is the one with the largest consensus set. It is then refitted by least squares on its inliers.
- The number of trials follows from the inlier fraction , the sample size and the success probability , through .
- The sample size is the main cost driver: a homography needs four points, a line needs two.
- Read next: Epipolar Geometry, which RANSAC estimates from feature matches, and Visual SLAM, which uses it at every new frame.
Check yourself
QuestionRecall. What is a consensus set, and what does RANSAC do with the best one at the end?Show answer
A consensus set is the set of points whose residual to a fitted model is within the threshold, the inliers of that model. RANSAC keeps the model with the largest consensus set, then refits it by least squares using only those inliers.
QuestionComputation. A plane needs points. The inlier fraction is , and the success probability is . How many trials are needed?Show answer
The chance a sample of three is clean is , so the chance a trial fails is . Then . Round up to .
QuestionWhy. Why does RANSAC draw a small random sample instead of fitting to all the points and then removing the ones that look like outliers?Show answer
When outliers are common, a fit to all points is already pulled toward them, so the residuals that would reveal the outliers are themselves distorted. A small sample has a chance of containing no outliers at all, and a model fitted to such a sample is not distorted. The method trades many cheap trials for one trial that is clean.
QuestionApplied. A robot's lidar sees a wall, and 40 percent of its points come from a person walking in front of the wall. The team fits a plane with RANSAC. What inlier fraction and sample size apply, and what must the threshold be set to?Show answer
The wall points are the inliers, so and for a plane. The person's points are outliers because they lie off the plane. The threshold should be set above the sensor's noise on the wall, so that the wall's points count, and below the distance at which the person's points lie off the wall, so that they do not. With about 13 trials are needed. The fitted plane should then be checked against the scan, because the person may be close enough to the wall to count as inliers.
Sources
- Random sample consensus. Wikipedia. https://en.wikipedia.org/wiki/Random_sample_consensus
- Fischler, M. A. and Bolles, R. C. (1981). Random Sample Consensus: A Paradigm for Model Fitting with Applications to Image Analysis and Automated Cartography. Communications of the ACM. https://doi.org/10.1145/358669.358692