Estimating Homography with RANSAC
Introduction to Homographies and Alignment
Welcome to the first unit of our course, "Estimate Geometry Using Homographies and RANSAC with Python"! Over the next four units, we are going to learn how to combine overlapping photographs to create a single, wide panorama.
To build a panorama, we need to mathematically align overlapping images. Imagine taking a map, tearing it into two pieces, and then trying to tape them back together. To align them correctly, you look for matching landmarks on both pieces.
In computer vision, the mathematical rule that aligns these two pieces is called a homography. A homography is a transformation matrix that maps points in one image to their corresponding points in another. By calculating this matrix, we can warp and shift our photos so they overlap perfectly.
It is important to note that a homography is the correct model in two specific scenarios: when the scene is roughly planar (like a flat wall) or when the camera undergoes pure rotation around its center (like panned shots on a tripod). If you move the camera's position in a scene with objects at many different depths, you encounter parallax, which can prevent a single homography from perfectly aligning the entire image. In this lesson, we will focus on exactly how to create this homography matrix using Python.
Handling Raw Matches
Before we can calculate our homography math, we need to find overlapping points. Imagine our code has already used some helper functions (which we will call detect_and_compute and match_descriptors) to look at two photos, identify interesting spots like corners or edges, and pair them up.
However, we face a big problem: raw matches are often imperfect. The computer might mistakenly match a cloud in the left image to a completely different cloud in the right image. These bad matches are called "outliers." We cannot just trust all of our matches to build our map. We need a smart way to calculate our homography while completely ignoring the bad data.
Extracting Coordinates from Matches
To figure out our alignment, we first need to extract the exact pixel (x, y) coordinates of our matched points. The match objects provide an index, which we use to look up the actual (x, y) coordinate.
Let's begin writing a function called matched_points. First, we pull out the coordinates using Python list comprehensions.
In this code:
kp1andkp2are lists of "keypoints" (the interesting spots) for the left and right images.m.queryIdxgives us the index of the point in the first image (the source).m.trainIdxgives us the index of the matched point in the second image (the target)..ptgrabs the actual(x, y)pixel coordinates.
Next, we need to format these lists for OpenCV. OpenCV requires these points to be wrapped in a specific NumPy format.
Here, we wrap our lists in np.float32 because OpenCV requires 32-bit decimal numbers. We then use .reshape(-1, 1, 2) to structure the data into a 3D array format. This tells OpenCV that we have a list of single points, and each point has 2 values (an X and a Y).
