Implement a RANSAC algorithm to fit a line to a set of points. The goal is to find the best line that represents the majority of the points, despite the presence of outliers.
The RANSAC algorithm is a model fitting technique that uses a random sampling approach to find the best model. It is based on the idea of selecting a random subset of points, fitting a model to these points, and then checking how well this model fits the rest of the points.
Select a random subset of 2 points
Fit a line to these points using the equation y=mx+b
Count the number of inliers, points that are close to the fitted line: a point (x,y) is an inlier when its perpendicular distance m2+1∣mx−y+b∣ is strictly less than threshold
Repeat the process for a specified number of iterations, keeping the line with the most inliers (a later line replaces it only if it has strictly more)
Seed the random number generator with seed before sampling. Skip any sampled pair with equal x coordinates (a vertical line has no slope m).
y=mx+b
This technique is widely used in computer vision for tasks such as line detection and image registration.
points are four (x,y) positions; iterations = 100 random draws, threshold = 0.5 is the inlier distance and seed = 42 makes the draws repeatable.
A pair fixes a line: m=x2−x1y2−y1, b=y1−mx1. For (0,0) and (2,2): m=2/2=1, b=0, i.e. y=x.
Distances to y=x are 2∣x−y∣: 0 for the three diagonal points and 4/2≈2.828 for (1,5), so 3 inliers.
A line through the outlier does worse: (0,0),(1,5) gives y=5x, where (1,1) is 4/26≈0.784 away, above 0.5, so only 2 inliers. (2,2),(1,5) gives y=−3x+8, also 2. The pair (1,1),(1,5) is vertical and is skipped.
Of the 6 possible pairs, 3 produce y=x, so 100 draws certainly hit it and nothing beats 3 inliers: output [1.0, 0.0]. Notice the outlier never moves the answer, which is the point of RANSAC.
RANSAC Line Fitting: Background and Implementation Guide
Background Knowledge
RANSAC (Random Sample Consensus) is a robust statistical method for fitting models to data contaminated with outliers. Unlike traditional least-squares fitting, which minimizes error across all points and is heavily influenced by outliers, RANSAC iteratively identifies the largest subset of points that fit a model well. The algorithm assumes that a majority of your data points are inliers (valid observations) and that outliers can be safely ignored. This makes it particularly valuable in computer vision and robotics applications where noisy sensor data is common.
The core insight of RANSAC is that you can estimate a model from a minimal set of points (the minimum required to define that model) and then test how well this model explains the entire dataset. For line fitting, you need exactly 2 points to define a line. By randomly sampling different pairs of points, fitting lines to each pair, and evaluating how many points lie close to each fitted line, you can find the line that best represents the underlying structure in your data, even when significant noise is present.
The algorithm trades computational cost for robustness: it requires multiple iterations to have high confidence of finding a good model, but each iteration is computationally cheap. The number of iterations needed depends on the outlier ratio—higher outlier percentages require more iterations to guarantee finding a good sample.
Algorithm/Approach
RANSAC for line fitting follows this general pattern:
Initialization: Set the number of iterations k (typically calculated based on desired confidence and estimated outlier ratio)
Iteration Loop: Repeat k times:
Randomly sample 2 points from your dataset
Fit a line model to these 2 points
Evaluate the model by counting inliers (points within a distance threshold of the line)
If this model has more inliers than the best model found so far, update the best model
Return: The model with the maximum number of inliers
The key parameters are:
Distance threshold: How close a point must be to the line to count as an inlier
Number of iterations k: How many random samples to try
Random seed: For reproducibility in testing
Step-by-Step Strategy
Step 1: Understand Line Representation
Represent a line using the form ax+by+c=0 (normalized so that a2+b2=1). Given two points (x1,y1) and (x2,y2), you can derive the line coefficients. Alternatively, use the distance formula from a point to a line to evaluate inliers.
Step 2: Implement Point-to-Line Distance
Calculate the perpendicular distance from a point to a line. For a line ax+by+c=0 and point (x0,y0):
distance=a2+b2∣ax0+by0+c∣
Step 3: Implement the Main Loop
best_model = None
best_inlier_count = 0
for iteration in range(k):
# Sample 2 random points
# Fit line to these points
# Count inliers within threshold
# Update best_model if needed
Step 4: Random Sampling
Use the provided random seed to ensure reproducibility. Randomly select 2 distinct indices from your dataset without replacement.
Step 5: Model Evaluation
For each fitted line, iterate through all points and count how many fall within the distance threshold. Track the model with the highest inlier count.
Common Pitfalls
Duplicate points in sampling: Ensure you select 2 different points. Sampling the same point twice will not define a valid line.
Numerical instability: When two points are very close or nearly vertical/horizontal, line fitting can become numerically unstable. Normalize your line equation properly.
Threshold sensitivity: The distance threshold dramatically affects results. Too small and you'll have few inliers; too large and the model becomes meaningless. This is typically a tunable parameter.
Insufficient iterations: If k is too small, you may never sample a good pair of inliers. The required k grows exponentially with outlier ratio.
Forgetting the random seed: Always set the random seed at the start for reproducible results during testing and debugging.
Off-by-one errors: Be careful with loop bounds and array indexing, especially when sampling and counting inliers.
Time & Space Complexity
Time Complexity: O(k⋅n) where k is the number of iterations and n is the number of data points. For each iteration, you sample 2 points (constant time) and evaluate all n points against the fitted line (linear time).
Space Complexity: O(n) for storing the input data points and O(1) additional space for the best model parameters (typically 3 coefficients for a line equation).
In practice, k is often set to a small constant or calculated as k=log(1−p)/log(1−(1−\epsilon)m) where p is the desired confidence, ϵ is the estimated outlier ratio, and m is the minimum sample size (2 for lines). This ensures high probability of finding a good model without excessive computation.