forked from vcg-uvic/acne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geom.py
143 lines (101 loc) · 3.42 KB
/
geom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Filename: geom.py
# License: LICENSES/LICENSE_UVIC_EPFL
import numpy as np
def parse_geom(geom, geom_type):
parsed_geom = {}
if geom_type == "Homography":
parsed_geom["h"] = geom.reshape((-1, 3, 3))
elif geom_type == "Calibration":
parsed_geom["K"] = geom[:, :9].reshape((-1, 3, 3))
parsed_geom["R"] = geom[:, 9:18].reshape((-1, 3, 3))
parsed_geom["t"] = geom[:, 18:21].reshape((-1, 3, 1))
parsed_geom["K_inv"] = geom[:, 23:32].reshape((-1, 3, 3))
parsed_geom["q"] = geom[:, 32:36].reshape([-1, 4, 1])
parsed_geom["q_inv"] = geom[:, 36:40].reshape([-1, 4, 1])
else:
raise NotImplementedError(
"{} is not a supported geometry type!".format(geom_type)
)
return parsed_geom
def np_skew_symmetric(v):
zero = np.zeros_like(v[:, 0])
M = np.stack([
zero, -v[:, 2], v[:, 1],
v[:, 2], zero, -v[:, 0],
-v[:, 1], v[:, 0], zero,
], axis=1)
return M
def np_unskew_symmetric(M):
v = np.concatenate([
0.5 * (M[:, 7] - M[:, 5])[None],
0.5 * (M[:, 2] - M[:, 6])[None],
0.5 * (M[:, 3] - M[:, 1])[None],
], axis=1)
return v
def get_episqr(x1, x2, dR, dt):
num_pts = len(x1)
# Make homogeneous coordinates
x1 = np.concatenate([
x1, np.ones((num_pts, 1))
], axis=-1).reshape(-1, 3, 1)
x2 = np.concatenate([
x2, np.ones((num_pts, 1))
], axis=-1).reshape(-1, 3, 1)
# Compute Fundamental matrix
dR = dR.reshape(1, 3, 3)
dt = dt.reshape(1, 3)
F = np.repeat(np.matmul(
np.reshape(np_skew_symmetric(dt), (-1, 3, 3)),
dR
).reshape(-1, 3, 3), num_pts, axis=0)
x2Fx1 = np.matmul(x2.transpose(0, 2, 1), np.matmul(F, x1)).flatten()
ys = x2Fx1**2
return ys.flatten()
def get_episym(x1, x2, dR, dt):
num_pts = len(x1)
# Make homogeneous coordinates
x1 = np.concatenate([
x1, np.ones((num_pts, 1))
], axis=-1).reshape(-1, 3, 1)
x2 = np.concatenate([
x2, np.ones((num_pts, 1))
], axis=-1).reshape(-1, 3, 1)
# Compute Fundamental matrix
dR = dR.reshape(1, 3, 3)
dt = dt.reshape(1, 3)
F = np.repeat(np.matmul(
np.reshape(np_skew_symmetric(dt), (-1, 3, 3)),
dR
).reshape(-1, 3, 3), num_pts, axis=0)
x2Fx1 = np.matmul(x2.transpose(0, 2, 1), np.matmul(F, x1)).flatten()
Fx1 = np.matmul(F, x1).reshape(-1, 3)
Ftx2 = np.matmul(F.transpose(0, 2, 1), x2).reshape(-1, 3)
ys = x2Fx1**2 * (
1.0 / (Fx1[..., 0]**2 + Fx1[..., 1]**2) +
1.0 / (Ftx2[..., 0]**2 + Ftx2[..., 1]**2))
return ys.flatten()
def get_sampsons(x1, x2, dR, dt):
num_pts = len(x1)
# Make homogeneous coordinates
x1 = np.concatenate([
x1, np.ones((num_pts, 1))
], axis=-1).reshape(-1, 3, 1)
x2 = np.concatenate([
x2, np.ones((num_pts, 1))
], axis=-1).reshape(-1, 3, 1)
# Compute Fundamental matrix
dR = dR.reshape(1, 3, 3)
dt = dt.reshape(1, 3)
F = np.repeat(np.matmul(
np.reshape(np_skew_symmetric(dt), (-1, 3, 3)),
dR
).reshape(-1, 3, 3), num_pts, axis=0)
x2Fx1 = np.matmul(x2.transpose(0, 2, 1), np.matmul(F, x1)).flatten()
Fx1 = np.matmul(F, x1).reshape(-1, 3)
Ftx2 = np.matmul(F.transpose(0, 2, 1), x2).reshape(-1, 3)
ys = x2Fx1**2 / (
Fx1[..., 0]**2 + Fx1[..., 1]**2 + Ftx2[..., 0]**2 + Ftx2[..., 1]**2
)
return ys.flatten()
#
# geom.py ends here