我得到了一个n
盒子的数组,其中每个盒子有8个4D坐标,即一个(x, y, z, l)
,其中(x,y,z)
是坐标,l
是一些标签,比如“汽车”。
boxes.shape = (4, 8, 3, 1)
字符串
例如,位置以米为单位给出,标签是一个简单的整数。因此,数组的一个元素可能如下所示:
boxes[0]
[
[0.0, 0.0, 0.0, 1],
[2.0, 0.0, 0.0, 1],
[2.0, 3.0, 0.0, 1],
[0.0, 3.0, 0.0, 1],
[0.0, 0.0, 1.0, 1],
[2.0, 0.0, 1.0, 1],
[2.0, 3.0, 1.0, 1],
[0.0, 3.0, 1.0, 1]
]
型
我想采样点(即(x,y,z)
坐标和labels
分别),每step_size
米的盒子内。例如,我想在盒子内的所有点,这是0.01米的距离,并将它们添加到一个列表中,与相应数量的标签。我目前使用以下方法:
mins = np.min(boxes, axis=1)
maxs = np.max(boxes, axis=1)
# collect all new points
sampled_points = []
sampled_labels = []
# get the label for each box
labels = boxes[:, 3]
# distance between each point, equal for all dimensions
step_size = 0.01
# number of points we want to inlcude for each dimension
num_points_x = np.floor((maxs[:, 0] - mins[:, 0]) / step_size).astype(int)
num_points_y = np.floor((maxs[:, 1] - mins[:, 1]) / step_size).astype(int)
num_points_z = np.floor((maxs[:, 2] - mins[:, 2]) / step_size).astype(int)
# loop over all boxes and create the points
for i in range(boxes.shape[0]):
x_coords, y_coords, z_coords = np.mgrid[mins[i, 0]:maxs[i, 0]:num_points_x[i]*1j, # we use complex number here, so that the endpoint is inlcusive
mins[i, 1]:maxs[i, 1]:num_points_y[i]*1j,
mins[i, 2]:maxs[i, 2]:num_points_z[i]*1j]
points = np.vstack([x_coords.ravel(), y_coords.ravel(), z_coords.ravel()]).T
labels = np.repeat(labels[i], points.shape[0])
sampled_points.append(points)
sampled_labels.append(labels)
型
我不知道这是否正确,是否有更好的方法
2条答案
按热度按时间yxyvkwin1#
假设
box
是你的一个盒子(一个形状为(8, 4)
的NumPy数组),label
是与盒子相关联的标签,假设step_size
是一个浮点数,这就是我如何找到盒子中所有点的列表及其标签:字符串
范例:
型
9rbhqvlz2#
您当前的方法似乎对框内的采样点是正确的,并且它有效地利用了NumPy。然而,您的代码中存在一个小错误。您使用labels[i]而不是boxes[i,0,0,3]来获取每个框的标签。
检查这个代码。
字符串