CS180 — Project 3: Image Warping and Mosaicing
This project explores image warping and mosaicing techniques to create seamless panoramic images. I implement homography computation, image warping with different interpolation methods, and blending techniques to combine multiple photographs into cohesive mosaics.
Part A.1: Shoot the Pictures
I captured multiple sets of images with projective transformations between them by fixing the center of projection and rotating the camera. The images have significant overlap (40-70%) to facilitate registration.
Image Set 1: Lecture
Image Set 2: 7/11 + Car Crash
Image Set 3: Campus
Part A.2: Recover Homographies
I implemented a function to compute homographies from point correspondences using least-squares optimization.
Theory
A homography H is a 3x3 matrix that relates corresponding points between two images: p' = Hp. With 8 degrees of freedom, we need at least 4 point correspondences to solve for H, though more points provide better stability. Typically I try to use 12-20, and then compute the least squares solution as the system is now overdetermined and has infinitely many solutions.
Point Correspondences and Homography Results
Below we show the images with the correspondence points plotted on them. Below each set of images, we have the homography matrices computed for each one.
Lecture Images
Homography Matrix:
2.54224189,-0.02326480,-4665.08583807
0.58036296,2.01332082,-1374.72424197
0.00040159,-0.00003625,1.00000000
7/11 Images
Homography Matrix:
3.39946906,-0.23491370,-6928.81047850
1.08393872,2.72711418,-2939.12346025
0.00061393,-0.00003907,1.00000000
Campus Images
Homography Matrix 1 (Image 1 to Image 2):
1.45918618,-0.04536459,-1514.89683294
0.18613671,1.22894351,-338.71987586
0.00012040,-0.00003097,1.00000000
Homography Matrix 2 (Image 2 to Image 3):
0.36786312,-0.14242818,91.61102915
-0.22004851,0.45603987,574.54088030
-0.00013562,-0.00012392,1.00000000
Part A.3: Warp the Images
I implemented two interpolation methods for image warping using inverse warping to avoid holes in the output.
Interpolation Methods
Nearest Neighbor Interpolation
- Rounds coordinates to the nearest pixel value
- Fast but can produce blocky artifacts
- Good for preserving sharp edges
Bilinear Interpolation
- Uses weighted average of four neighboring pixels
- Smoother results but more computationally expensive
- Better for natural image content
Rectification Results
Before creating mosaics, I tested the warping implementation by rectifying images containing rectangular objects.
Trade-offs:
- Nearest neighbor is faster
- For these images, the methods ended up doing the same transformation. Moving forward, I use the bilinear warping as I feel like it is the safer method in general to use.
Part A.4: Blend the Images into a Mosaic
I created seamless mosaics by warping images into alignment and using blending to reduce edge artifacts.
Blending Strategy
Instead of simple overlay which creates harsh edges, I implemented weighted averaging with alpha masks. Here's how the blending process works:
Why Blending is Necessary:
When you simply overlay warped images, you get harsh transitions and visible seams where images meet. This happens because:
- Small alignment errors create double images or gaps
- Different exposures between photos create brightness discontinuities
- Camera settings may vary slightly between shots
- Scene changes quickly
How to Use Blending:
- Create Alpha Masks: For each image, generate a mask where values are 1.0 at the center and gradually decrease to 0.0 at the edges
- Normalize Weights: At each pixel, ensure all alpha values sum to 1.0 across all contributing images
- Weighted Average: Compute the final pixel value as:
final_pixel = Σ(alpha_i × pixel_i) / Σ(alpha_i)
Alpha Mask Design:
- Linear Falloff: Alpha decreases linearly from center to edge over a feathering distance
- Distance Transform: Use distance from image border to create smooth transitions
- Overlap Handling: In regions where multiple images overlap, blend based on relative distances from each image's center
Benefits of This Approach:
- Eliminates hard edges between images
- Reduces ghosting from small misalignments
- Handles exposure differences gracefully
- Creates natural-looking transitions
Mosaic Results
Mosaic 1: Lecture
Mosaic 2: 7/11 + Car Crash
Mosaic 3: Campus
Part B: Feature Matching for Autostitching
In this section, I implement the Multi-Image Matching algorithm based on the paper "Multi-Image Matching using Multi-Scale Oriented Patches" by Brown et al., with several simplifications. The goal is to automatically detect, match, and stitch images together without manual point selection as in the previous section.
B.1: Harris Corner Detection
The first step involves detecting interest points using the Harris Corner Detector. Harris corners are points in an image where the intensity changes significantly in multiple directions, making them good candidates for matching across different images.
I used the provided Harris corner detection implementation to identify potential interest points. The algorithm computes the Harris response function at each pixel and identifies local maxima as corner candidates.
After detecting Harris corners, I implemented Adaptive Non-Maximal Suppression (ANMS) to select a more evenly distributed subset of the strongest corners. ANMS works by suppressing corners that are not significantly stronger than their neighbors within a certain radius, resulting in a more spatially distributed set of features.
Harris Corners Without ANMS
Harris Corners With ANMS
B.2: Feature Descriptor Extraction
Once interest points are detected, the next step is to extract distinctive feature descriptors around each corner. These descriptors capture the local appearance of the image region and enable matching between different images.
For each detected corner, I extract an 8x8 patch from a larger 40x40 window around the interest point. The larger window is first blurred to reduce noise and then sampled down to create the 8x8 descriptor. This approach provides better stability and distinctiveness compared to directly sampling the 8x8 patch.
The extracted descriptors are then normalized using bias and gain normalization to make them invariant to illumination changes. Bias normalization removes the mean, and gain normalization scales the descriptor to unit variance. This preprocessing step is crucial for robust matching across images with different lighting conditions.
B.3: Feature Matching
Feature matching involves finding pairs of descriptors from different images that are likely to correspond to the same physical point in the scene. I use the nearest neighbor distance ratio test proposed by Lowe to identify good matches.
For each descriptor in the first image, I compute its distance to all descriptors in the second image. A match is considered valid if the ratio of the distance to the nearest neighbor and the distance to the second nearest neighbor is below a threshold (typically 0.7-0.8). This ratio test helps reject ambiguous matches where multiple descriptors are similarly close. Below I show some example features that are extracted and match between the 7/11 image (we show the entire 40x40 patch so it is easier to visualize how the matching process occurs, but for the actual computations, we use the normalized 8x8 patches).
Feature Matching Results
B.4: RANSAC for Robust Homography
The final step involves computing a robust homography transformation using RANSAC (Random Sample Consensus). Even with the ratio test, some feature matches will be incorrect (outliers). RANSAC helps identify the largest set of consistent matches (inliers) and computes the homography from these reliable correspondences.
The 4-point RANSAC algorithm works by repeatedly:
- Randomly selecting 4 point correspondences
- Computing a homography from these 4 points
- Evaluating how many other correspondences are consistent with this homography (within a small error threshold)
- Keeping track of the homography with the most inliers
After many iterations, the homography with the most inliers is selected as the final transformation. This approach is robust to outliers and can handle situations where up to 50% of the matches are incorrect.
RANSAC Inlier Detection
Manual vs Automatic Stitching Comparison
The automatic feature-based approach produces different results compared to manual correspondence selection. While manual stitching allows for precise control over the alignment, automatic methods can sometimes capture more subtle geometric relationships. The automatic matching process is more robust when implemented correctly and is several orders of magnitudes faster (~30 seconds vs 10+ minutes). The 7/11 manual stitching is compared to the automatic cropping below:
Automatic Mosaics Gallery
Here are some of the mosaics I created using this automatic stitching process. I make use of some clever techniques to produce some funny images.