CS 180 Project 4: Image Warping and Mosaicing

By Rhythm Seth

Overview

This project explores image mosaicing techniques, including recovering homographies, warping images, and blending them into a seamless mosaic. We'll go through several steps to achieve these fascinating visual effects.

Final Mosaic
Final Mosaic

Part 1A: Recover Homographies

This function uses point correspondences to set up and solve a linear system, recovering the 3x3 homography matrix. We used more than 4 correspondences and implemented a least-squares solution for stability.

$$ \begin{align*} A[2i] &= [-x, -y, -1, 0, 0, 0, x \cdot x', y \cdot x', x'] \\ A[2i+1] &= [0, 0, 0, -x, -y, -1, x \cdot y', y \cdot y', y'] \end{align*} $$

Where (x, y) are the coordinates in the first image, and (x', y') are the corresponding coordinates in the second image. This system of equations is then solved to find the entries of the homography matrix H.

Part 2: Warp the Images

Using the computed homography, we implemented an image warping function. This function transforms the source image to align with the target image's perspective.

Source Image 1
Home Image 1
Source Image 2
Home Image 2
Warped Image
Warped Image
Source Image 1
Living Room Image 1
Source Image 2
Living Room Image 2
Warped Image
Warped Image
Source Image 1
TV Image 1
Source Image 2
TV Image 2
Warped Image
Warped Image

The warping process involves inverse mapping and interpolation to ensure smooth transformation of the image.

Part 3: Rectification

We demonstrated the rectification process by selecting a rectangular object in an image (painting) and warping it to an approximate rectangle. We used the provided online correspondence tool same as we did in Project 3. This technique is useful for correcting perspective distortions.

Source Image
Art Museum Image 1
Rectified Image
Rectified Image
Source Image
Art Museum Image 2
Rectified Image
Rectified Image

The rectification process involved defining corresponding points between the distorted rectangle and a target rectangle in the source image. (we picked points that define a rectangle when defining correspondences). As we can see in both images above, the distorted (due to perspective) rectangle has been rectified to a rectangle

Part 4A: Blend the Images into a Mosaic

In this final step, we blended the two images into a mosaic using the warped images - we also see if naive blending works well.

If naive blending wasn't working well, we tried to create a seamless mosaic by implementing a sophisticated blending technique that accounts for the overlap between the warped images. Our method involves the following key steps:

  1. Create binary masks for each image to identify non-black pixels.
  2. Identify the overlapping region between the two images.
  3. Generate weight matrices for each image using distance transforms. This assigns higher weights to pixels farther from the edge of each image.
  4. Normalize the weights in the overlapping region to ensure a smooth transition.
  5. Apply the weights to each image and combine them.
  6. Use the masks to fill in non-overlapping regions with the original image content.

This approach, which utilizes distance-based weighting, ensures a gradual and natural blend in the overlapping areas while preserving the original image content in non-overlapping regions. The result is a seamless mosaic that smoothly transitions between the input images.

Blended Mosaic 1
Blended Mosaic 1
Mosaic 2
Mosaic 2 without Blending
Blended Mosaic 2
Blended Mosaic 2

The image on the left has some artifacts (can be seen by looking closely at area near bathroom) which we can resolve by using alpha blending. The image on the right demonstrates the result of our blending technique, showing a smooth transition between the warped images and creating a cohesive panoramic view.

Blended Mosaic 3
Blended Mosaic 3

Part B: Automatic Feature Detection and Matching

Corner Detection

Implementation of Harris Corner Detection with Adaptive Non-Maximum Suppression (ANMS) to select 600 points.

Approach

The corner detection process involves:

  • Using Harris corner detection algorithm to get scores h for each pixel
  • Extracting scores into a vector: scores = h[corners[:, 0], corners[:, 1]]
  • Implementing ANMS to identify strongest corners while maintaining even distribution
  • Calculating pairwise distances using dist2()
  • Using numpy broadcasting for f(x_i) < c_robust * f(x_j) comparison
  • Creating masked distances and calculating minimum radii
  • Sorting corners by decreasing radii and selecting best 600 corners
All Corners
All Detected Harris Corners - image 1
All Corners
All Detected Harris Corners - image 2
ANMS Corners
After ANMS - image 1 (600 points)
ANMS Corners
After ANMS - image 2 (600 points)

Feature Descriptors

Approach

For each corner in points obtained from ANMS:

  • Extract 40x40 region feature around the point from original color image
  • Resize to 8x8 using skimage.transform.resize
  • Normalize by subtracting mean and dividing by standard deviation for each channel
  • Flatten and stack into matrix
Feature Descriptors
Example 8x8 Feature Descriptors

Feature Matching

Approach

The matching process involves:

  • Calculate pairwise differences between feature descriptors
  • Compute sum-squared differences (SSD)
  • Calculate Lowe scores using 2-NN distances
  • Filter matches using threshold
  • Pair corresponding features between images
Feature Matches
Feature Matches Between Images

Random Sample Consensus (RANSAC)

Approach

RANSAC implementation steps:

  • Sample four points without replacement
  • Compute homography using sampled points
  • Transform all source points
  • Calculate Euclidean distances to targets
  • Identify inliers within threshold
  • Update best set when more inliers found
RANSAC Results
RANSAC Inliers
RANSAC Results
Warped House using Ransac Homography

Autostitched Mosaics

Results

Comparison between manual and automatic stitching:

Manual Stitching
Manual Stitching
Auto Stitching
Automatic Stitching
Manual Stitching
Manual Stitching
Auto Stitching
Automatic Stitching
Manual Stitching
Manual Stitching
Auto Stitching
Automatic Stitching

Key observations:

  • Slightly less accurate than manual correspondences for some feature matches but much more efficient
  • Limited control over feature selection
  • Some misalignments in specific areas (chairs, walls, windows)

Reflection

Key insights from the project:

  • Understanding homography matrix transformations and chaining
  • Implementing efficient feature descriptor matching using broadcasting
  • Visualizing algorithm-chosen features versus human-chosen features
  • Comparing automatic versus manual stitching results

Overall this project was a great learning experience and helped me really unpack homographies, warping between geometries and how we can use basic math to achieve a task that sounds so impossible.