cs194-26 project5

Mandi Zhao

PartA: Image Warping and Mosaicing

see function code in proj5.py

1. Recover Homographies and Warp Images

To recover the $3x3$ deg-8 homography matrix $H$, I manually marked 10+ corresponding points on each image, then used least-squares to find the best approximate solution for entries of $H$. See result of first image warped into the projection of the second image.

See scatter plots below for selected corresponding points

In [11]:
from proj5 import computeH, warpImage, rectify, get_points
import numpy as np
import matplotlib.pyplot as plt
import skimage.io as skio
im1, im2, im3 = skio.imread('1a.jpg'), skio.imread('1b.jpg'), skio.imread('1c.jpg')
w1 = skio.imread('warp1.jpg')
pt1, pt2 = ([[(2054.171842886504, 353.0711349623198)],
  [(3374.5390500114186, 750.83866179493)],
  [(1739.2725508106873, 634.8231331354186)],
  [(3015.4433660653117, 828.1823475679375)],
  [(2573.479447362411, 1347.4899520438453)],
  [(2346.972939027175, 1446.931833751998)],
  [(1037.654829869833, 1165.1798355788992)],
  [(2120.466430691939, 1629.2419502169444)],
  [(1849.7635304864125, 1750.782027860242)],
  [(2148.08917561087, 1949.665791276547)]],
 [[(1960.2545101621376, 1148.6061886275402)],
  [(3219.8516784654034, 1540.8491664763642)],
  [(1645.3552180863212, 1397.210892897922)],
  [(2888.3787394382275, 1601.619205298013)],
  [(2468.5130166704726, 2109.877711806348)],
  [(2242.0065083352365, 2203.7950445307147)],
  [(921.6393012103222, 1910.9939483900432)],
  [(2009.9754510162138, 2397.1542589632336)],
  [(1728.223452843115, 2529.7434345741035)],
  [(2037.5981959351452, 2756.24994290934)]])
n = 10 ## 10 selected points
pts1, pts2 = np.array(pt1).astype(np.int), np.array(pt2).astype(np.int)
pts1, pts2 = pts1.reshape(n,2), pts2.reshape(n,2)
H = computeH(pts1, pts2)
box = (0,4000,0,4000)
warped = warpImage(im1, H, box)
In [16]:
fig, axs = plt.subplots(2, 2, squeeze=True, figsize=(40, 40))
axs[0,0].imshow(im1)
xs1 = [pair[0] for pair in pts1]
ys1 = [pair[1] for pair in pts1]
axs[0,0].scatter(xs1, ys1)
axs[0,0].set_title("Original first image", fontsize=30)
axs[0,1].imshow(im2)
xs2 = [pair[0] for pair in pts2]
ys2 = [pair[1] for pair in pts2]
axs[0,1].scatter(xs2, ys2)
axs[0,1].set_title("Original second image", fontsize=30)
axs[1,0].imshow(warped)
axs[1,0].set_title("First image projected to the second image's perspective", fontsize=30)
axs[1,1].imshow(w1)
axs[1,1].set_title("Stitch warped-first and second image together to get mosaic", fontsize=30)
plt.show()

Image Rectification

Once I'm able to compute $H$ matrix for any given pair of points from two images, I can take a non-frontal sqaure from an image, correpond its coordinates with that of a customized frontal square, then warp the original image to make the chosen square facing front of the viewer. See scattered blue points below for my chosen square

In [2]:
import numpy as np
import matplotlib.pyplot as plt
import skimage as sk
import skimage.io as skio
import scipy
from scipy.interpolate import interp2d 
from proj5 import computeH, warpImage, rectify, get_points
n = 4
im1 = skio.imread('3.jpg')
In [5]:
pt1, pt2 = ([[(1679.6504702194359, 459.93887147335454)],
  [(2232.1771159874606, 140.3401253918496)],
  [(1657.98275862069, 1906.2586206896551)],
  [(2297.180250783699, 1787.0862068965519)]],
              [[(2700, 400)],[(3300, 400)],[(2700, 1900)],[(3300, 1900)]])
pts1, pts2 = np.array(pt1).astype(np.int), np.array(pt2).astype(np.int)
pts1, pts2 = pts1.reshape(n,2), pts2.reshape(n,2)
box = (0, 2700, 500, 3500)
rect = rectify(im1, pts1, pts2, box)
In [8]:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(20, 16))
fig.add_subplot(1, 2, 1)
plt.imshow(im1)
xs1, ys1 = [pair[0] for pair in pts1], [pair[1] for pair in pts1]
plt.scatter(xs1, ys1)
fig.add_subplot(1, 2, 2)
plt.imshow(rect[0:2700,500:3500])
plt.show()
In [12]:
import matplotlib.pyplot as plt
pts3 = np.array(pt3).astype(np.int)
pts3 = pts3.reshape(6,2)
fig = plt.figure(figsize=(20, 16))
fig.add_subplot(1, 2, 1)
plt.imshow(im3)
xs3 = [pair[0] for pair in pts3]
ys3 = [pair[1] for pair in pts3]
plt.scatter(xs3, ys3)

fig.add_subplot(1, 2, 2)
plt.imshow(rect[400:,:])

plt.show()
In [ ]: