This project features the implementation of algorithms to stitch together disparate images into
a larger mosaic of the images, using both user-defined correspondences and automatic methods.
Part A: Image Warping and Mosaicing
For the first part of this project, we aim to warp and blend images using correspondence points
we indicate to merge multiple images into a mosaic.
Recovering Homographies
Using two sets of correspondence points
im1_points
and
im2_points
which are two \(n\times 2\) matrices, we can estimate a homography
between the correspondence points (a projective transformation). Remember
that homographies have 8 degrees of freedom, therefore we need at least 4 pairs of correspondence points
to determine a homography.
For each pair of points \(x_1, x_2\) and \(x_2, y_2\), we can find two linear equation constraints
they correspond to by the following argument:
$$H = \begin{bmatrix} a & b & c \\
d & e & f \\
g & h & 1 \end{bmatrix}$$
$$H\begin{bmatrix} x_1 \\ y_1 \\ 1 \end{bmatrix} = \begin{bmatrix} wx_2 \\ wy_2 \\ w \end{bmatrix}$$
Plugging in our unknown values from the matrix \(H\) and plugging in the value \(w\), we get the following equations:
$$ax_1 + by_1 + c - gx_1x_2 - hy_1x_2 = x_2$$
$$dx_1 + ey_1 + f - gx_1y_2 - hy_1y_2 = y_2$$
which gives the matrix equation:
$$Ah = \begin{bmatrix} x_1 & x_2 & 1 & 0 & 0 & 0 & -x_1x_2 & -y_1x_2 \\
0 & 0 & 0 & x_1 & x_2 & 1 & -x_1y_2 & -y_1y_2 \end{bmatrix} \begin{bmatrix} a\\b\\c\\d\\e\\f\\g\\h \end{bmatrix}
= \begin{bmatrix} x_2 \\ y_2 \end{bmatrix} = b$$
Thus, for every pair of correspondence points, we get two more rows for the matrix \(A\) and column vector \(b\).
If we have only 4 pairs of correspondence points, then \(A\) is invertible, thus \(h = A^{-1}b\).
If we have more than our system is overdetermined and we can use least-squares to find a solution
that minimizes the mean squared error of the homography, which gives us:
$$h = (A'A)^{-1}A'b$$
Warp the Images
Now given a homography, we can warp an image using the homography by using the concept of
an inverse warp similar to what we did in project 3. First, we map the corners of the original
image through the homography to find the bounds of the warped image. Then, we can use
polygon
to get all of the internal points of the warped images and warp them through the inverse of the
homography to get their coordinates in the original image. Each pixel values in the warped image is
set to its inverse warped pixel in the original image, with aliasing handled by using
griddata
.
Rectifying Images
Now that we have the means to calculate the homographies between images and can warp images
according to those homographies, we can use this to rectify images, aka take rectangular objects
that have been photographed at an angle, and warp the image so that the object is now rectangular.
Correspondence points are manually labeled using the provided correspondence tool from project 3.
Result for two images are below:
A poster of Benjamin Franklin.
|
Rectified poster.
|
My bed.
|
A rectified image of my bed,
rectifying one of the squares of the sheet pattern.
|
Blend the Images into a Mosaic
Now, by labeling correspondences between two images that we want to create a mosaic of, we
can warp one image into the projective plane of the other image to align them. By doing some
padding to the other image regarding the differences in dimension, we have solve the issue of
alignment.
In order to blend the images together, we use
morpohology.distance_transform_edt
on each image (with the original images barely brightened by 0.0001 to distinguish them from the
background), call the results
dist1
and
dist2
.
This gives a measure of how far each point is from the background.
Then, applying a logical operator of
dist1 > dist2
, we end up with a mask for
which areas of the combining image should be sampled from image 1 (1 - mask gives image 2).
Finally, we blend the two images using this mask by a two-level Laplacian stack, taking the
code that I wrote in from project 2 to get our final blended mosaic.
To showcase this algorithm, I find correspondence points and blend the following 3 sets of
images into mosaics, with results (as well as intermediate mosaics) shown below:
Bay Area Sunset from the Campanile (with Venus also visible in the sky!)
Set of 5 images. Blended in the order 3 -> 4 -> 2 -> 1 -> 5.
Intermediate results: