Tune the Alignment
Introduction: Tuning "Magic Numbers"
Welcome to the fourth and final unit of our course! Throughout the previous units, we have built a solid foundation. You learned how to extract features, use RANSAC to calculate a homography matrix while filtering out bad matches, and warp images into a shared coordinate frame to stitch them together.
Up until now, whenever we matched features or ran RANSAC, we used fixed numbers for our settings — like a match ratio of 0.75 or a RANSAC threshold of 5.0. In programming, we often call these hardcoded values "magic numbers."
The problem with magic numbers is that the same image pair might align perfectly or fail completely depending on these settings. In the real world, you need to adjust them. In this lesson, we will build a parameter sweeper — a tool that automatically tests different combinations of these settings to help us find the perfect balance for our alignment.
Recall: The Feature Pipeline
Before we start tuning our settings, we need to load our images and extract their features. Since we already covered these steps in previous lessons, we will use our custom helper functions to handle this quickly.
As a quick reminder, detect_and_compute finds the interesting points (keypoints or kp) and describes what they look like (descriptors or des). We do this for both images so we can compare them. With our features ready, we can focus entirely on the tuning process.
The Two Key Parameters: Ratio and RANSAC Threshold
When we try to align two images, there are two main parameters that control our success:
- Ratio: This controls how strict we are when matching descriptors between the two images. A lower
ratio(like0.65) means we are very strict and only keep matches we are highly confident about. A higherratio(like0.85) is more relaxed and will give us more matches, but many of them might be incorrect. - RANSAC Threshold: This controls how much geometric error we tolerate. It is measured in pixels. If we set it to
3.0, a matched point must land within3pixels of its expected location to be considered a good match (an "inlier"). If we set it to8.0, we allow up to8pixels of error.
Our goal is to find settings that produce a high percentage of inliers, not just the largest total number of matches. To do this, we need to test multiple combinations of both parameters.
