How RANSAC fits a model to inliers

Short answer: RANSAC fits a geometric model when many points are outliers. It draws the smallest sample that determines the model, fits, and counts how many points agree within a threshold. After enough trials it refits on the largest inlier set. The number of trials needed to see at least one clean sample with probability p is log(1−p) / log(1−w^s), where w is the inlier fraction and s is the sample size. The algorithm is Monte Carlo. It can miss the inliers.


What the problem actually asks

Least squares uses every point, so one distant outlier drags the fit. If the model is determined by s points, a sample that happens to be all inliers produces a hypothesis close to the truth, and the other inliers will agree. Outlier samples produce hypotheses that few points agree with. Counting agreements separates those cases without a prior clustering step.

The trial count comes from a coin-flip argument. The chance a sample is clean is w^s if points are drawn independently and w is the inlier ratio. The chance every trial is dirty after N trials is (1−w^s)^N. Set that below 1−p and solve for N.


What the algorithm guarantees

When the inlier ratio and the threshold match the data, N from the formula yields at least one all-inlier sample with probability at least p, and the largest consensus set is then a reasonable refit. The threshold is a parameter, not a theorem. A too-wide threshold treats outliers as inliers. A too-narrow one rejects true noise. Extra memory is the current best inlier set. Each trial costs a fit on s points and a scan of the data.

The formula assumes sampling with the stated independence and a known w. If w is overestimated, N is too small and the success probability drops. Adaptive RANSAC updates N from the best consensus seen so far. That is the practical version.


The working steps

  1. Choose the model, the sample size s, the inlier threshold, and a success probability p. Estimate the inlier ratio w, or update it adaptively.
  2. Set N = log(1−p) / log(1−w^s), and cap it at a budget.
  3. Each trial draws s points, fits the model, and counts points within the threshold.
  4. Keep the model with the largest count. After the trials, refit the model on that consensus set, often by least squares.
  5. If a later trial beats the consensus, recompute N from the new implied w and continue until the updated N is reached.

The probability is about seeing a clean sample, not about the refit being the maximum-likelihood model under a noise law. A single trial is not RANSAC. Least squares on all points is the right fit when there are no outliers. The threshold has to be in the same units as the residual.


Approaches that look simpler and fail

Approach Why it fails when the trial count ignores the sample size
Run a fixed handful of trials for a model that needs many points w^s shrinks quickly as s grows. A line, s = 2, needs far fewer trials than a model with s = 8 at the same inlier ratio.
Fit least squares on every point and then delete large residuals once One pass can be pulled so far that true inliers look like outliers. RANSAC hypothesizes from clean samples first.
Treat the consensus count as a probability The count ranks hypotheses. The probability p is a statement about the number of trials, computed from w before or during the loop.
Skip the final refit The hypothesis came from s points. The consensus set contains more information, and the refit uses it.

A fit with no outliers is ordinary least squares. A robust loss that downweights residuals without sampling is an M-estimator. RANSAC is the random-sample consensus loop.


A working example

This Python fragment draws the smallest sample that defines a line, counts inliers, and refits on the largest consensus set. Four points lie on y = 2x and two are far away. The fitted slope prints 2.0.

import random
pts = [(0, 0), (1, 2), (2, 4), (3, 6), (0, 5), (3, 0)]
rng = random.Random(0)
best = None
for _ in range(40):
    p, q = rng.sample(pts, 2)
    if p[0] == q[0]:
        continue
    slope = (q[1] - p[1]) / (q[0] - p[0])
    intercept = p[1] - slope * p[0]
    inliers = [r for r in pts if abs(r[1] - (slope * r[0] + intercept)) < 0.5]
    if best is None or len(inliers) > len(best[0]):
        best = (inliers, slope)
xs = [p[0] for p in best[0]]
ys = [p[1] for p in best[0]]
n = len(xs)
mean_x, mean_y = sum(xs) / n, sum(ys) / n
num = sum((x - mean_x) * (y - mean_y) for x, y in zip(xs, ys))
den = sum((x - mean_x) ** 2 for x in xs)
slope = num / den
assert abs(slope - 2) < 1e-6
print(f"{slope:.1f}")

Where it is used

OpenCV findHomography and image-stitching pipelines fit a model with RANSAC: draw a minimal sample, keep the fit with the most inliers. Structure-from-motion uses the same loop. Least squares on every point is pulled off the model by one bad match.


When you can drop this algorithm

Drop RANSAC when the data are clean. A direct fit is simpler and deterministic. Keep it when a large fraction of points do not belong to the model, and set N from the formula rather than from a fixed guess.


Why this stays published

We publish this so a search for RANSAC returns the trial-count formula and the consensus refit that follows the best sample. RANSAC fits a model in contaminated data by repeating minimal random samples and keeping the hypothesis with the most inliers, using a trial count set by the inlier ratio.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *