Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 6249
License: OTHER
1
from __future__ import print_function
2
3
import numpy as np
4
import matplotlib.pyplot as plt
5
6
from skimage import transform
7
8
9
from skimage.transform import estimate_transform
10
11
source = np.array([(129, 72),
12
(302, 76),
13
(90, 185),
14
(326, 193)])
15
16
target = np.array([[0, 0],
17
[400, 0],
18
[0, 400],
19
[400, 400]])
20
21
tf = estimate_transform('projective', source, target)
22
H = tf.params # in older versions of skimage, this should be
23
# H = tf._matrix
24
25
print(H)
26
27
# H = np.array([[ 3.04026872e+00, 1.04929628e+00, -4.67743998e+02],
28
# [ -1.44134582e-01, 6.23382067e+00, -4.30241727e+02],
29
# [ 2.63620673e-05, 4.17694527e-03, 1.00000000e+00]])
30
31
def rectify(xy):
32
x = xy[:, 0]
33
y = xy[:, 1]
34
35
# You must fill in your code here.
36
#
37
# Handy functions are:
38
#
39
# - np.dot (matrix multiplication)
40
# - np.ones_like (make an array of ones the same shape as another array)
41
# - np.column_stack
42
# - A.T -- type .T after a matrix to transpose it
43
# - x.reshape -- reshapes the array x
44
45
# We need to provide the backward mapping
46
HH = np.linalg.inv(H)
47
48
homogeneous_coordinates = np.column_stack([x, y, np.ones_like(x)])
49
xyz = np.dot(HH, homogeneous_coordinates.T)
50
51
# We want one coordinate per row
52
xyz = xyz.T
53
54
# Turn z into a column vector
55
z = xyz[:, 2]
56
z = z.reshape([len(z), 1])
57
58
xyz = xyz / z
59
60
return xyz[:, :2]
61
62
image = plt.imread('../../images/chapel_floor.png')
63
out = transform.warp(image, rectify, output_shape=(400, 400))
64
65
f, (ax0, ax1) = plt.subplots(1, 2, figsize=(8, 4))
66
ax0.imshow(image)
67
ax1.imshow(out)
68
69
plt.show()
70
71