RANSAC
From WikiPedia:
Random sample consensus (RANSAC) is an iterative method to estimate parameters of a mathematical model from a set of observed data which contains outliers. Therefore, it also can be interpreted as an outlier detection method.
Let's set up some random data points:
Now, fit a line to the data. We start with our model:
Or, in matrix notation:
Since we have an over-determined system, we use least squares to solve:
With those parameters in hand, let's plot the resulting line:
Scikit-image provides an N-dimensional LineModel object that encapsulates the above:
Instead of m
and c
, it parameterizes the line by origin
and direction
--- much safer when dealing with vertical lines, e.g.!
Now, we robustly fit the line using inlier data selecte with the RANSAC algorithm:
Exercise: Going interplanetary
The sun is one of the most spherical objects in our solar system. According to an article in Scientific American:
Earth's closest star is one of the roundest objects humans have measured. If you shrank the sun down to beach ball size, the difference between its north-south and the east-west diameters would be thinner than the width of a human hair, says Jeffery Kuhn, a physicist and solar researcher at the University of Hawaii at Manoa. "Not only is it very round, but it's too round," he adds. The sun is more spherical and more invariable than theories predict.
If the sun is spherical, we should be able to fit a circle to a 2D slice of it! Your task is to do just that, using RANSAC and scikit-image's CircleModel.
Let's start by loading an example image:
In this specific image, we got a bit more than we bargained for in the form of magnificently large solar flares. Let's see if some canny edge detection will help isolate the sun's boundaries.
The edges look good, but there's a lot going on inside the sun. We use RANSAC to fit a robust circle model.
The parameters of the circle are center x, y and radius:
Let's visualize the results, drawing a circle on the sun, and also highlighting inlier vs outlier edge pixels:
Exercise: CardShark
Your small start-up, CardShark, that you run from your garage over nights and evenings, takes photos of credit cards and turns them into machine readable information.
The first step is to identify where in a photo the credit card is located.
Load the photo
../images/credit_card.jpg
Using RANSAC and LineModelND shown above, find the first most prominent edge of the card
Remove the edge points belonging to the most prominent edge, and repeat the process to find the second, third, and fourth