-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbev.py
232 lines (197 loc) · 7.35 KB
/
bev.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
import numpy as np
import cv2
import os, math
from scipy.optimize import leastsq
from PIL import Image
TOP_Y_MIN = -30
TOP_Y_MAX = +30
TOP_X_MIN = 0
TOP_X_MAX = 100
TOP_Z_MIN = -3.5
TOP_Z_MAX = 0.6
TOP_X_DIVISION = 0.1
TOP_Y_DIVISION = 0.1
TOP_Z_DIVISION = 0.15
def lidar_to_top(lidar):
idx = np.where(lidar[:, 0] > TOP_X_MIN)
lidar = lidar[idx]
idx = np.where(lidar[:, 0] < TOP_X_MAX)
lidar = lidar[idx]
idx = np.where(lidar[:, 1] > TOP_Y_MIN)
lidar = lidar[idx]
idx = np.where(lidar[:, 1] < TOP_Y_MAX)
lidar = lidar[idx]
idx = np.where(lidar[:, 2] > TOP_Z_MIN)
lidar = lidar[idx]
idx = np.where(lidar[:, 2] < TOP_Z_MAX)
lidar = lidar[idx]
pxs = lidar[:, 0]
pys = lidar[:, 1]
pzs = lidar[:, 2]
prs = lidar[:, 3]
qxs = ((pxs - TOP_X_MIN) // TOP_X_DIVISION).astype(np.int32)
qys = ((pys - TOP_Y_MIN) // TOP_Y_DIVISION).astype(np.int32)
# qzs=((pzs-TOP_Z_MIN)//TOP_Z_DIVISION).astype(np.int32)
qzs = (pzs - TOP_Z_MIN) / TOP_Z_DIVISION
quantized = np.dstack((qxs, qys, qzs, prs)).squeeze()
X0, Xn = 0, int((TOP_X_MAX - TOP_X_MIN) // TOP_X_DIVISION) + 1
Y0, Yn = 0, int((TOP_Y_MAX - TOP_Y_MIN) // TOP_Y_DIVISION) + 1
Z0, Zn = 0, int((TOP_Z_MAX - TOP_Z_MIN) / TOP_Z_DIVISION)
height = Xn - X0
width = Yn - Y0
channel = Zn - Z0 + 2
# print('height,width,channel=%d,%d,%d'%(height,width,channel))
top = np.zeros(shape=(height, width, channel), dtype=np.float32)
# histogram = Bin(channel, 0, Zn, "z", Bin(height, 0, Yn, "y", Bin(width, 0, Xn, "x", Maximize("intensity"))))
# histogram.fill.numpy({"x": qxs, "y": qys, "z": qzs, "intensity": prs})
if 1: # new method
for x in range(Xn):
ix = np.where(quantized[:, 0] == x)
quantized_x = quantized[ix]
if len(quantized_x) == 0:
continue
yy = -x
for y in range(Yn):
iy = np.where(quantized_x[:, 1] == y)
quantized_xy = quantized_x[iy]
count = len(quantized_xy)
if count == 0:
continue
xx = -y
top[yy, xx, Zn + 1] = min(1, np.log(count + 1) / math.log(32))
max_height_point = np.argmax(quantized_xy[:, 2])
top[yy, xx, Zn] = quantized_xy[max_height_point, 3]
for z in range(Zn):
iz = np.where(
(quantized_xy[:, 2] >= z) & (quantized_xy[:, 2] <= z + 1)
)
quantized_xyz = quantized_xy[iz]
if len(quantized_xyz) == 0:
continue
zz = z
# height per slice
max_height = max(0, np.max(quantized_xyz[:, 2]) - z)
top[yy, xx, zz] = max_height
# if 0: #unprocess
# top_image = np.zeros((height,width,3),dtype=np.float32)
#
# num = len(lidar)
# for n in range(num):
# x,y = qxs[n],qys[n]
# if x>=0 and x <width and y>0 and y<height:
# top_image[y,x,:] += 1
#
# max_value=np.max(np.log(top_image+0.001))
# top_image = top_image/max_value *255
# top_image=top_image.astype(dtype=np.uint8)
return top
def draw_top_image(lidar_top):
top_image = np.sum(lidar_top, axis=2)
top_image = top_image - np.min(top_image)
divisor = np.max(top_image) - np.min(top_image)
top_image = top_image / divisor * 255
top_image = np.dstack((top_image, top_image, top_image)).astype(np.uint8)
return top_image
def lidar_to_top_coords(x, y):
# print("TOP_X_MAX-TOP_X_MIN:",TOP_X_MAX,TOP_X_MIN)
Xn = int((TOP_X_MAX - TOP_X_MIN) // TOP_X_DIVISION) + 1
Yn = int((TOP_Y_MAX - TOP_Y_MIN) // TOP_Y_DIVISION) + 1
xx = Yn - int((y - TOP_Y_MIN) // TOP_Y_DIVISION)
yy = Xn - int((x - TOP_X_MIN) // TOP_X_DIVISION)
return xx, yy
def roty(t):
""" Rotation about the y-axis. """
c = np.cos(t)
s = np.sin(t)
return np.array([[c, 0, s], [0, 1, 0], [-s, 0, c]])
def project_to_image(pts_3d, P):
""" Project 3d points to image plane.
Usage: pts_2d = projectToImage(pts_3d, P)
input: pts_3d: nx3 matrix
P: 3x4 projection matrix
output: pts_2d: nx2 matrix
P(3x4) dot pts_3d_extended(4xn) = projected_pts_2d(3xn)
=> normalize projected_pts_2d(2xn)
<=> pts_3d_extended(nx4) dot P'(4x3) = projected_pts_2d(nx3)
=> normalize projected_pts_2d(nx2)
"""
n = pts_3d.shape[0]
pts_3d_extend = np.hstack((pts_3d, np.ones((n, 1))))
# print(('pts_3d_extend shape: ', pts_3d_extend.shape))
pts_2d = np.dot(pts_3d_extend, np.transpose(P)) # nx3
pts_2d[:, 0] /= pts_2d[:, 2]
pts_2d[:, 1] /= pts_2d[:, 2]
return pts_2d[:, 0:2]
def compute_box_3d(obj, P):
""" Takes an object and a projection matrix (P) and projects the 3d
bounding box into the image plane.
Returns:
corners_2d: (8,2) array in left image coord.
corners_3d: (8,3) array in in rect camera coord.
"""
# compute rotational matrix around yaw axis
R = roty(obj.ry)
# 3d bounding box dimensions
l = obj.l
w = obj.w
h = obj.h
# 3d bounding box corners
x_corners = [l / 2, l / 2, -l / 2, -l / 2, l / 2, l / 2, -l / 2, -l / 2]
y_corners = [0, 0, 0, 0, -h, -h, -h, -h]
z_corners = [w / 2, -w / 2, -w / 2, w / 2, w / 2, -w / 2, -w / 2, w / 2]
# rotate and translate 3d bounding box
corners_3d = np.dot(R, np.vstack([x_corners, y_corners, z_corners]))
# print corners_3d.shape
corners_3d[0, :] = corners_3d[0, :] + obj.t[0]
corners_3d[1, :] = corners_3d[1, :] + obj.t[1]
corners_3d[2, :] = corners_3d[2, :] + obj.t[2]
# print 'cornsers_3d: ', corners_3d
# only draw 3d bounding box for objs in front of the camera
if np.any(corners_3d[2, :] < 0.1):
corners_2d = None
return corners_2d, np.transpose(corners_3d)
# project the 3d bounding box into the image plane
corners_2d = project_to_image(np.transpose(corners_3d), P)
# print 'corners_2d: ', corners_2d
return corners_2d, np.transpose(corners_3d)
def draw_box3d_on_top(
image,
boxes3d,
color=(255, 255, 255),
thickness=1,
scores=None,
text_lables=[],
is_gt=False,
):
# if scores is not None and scores.shape[0] >0:
# print(scores.shape)
# scores=scores[:,0]
font = cv2.FONT_HERSHEY_SIMPLEX
img = image.copy()
num = len(boxes3d)
startx = 5
for n in range(num):
b = boxes3d[n]
x0 = b[0, 0]
y0 = b[0, 1]
x1 = b[1, 0]
y1 = b[1, 1]
x2 = b[2, 0]
y2 = b[2, 1]
x3 = b[3, 0]
y3 = b[3, 1]
u0, v0 = lidar_to_top_coords(x0, y0)
u1, v1 = lidar_to_top_coords(x1, y1)
u2, v2 = lidar_to_top_coords(x2, y2)
u3, v3 = lidar_to_top_coords(x3, y3)
if text_lables[n] == 'Car':
color = (0,0,255)
elif text_lables[n] == 'Pedestrian':
color = (0,255,0)
elif text_lables[n] == 'Cyclist':
color = (255,0,0)
cv2.line(img, (u0, v0), (u1, v1), color, thickness, cv2.LINE_AA)
cv2.line(img, (u1, v1), (u2, v2), color, thickness, cv2.LINE_AA)
cv2.line(img, (u2, v2), (u3, v3), color, thickness, cv2.LINE_AA)
cv2.line(img, (u3, v3), (u0, v0), color, thickness, cv2.LINE_AA)
return img