[Auto]Stitching Photo Mosaics

Catherine Chu

Part A: Image Warping and Mosaicing

Recover Homographies

To start, correspondences are labeled among the pictures taken. In order to warp an image to align with its reference image, homography H (defining transformation p'=Hp) is recovered. This step is implemented in the function H = computeH(im1_pts, im2_pts). To ensure the stability of H, more than 4 correspondences are used to produce an overdetermined system, which is solved with least squares.

left_porch_pts
Porch (Left)
right_porch_pts
Porch (Right)
left_hallway_pts
Hallway (Left)
right_hallway_pts
Hallway (Right)
top_shelf
Shelf (Top)
bottom_shelf
Shelf (Bottom)

Warp the Images

Next warpImage(im, H) leverages the recovered homography H to perform inverse warping. The size, or bounding box, of the warped image is predicted by piping the 4 corners of the original image through H. Then, the bounding box is inverse warped onto the original image, where an alpha mask drops "invalid" (x,y)'s outside of the original image's dimensions. Finally, this warped image's values are resampled with scipy's RegularGridInterpolator.

Image Rectification

To test the above functions, "rectification" is performed on two example images. In both cases, the post-it note is "made rectangular" using a homography.

pi1
Original Image
pi1_rect
Rectification
pi1_rect_cropped
Cropped
pi2
Original Image
pi2_rect
Rectification
pi2_rect_cropped
Cropped

Blend the images into a mosaic

The images are warped to create an image mosaic. Specifically, the image on the left is warped in reference to the image on the right, which is positioned accordingly.

left_rect
Porch (Left, Warped)
right_rect
Porch (Right, Positioned)
left_hw_warp
Hallway (Left, Warped)
right_hw_pos
Hallway (Right, Positioned)
top_sh_warp
Shelf (Top, Warped)
bot_sh_pos
Shelf (Bottom, Positioned)

The images need to be blended together into a single image. A naive approach involves setting alpha to 0.5 in the overlapping region:

naive_blend
Porch
hw_naive_blend
Hallway
sh_naive_blend
Shelf

A second approach involves the distance transform, where alpha is 1 at the center of the original images, then decreases to 0 at their edges. In the overlapping region, the left image's alpha is set to 1 if its distance transform is greater than the right image's distance transform, and vice versa.

left_dist_tr
Left Porch's bwdist
right_dist_tr
Right Porch's bwdist
hw_left_dist_tr
Left Hallway's bwdist
hw_right_dist_tr
Right Hallway's bwdist
sh_top_dist_tr
Top Shelf's bwdist
sh_bot_dist_tr
Bottom Shelf's bwdist

Moreover, the 2-band blending technique (with a 2-level image pyramid) can remedy wedge-like artifacts:

Porch Results...

final_blend
low_blend
Low Frequency Blend
high_blend
High Frequency Blend

Hallway Results...

hw_final_dist_tr_blend
hw_low_blend
Low Frequency Blend
hw_high_blend
High Frequency Blend

Shelf Results...

sh_final_dist_tr_blend
sh_low_blend
Low Frequency Blend
sh_high_blend
High Frequency Blend

Part B: Feature Matching for Autostitching

Harris Interest Point Detector

The first step of autostitching is finding corners in the images. Harris corners are computed and then thresholded. The images below are overlaid with their respective Harris corners.

ptb_lp_corners
Porch (Left)
ptb_rp_corners
Porch (Right)
ptb_lhw_corners
Hallway (Left)
ptb_rhw_corners
Hallway (Right)
ptb_tsh_corners
Shelf (Top)
ptb_bsh_corners
Shelf (Bottom)

Adaptive Non-Maximal Suppression

Adaptive non-maximal suppression is used to select 200 interest points from the Harris corners. This method sorts the corners by non-maximal suppression radius and retrieves 200 corresponding to the largest radii, ensuring that the interest points are well-distributed across the images.

ptb_lp_anms
Porch (Left)
ptb_rp_anms
Porch (Right)
ptb_lhw_anms
Hallway (Left)
ptb_rhw_anms
Hallway (Right)
ptb_tsh_anms
Shelf (Top)
ptb_bsh_anms
Shelf (Bottom)

Feature Descriptor Extraction

For each feature point, a feature descriptor is extracted. More specifically, a 40x40 patch is created around each feature point, and an 8x8 patch is sampled with a spacing of s=5 pixels. Finally, the feature descriptors are bias/gain-normalized. For each image below, the 200 feature descriptors are shown in a 10x20 grid.

ptb_lp_feat_desc
Porch (Left)
ptb_rp_feat_desc
Porch (Right)
ptb_lhw_feat_desc
Hallway (Left)
ptb_rhw_feat_desc
Hallway (Right)
ptb_tsh_feat_desc
Shelf (Top)
ptb_bsh_feat_desc
Shelf (Bottom)

Feature Matching

Feature descriptors are matched across images in a mosaic. Using scipy.spatial's KDTree, a match is recorded if:

ptb_p_feat_match
Porch (# Matches: 16)
ptb_hw_feat_match
Hallway (# Matches: 28)
ptb_sh_feat_match
Shelf (# Matches: 36)

RANSAC

Given feature matches, 4-point RANSAC is used to compute a robust homography estimate. The algorithm is described in the following steps:

  1. Select four feature pairs at random
  2. Compute exact homography H
  3. Compute inliers where dist(p', Hp) < error
  4. Repeat steps 1-3 for k iterations
  5. Keep largest set of inliers
  6. Recompute least squares H estimate on all inliers
ptb_p_inliers
Porch (Size of largest inlier set: 9)
ptb_hw_inliers
Hallway (Size of largest inlier set: 19)
ptb_sh_inliers
Shelf (Size of largest inlier set: 19)

Results

Finally, Part A (image warping and blending) is repeated to produce mosaics. The manually and automatically stitched results from both parts are displayed side by side for comparison. For the most part, the automatically stitched results look just as if not better aligned than the manually stitched results.

final_blend
Porch (Manual)
ptb_p_mosaic
Porch (Automatic)
hw_final_dist_tr_blend
Hallway (Manual)
ptb_hw_mosaic
Hallway (Automatic)
sh_final_dist_tr_blend
Shelf (Manual)
ptb_sh_mosaic
Shelf (Automatic)

The coolest thing that I learned from this project was RANSAC. By testing a handful of random samples, a comparable homography can be computed automatically and in a time-efficient manner.