The RANSAC (Random sample and consensus) algorithm is the gold standard in eliminating noise. A while ago, I wrote an article on how the RANSAC algorithm is implemented for finding the model of a straight line in a noisy field of points. The RANSAC algorithm in its original form was developed around finding straight line models when presented with noisy visual data. In this article, I will explore how to implement the RANSAC algorithm a circle model in a noisy set of points. I hope that by the end of this article you will appreciate how beautiful RANSAC is. Sometimes, I get carried away and visualize that human brain is perhaps doing some sort of RANSAC deep inside. Enough!
We have a mix of inliers (black) and outliers (red). We want to find the model of the straight line which fits the inliers
The human mind can easily eliminate the outliers.
The Least Squares method will consider all the points with equal importance. We can clearly see that the resulting line is visibly skewed towards the outliers.
The example shown above exhibits how noise can impact the outcome of the least squares fitting algorithm. The human mind can easily spot the outlier, but the least squares algorithm cannot. This is where RANSAC steps in. RANSAC is a simple voting based algorithm that iteratively samples the population of points and find the subset of those lines which appear to conform.
Consider the points shown below. It is not difficult to spot there is a nice circle that can be traced out of the points only if we exclude the 2 noisy points on the far right of the visual.
The output after finding the best fitting circle is presented below. Notice that the position of the circle has shifted towards the outliers
The output after using RANSAC to take into account the outliers. Notice that the algorithm has nicely detected the noisy points on the far right. The RANSAC algorithm has ignored the outliers completely.
Model circle: The candidate circle which we think fits the points
Threshold distance: Used for creating a doughnut like concentric circles with the model circle in the middle
Outlier point: Any point outside the doughnut region
Inlier point: Any point within the doughnut region
Threshold inlier count: Minimum number of inliers points for a candidate circle to be selected
Mean absolute error:
1/N X Σabs(distance of inlier point from candidate center - radius of candidate center)
N=count of all inlier points
Consider the following arrangement of points. We will use this example for understanding the RANSAC algorithm
RANSAC for straight lines relies on least squares algorithm to find the line which fits a set of points. When I started implementing RANSAC for circles, I was unsure of what would be the best mathematical approach to fit a circle to a set of points. Unlike the least squares method for lines, the equivalent approach for circles is non-linear and hard to solve without an interative approach I used the Gradient Descent Approach and this worked well. However, GD is an iterative approach and fairly expense. In the later stages I learnt of Randy Bullock's approach which reduces the non-learn optimization problem to a liner problem
The folder hierarchy is as follows: