Visualize Inliers and Outliers
Introduction: Seeing the Good and the Bad
Welcome to the second unit of our course. In the previous unit, you learned how to align images by calculating a homography matrix. You practiced extracting coordinates and using the RANSAC algorithm to find an accurate alignment while ignoring incorrect matches.
RANSAC does a fantastic job of filtering out bad matches, but as developers, we need to inspect what it accepted and what it rejected to ensure our alignment is actually sound. A homography might compute mathematically but practically represent a bad alignment.
In this lesson, we will learn how to extract the inliers (the good matches RANSAC kept) and the outliers (the bad matches it threw away) into separate lists. We will write a diagnostic tool to evaluate the alignment quality automatically, and then we will draw the results on the screen to visually confirm our math.
Extracting Inliers and Outliers
As a quick reminder, when we estimate a homography using RANSAC, the algorithm returns a matrix and a mask. This mask is essentially a list of 1s and 0s telling us exactly which matches fit the geometry (1) and which do not (0).
To make use of this mask, we need to split our original list of matches into two separate lists. We can achieve this cleanly in Python using the zip() function, which lets us loop through the matches and the mask at the same time.
Let's look at how we can use list comprehensions to separate our data:
In this code, zip(matches, inliers) pairs each match object m with its corresponding 1 or 0 value, which we call keep. If keep is 1 (which evaluates to True in Python), the match is added to our inlier_matches list.
We can apply the exact same logic to find the outliers by simply looking for the items we do not want to keep:
Output:
By separating them, we now have precise control over our data for both analysis and visualization.
