-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathutils.py
246 lines (197 loc) · 6.41 KB
/
utils.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import json
import numpy as np
# =========================================================
# Geometry
# =========================================================
def rotation_from_euler(roll=1., pitch=1., yaw=1.):
"""
Get rotation matrix
Args:
roll, pitch, yaw: In radians
Returns:
R: [4, 4]
"""
si, sj, sk = np.sin(roll), np.sin(pitch), np.sin(yaw)
ci, cj, ck = np.cos(roll), np.cos(pitch), np.cos(yaw)
cc, cs = ci * ck, ci * sk
sc, ss = si * ck, si * sk
R = np.identity(4)
R[0, 0] = cj * ck
R[0, 1] = sj * sc - cs
R[0, 2] = sj * cc + ss
R[1, 0] = cj * sk
R[1, 1] = sj * ss + cc
R[1, 2] = sj * cs - sc
R[2, 0] = -sj
R[2, 1] = cj * si
R[2, 2] = cj * ci
return R
def translation_matrix(vector):
"""
Translation matrix
Args:
vector list[float]: (x, y, z)
Returns:
T: [4, 4]
"""
M = np.identity(4)
M[:3, 3] = vector[:3]
return M
def load_camera_params(file):
"""
Get the intrinsic and extrinsic parameters
Returns:
Camera extrinsic and intrinsic matrices
"""
with open(file, 'rt') as handle:
p = json.load(handle)
fx, fy = p['fx'], p['fy']
u0, v0 = p['u0'], p['v0']
pitch, roll, yaw = p['pitch'], p['roll'], p['yaw']
x, y, z = p['x'], p['y'], p['z']
# Intrinsic
K = np.array([[fx, 0, u0, 0],
[0, fy, v0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
# Extrinsic
R_veh2cam = np.transpose(rotation_from_euler(roll, pitch, yaw))
T_veh2cam = translation_matrix((-x, -y, -z))
# Rotate to camera coordinates
R = np.array([[0., -1., 0., 0.],
[0., 0., -1., 0.],
[1., 0., 0., 0.],
[0., 0., 0., 1.]])
RT = R @ R_veh2cam @ T_veh2cam
return RT, K
# =========================================================
# Projections
# =========================================================
def perspective(cam_coords, proj_mat, h, w):
"""
P = proj_mat @ (x, y, z, 1)
Project cam2pixel
Args:
cam_coords: [4, npoints]
proj_mat: [4, 4]
Returns:
pix coords: [h, w, 2]
"""
eps = 1e-7
pix_coords = proj_mat @ cam_coords
pix_coords = pix_coords[:2, :] / (pix_coords[2, :] + eps)
pix_coords = np.reshape(pix_coords, (2, h, w))
pix_coords = np.transpose(pix_coords, (1, 2, 0))
return pix_coords
def bilinear_sampler(imgs, pix_coords):
"""
Construct a new image by bilinear sampling from the input image.
Args:
imgs: [H, W, C]
pix_coords: [h, w, 2]
:return:
sampled image [h, w, c]
"""
img_h, img_w, img_c = imgs.shape
pix_h, pix_w, pix_c = pix_coords.shape
out_shape = (pix_h, pix_w, img_c)
pix_x, pix_y = np.split(pix_coords, [1], axis=-1) # [pix_h, pix_w, 1]
pix_x = pix_x.astype(np.float32)
pix_y = pix_y.astype(np.float32)
# Rounding
pix_x0 = np.floor(pix_x)
pix_x1 = pix_x0 + 1
pix_y0 = np.floor(pix_y)
pix_y1 = pix_y0 + 1
# Clip within image boundary
y_max = (img_h - 1)
x_max = (img_w - 1)
zero = np.zeros([1])
pix_x0 = np.clip(pix_x0, zero, x_max)
pix_y0 = np.clip(pix_y0, zero, y_max)
pix_x1 = np.clip(pix_x1, zero, x_max)
pix_y1 = np.clip(pix_y1, zero, y_max)
# Weights [pix_h, pix_w, 1]
wt_x0 = pix_x1 - pix_x
wt_x1 = pix_x - pix_x0
wt_y0 = pix_y1 - pix_y
wt_y1 = pix_y - pix_y0
# indices in the image to sample from
dim = img_w
# Apply the lower and upper bound pix coord
base_y0 = pix_y0 * dim
base_y1 = pix_y1 * dim
# 4 corner vertices
idx00 = (pix_x0 + base_y0).flatten().astype(np.int)
idx01 = (pix_x0 + base_y1).astype(np.int)
idx10 = (pix_x1 + base_y0).astype(np.int)
idx11 = (pix_x1 + base_y1).astype(np.int)
# Gather pixels from image using vertices
imgs_flat = imgs.reshape([-1, img_c]).astype(np.float32)
im00 = imgs_flat[idx00].reshape(out_shape)
im01 = imgs_flat[idx01].reshape(out_shape)
im10 = imgs_flat[idx10].reshape(out_shape)
im11 = imgs_flat[idx11].reshape(out_shape)
# Apply weights [pix_h, pix_w, 1]
w00 = wt_x0 * wt_y0
w01 = wt_x0 * wt_y1
w10 = wt_x1 * wt_y0
w11 = wt_x1 * wt_y1
output = w00 * im00 + w01 * im01 + w10 * im10 + w11 * im11
return output
def warped(src_image, pix_coords):
"""
Warp source image using transformed points.
"""
src_h, src_w, src_c = src_image.shape
# dst_h, dst_w = dst_size
dst_h, dst_w, pix_c = pix_coords.shape
# Discretize & get points within image frame
xpoints, ypoints = pix_coords[..., 0], pix_coords[..., 1]
ind = np.where((xpoints >= 0) & (xpoints < src_w) & (ypoints >= 0) & (ypoints < src_h))
ypoints, xpoints = ypoints[ind].astype(np.int), xpoints[ind].astype(np.int)
# Get the corresponding point
xpix, ypix = np.meshgrid(np.linspace(0, dst_w - 1, dst_w), np.linspace(0, dst_h - 1, dst_h))
ypix, xpix = ypix[ind].astype(int), xpix[ind].astype(int)
out = np.zeros((dst_h, dst_w, src_c), dtype=src_image.dtype)
out[ypix, xpix, :] = src_image[ypoints, xpoints, :]
return out
class Plane:
"""
Defines a plane in the world
"""
def __init__(self, x, y, z, roll, pitch, yaw,
col, row, scale):
self.x, self.y, self.z = x, y, z
self.roll, self.pitch, self.yaw = roll, pitch, yaw
self.col, self.row = col, row
self.scale = scale
self.xyz = self.xyz_coord()
def xyz_coord(self):
"""
Returns:
Grid coordinate: [b, 3/4, row*cols]
"""
xmin = self.x
xmax = self.x + self.col * self.scale
ymin = self.y
ymax = self.y + self.row * self.scale
return meshgrid(xmin, xmax, self.col,
ymin, ymax, self.row)
def meshgrid(xmin, xmax, num_x, ymin, ymax, num_y, is_homogeneous=True):
"""
Grid is parallel to z-axis
Returns:
array x,y,z,[1] coordinate [3/4, num_x * num_y]
"""
x = np.linspace(xmin, xmax, num_x)
y = np.linspace(ymin, ymax, num_y)
x, y = np.meshgrid(x, y)
x = x.flatten()
y = y.flatten()
z = np.zeros_like(x)
if is_homogeneous:
coords = np.stack([x, y, z, np.ones_like(x)], axis=0)
else:
coords = np.stack([x, y, z], axis=0)
return coords