🏛️

CS180 Project 4A: Image Warping and Mosaicing

Author: Nicolas Rault-Wang (nraultwang at berkeley.edu)

Credit to Notion for this template.

1. Recovering a Homography

We begin by explaining how we estimate the homography transformation matrix HH to map AA into the geometry of BB.

Two images AA and BB with the same center of projection are related by a homography. To estimate this transform, we first need to label a set of N4N\geq 4 feature correspondence points (xj,yj)(xj,yj)(x_j, y_j) \leftrightarrow (x_j^\prime, y_j^\prime) between both images.

Note that NN must be at least 4 because homographies have 8 degrees of freedom due to scale invariance andeach pair of correspondences provides two constraints on HH.

1a. Establishing Point Correspondences

The figure below shows some example pairs of correspondence points. Well-defined edges are excellent features to correspond because they are easy to identify in both images.

1b. Estimating the Homography

While a minimum of 4 correspondence pairs are required to solve for HH, this system is very sensitive to noise. To produce a more robust homography, we use more than 4 correspondence pairs to form an over-determined system, then use least-squares to obtain an estimate of the free parameters of HH.

Working in homogeneous coordinates, the derivation of two equations constraining the free parameters of HH from an arbitrary correspondence pair (xj,yj)(xj,yj)(x_j, y_j) \leftrightarrow (x_j^\prime, y_j^\prime) is straightforward:

[abcdefgh1][xjyj1]=w[xjyj1]\begin{bmatrix}a&b&c\\d&e&f\\g&h&1\end{bmatrix}\begin{bmatrix}x_j\\y_j\\1\end{bmatrix} = w \begin{bmatrix}x_j^\prime\\y_j^\prime\\1\end{bmatrix}
{axj+byj+c=wxjdxj+eyj+f=wyjgxj+hyj+1=w    {axj+byj+c=(gxj+hyj+1)xjdxj+eyj+f=(gxj+hyj+1)yj\begin{cases}ax_j + by_j + c &= wx_j^\prime\\dx_j + ey_j + f &= wy_j^\prime\\gx_j + hy_j + 1 &= w\end{cases} \implies \begin{cases}ax_j + by_j + c &= (gx_j + hy_j + 1) x_j^\prime \\ dx_j + ey_j + f &= (gx_j + hy_j + 1) y_j^\prime \end{cases}
{axj+byj+c+0d+0e+0fgxjxjhyjxj=xj0a+0b+0c+dxj+eyj+fgxjyjhyjyj=yj\begin{cases} ax_j + by_j + c +0d+0e+0f- gx_j x_j^\prime - hy_j x_j^\prime = x_j^\prime \\ 0a + 0b + 0c +dx_j + ey_j + f - gx_j y_j^\prime - hy_j y_j^\prime = y_j^\prime \end{cases}

Stacking all 2N2N equations into an 2N×82N\times 8 feature matrix XX and 2N×12N\times 1 target vector y\vec y gives

[x1y11000x1x1y1x1000x1y11x1y1y1y1xjyj1000xjxjyjxj000xjyj1xjyjyjyjxNyN1000xNxNyNxN000xNyN1xNyNyNyN]Xθ=[x1y1xjyjxNyN]y\underbrace{\begin{bmatrix}x_1&y_1&1&0&0&0&-x_1x_1^\prime&-y_1x_1^\prime \\0&0&0&x_1&y_1&1&-x_1y_1^\prime&-y_1y_1^\prime\\&&&&\vdots&&&&\\x_j&y_j&1&0&0&0&-x_jx_j^\prime&-y_jx_j^\prime \\0&0&0&x_j&y_j&1&-x_jy_j^\prime&-y_jy_j^\prime\\&&&&\vdots&&&&\\x_N&y_N&1&0&0&0&-x_Nx_N^\prime&-y_Nx_N^\prime \\0&0&0&x_N&y_N&1&-x_Ny_N^\prime&-y_Ny_N^\prime \end{bmatrix}}_{X} \vec \theta= \underbrace{\begin{bmatrix} x_1^\prime \\ y_1^\prime\\\vdots\\x_j^\prime \\ y_j^\prime\\\vdots\\x_N^\prime \\ y_N^\prime\end{bmatrix}}_{\vec y}

where we have unrolled HH into θ=[abcdefgh]T\vec \theta = \begin{bmatrix}a&b&c&d&e&f&g&h\end{bmatrix}^T.

For N4N \geq 4 well-chosen correspondence points (xj,yj)(xj,yj)(x_j, y_j) \leftrightarrow (x_j^\prime, y_j^\prime), the columns of XX are linearly independent, making XX have full column rank. Hence, the least-squares estimate of θ\vec\theta, and thus HH, is uniquely given by

θ=(XTX)1XTy\vec\theta^* = (X^TX)^{-1}X^T\vec y

2. Warping Images with a Homography

In all following sections, we’ll use the term “canvas” to refer to an xyxy-axis aligned bounding box that is large enough to contain all points in the image of the homography transform.

2a. Warping One Image

With the homography HH relating AA and BB, we map AA into BB with an inverse warping procedure:

  1. Determine the minimum dimensions of the canvas by warping the corners of AA into BB.
  1. For every canvas pixel (i,j)(i^\prime, j^\prime) in the quadrilateral formed from the warped corners of AA, compute the point (x,y)(x,y) in the geometry of AA by inverting the homography.
    [xy1]=H1[ij1]\begin{bmatrix}x \\y\\1\end{bmatrix} = H^{-1}\begin{bmatrix}i^\prime \\j^\prime\\1\end{bmatrix}
  1. In general, (x,y)(x,y) will not lie exactly on a grid point in AA, so we interpolate the RGBA color vector C(i,j)C(i^\prime, j^\prime) at (x,y)(x,y) from the grid points of AA. Our code uses scipy.interpolate.RegularGridInterpolator to do this.
  1. After completing steps 2 and 3, we have the color value C(i,j)C(i^\prime, j^\prime) at each canvas point (i,j)(i^\prime, j^\prime), completing the process of inverse-warping AA to the canvas with BB, producing AA^\prime in the geometry of BB.
  1. To avoid edge-artifacts, we use a 2-level laplacian pyramid to smoothly blend AA^\prime and BB.
    1. We found that a mask created from the Euclidean distance transform on AA^\prime and BB works very well. Our code uses scipy.ndimage.distance_transform_edt to do this.
The canvas is computed by transforming the corners of AA with the homography HH.
AA^\prime: the result of warping AA into the common canvas via the homography HH.

2b. Image Rectification

An application of homography warping is undoing (rectifying) the perspective distortion of rectangular objects in images. This is theoretically possible because two images of a planar rectangular-looking object, like a tile or poster, are related by a homography.

Below, we show some examples of this operation. The image on the left is the original and the image on the right is the result of rectification.

Rectification of the rectangular windows of the building.

Rectification of the bathroom floor tiles.
Rectification of the signs.

3. Creating Panoramas

We can extend our one-image warping procedure to create panoramas. Since two images with the same center of projection are related by a homography, we can form a panorama by warping a set of images with the same center of projection into a particular image in this set. This can be implemented by recursively applying the the one-image warp to grow a panorama one image at a time.

3a. Panorama Examples

The Wall
View from Doe
Etcheverry Alley
Doe at Night
VLSB at Sunset

3b. Original Images

3b.i. The Wall

3b.ii. Doe at Night

3b.iii. View from Doe

3b.iv. VLSB at Sunset

3b. v. Etcheverry Alley