from __future__ import print_function12import numpy as np3import matplotlib.pyplot as plt45from skimage import transform678from skimage.transform import estimate_transform910source = np.array([(129, 72),11(302, 76),12(90, 185),13(326, 193)])1415target = np.array([[0, 0],16[400, 0],17[0, 400],18[400, 400]])1920tf = estimate_transform('projective', source, target)21H = tf.params # in older versions of skimage, this should be22# H = tf._matrix2324print(H)2526# H = np.array([[ 3.04026872e+00, 1.04929628e+00, -4.67743998e+02],27# [ -1.44134582e-01, 6.23382067e+00, -4.30241727e+02],28# [ 2.63620673e-05, 4.17694527e-03, 1.00000000e+00]])2930def rectify(xy):31x = xy[:, 0]32y = xy[:, 1]3334# You must fill in your code here.35#36# Handy functions are:37#38# - np.dot (matrix multiplication)39# - np.ones_like (make an array of ones the same shape as another array)40# - np.column_stack41# - A.T -- type .T after a matrix to transpose it42# - x.reshape -- reshapes the array x4344# We need to provide the backward mapping45HH = np.linalg.inv(H)4647homogeneous_coordinates = np.column_stack([x, y, np.ones_like(x)])48xyz = np.dot(HH, homogeneous_coordinates.T)4950# We want one coordinate per row51xyz = xyz.T5253# Turn z into a column vector54z = xyz[:, 2]55z = z.reshape([len(z), 1])5657xyz = xyz / z5859return xyz[:, :2]6061image = plt.imread('../../images/chapel_floor.png')62out = transform.warp(image, rectify, output_shape=(400, 400))6364f, (ax0, ax1) = plt.subplots(1, 2, figsize=(8, 4))65ax0.imshow(image)66ax1.imshow(out)6768plt.show()697071