-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
192 lines (111 loc) · 5.46 KB
/
util.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
import numpy as np
from sensor_msgs import point_cloud2
import open3d
from scipy.spatial.transform import Rotation
from my_get_ground_mask import get_ground_mask
from my_get_registration_result import get_registration_result
import constant
def get_msg_lidar_points(msg):
lidar_points_rings = np.array(
list(point_cloud2.read_points(msg)))
return lidar_points_rings[:, :3]
def transform_lidar_points(lidar_points, translation):
lidar_points += translation
return lidar_points
def get_self_mask(lidar_points, labels, self_box):
self_mask = (np.all(lidar_points[:, :2] >= self_box[0][:2], -1)
& np.all(lidar_points[:, :2] <= self_box[1][:2], -1))
for label in range(np.max(labels)+1):
label_lidar_points = lidar_points[labels == label]
if np.any((np.all(label_lidar_points[:, :2] >= self_box[0][:2], -1)
& np.all(label_lidar_points[:, :2] <= self_box[1][:2], -1))):
self_mask[labels == label] = True
return self_mask
def get_point_cloud(lidar_points):
point_cloud = open3d.geometry.PointCloud()
point_cloud.points = open3d.utility.Vector3dVector(
lidar_points)
return point_cloud
def get_moving_labels(prev_points, points, correspondence_set, labels, self_box):
correspondence_mask = np.zeros(labels.shape, dtype=np.bool)
correspondence_mask[correspondence_set[:, 1]] = 1
distance_list = np.zeros(labels.shape)
distance = np.sqrt(np.sum(np.square(
prev_points[correspondence_set[:, 0]]-points[correspondence_set[:, 1]]), axis=-1))
distance_list[correspondence_set[:, 1]] = distance
label_range = np.max(labels)+1
for label in range(label_range):
label_mask = (labels == label)
len_label = np.sum(label_mask)
if len_label != 0:
label_distance_list = distance_list[label_mask]
label_correspondence_mask = correspondence_mask[label_mask]
len_correspondence = np.sum(label_correspondence_mask)
correspondence_ratio = len_correspondence/len_label
mean_label_distance = np.mean(
label_distance_list[label_correspondence_mask])
std_label_distance = np.std(
label_distance_list[label_correspondence_mask])
label_points = points[label_mask]
if correspondence_ratio > 0.5 and mean_label_distance < 0.1:
labels[label_mask] = -1
# elif len_correspondence < 10 \
# or correspondence_ratio < 0.1 \
# or mean_label_distance > 0.5 \
# or std_label_distance > 0.1 \
# or np.mean(label_points[:, 2]) < self_box[0, 2]+0.5:
# labels[label_mask] = -2
# print(correspondence_mask[labels == label])
# if np.mean(label_distance) < 0.1 and np.std(label_distance) < 0.05:
# print(np.max(label_distance))
# if np.mean(label_distance) < 0.2 and len(label_distance) > 100:
# labels[labels == label] = -1
return labels
def get_moving_box_points(lidar_points, moving_labels):
start_box_points = []
end_box_points = []
for moving_label in range(np.max(moving_labels)+1):
label_mask = (moving_labels == moving_label)
if np.sum(label_mask) > 30:
label_lidar_points = lidar_points[label_mask]
label_start_box_points, label_end_box_points = get_start_end_box_points(
np.array([np.min(label_lidar_points, axis=0), np.max(label_lidar_points, axis=0)]))
start_box_points.append(label_start_box_points)
end_box_points.append(label_end_box_points)
return np.array(start_box_points).reshape(-1, 3), np.array(end_box_points).reshape(-1, 3)
def transform_points(transformation, points):
return np.linalg.inv(transformation).dot(
np.hstack((points, np.ones((points.shape[0], 1)))).T).T[:, :3]
def get_start_end_box_points(box):
box_ranges = box.T
xy_x, xy_y = np.meshgrid(
box_ranges[0], box_ranges[1])
xy_points = np.stack([np.repeat(xy_x, 2), np.repeat(
xy_y, 2), np.tile(box_ranges[2], 4)]).T.reshape((4, 2, 3))
yz_y, yz_z = np.meshgrid(
box_ranges[1], box_ranges[2])
yz_points = np.stack([np.tile(box_ranges[0], 4), np.repeat(
yz_y, 2), np.repeat(yz_z, 2)]).T.reshape((4, 2, 3))
xz_x, xz_z = np.meshgrid(
box_ranges[0], box_ranges[2])
xz_points = np.stack([np.repeat(xz_x, 2), np.tile(box_ranges[1], 4), np.repeat(
xz_z, 2)]).T.reshape((4, 2, 3))
box_points = np.vstack((xy_points, yz_points, xz_points))
return box_points[:, 0, :], box_points[:, 1, :]
def plot_box_lines(ax, start_points, end_points, color):
for start_point, end_point in zip(start_points, end_points):
line = np.array([start_point, end_point])
ax.plot(line[:, 0], line[:, 1], line[:, 2], color=color)
def get_point_cloud_fpfh(point_cloud):
radius_normal = constant.voxel_size * 2*20
point_cloud.estimate_normals(
open3d.geometry.KDTreeSearchParamHybrid(radius=radius_normal, max_nn=30))
radius_feature = constant.voxel_size * 5*20
fpfh = open3d.registration.compute_fpfh_feature(
point_cloud,
open3d.geometry.KDTreeSearchParamHybrid(radius=radius_feature, max_nn=100))
return fpfh
def quat_to_ypr(quat):
return Rotation.from_quat(quat).as_euler('zyx')
def ypr_to_dcm(ypr):
return Rotation.from_euler('zyx', ypr).as_dcm()