python 在3D网格中填充框

x7rlezfr  于 5个月前  发布在  Python
关注(0)|答案(2)|浏览(67)

我得到了一个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)


我不知道这是否正确,是否有更好的方法

yxyvkwin

yxyvkwin1#

假设box是你的一个盒子(一个形状为(8, 4)的NumPy数组),label是与盒子相关联的标签,假设step_size是一个浮点数,这就是我如何找到盒子中所有点的列表及其标签:

points = np.mgrid[
    np.min(box[:, 0]):np.max(box[:, 0]):step_size,
    np.min(box[:, 1]):np.max(box[:, 1]):step_size,
    np.min(box[:, 2]):np.max(box[:, 2]):step_size,
    label:label + 1
].reshape(4, -1).T

字符串
范例:

>>> from itertools import product, repeat
>>> import numpy as np
>>> step_size = 0.6
>>> label = 7
>>> box = np.hstack([np.array(list(product(*repeat(range(2), 3)))), np.ones((8,1)) * label])
>>> box
array([[0., 0., 0., 7.],
       [0., 0., 1., 7.],
       [0., 1., 0., 7.],
       [0., 1., 1., 7.],
       [1., 0., 0., 7.],
       [1., 0., 1., 7.],
       [1., 1., 0., 7.],
       [1., 1., 1., 7.]])
>>> points = np.mgrid[
...     np.min(box[:, 0]):np.max(box[:, 0]):step_size,
...     np.min(box[:, 1]):np.max(box[:, 1]):step_size,
...     np.min(box[:, 2]):np.max(box[:, 2]):step_size,
...     label:label + 1
... ].reshape(4, -1).T
>>> points
array([[0. , 0. , 0. , 7. ],
       [0. , 0. , 0.6, 7. ],
       [0. , 0.6, 0. , 7. ],
       [0. , 0.6, 0.6, 7. ],
       [0.6, 0. , 0. , 7. ],
       [0.6, 0. , 0.6, 7. ],
       [0.6, 0.6, 0. , 7. ],
       [0.6, 0.6, 0.6, 7. ]])

9rbhqvlz

9rbhqvlz2#

您当前的方法似乎对框内的采样点是正确的,并且它有效地利用了NumPy。然而,您的代码中存在一个小错误。您使用labels[i]而不是boxes[i,0,0,3]来获取每个框的标签。
检查这个代码。

mins = np.min(boxes, axis=(1, 2))
maxs = np.max(boxes, axis=(1, 2))

sampled_points = []
sampled_labels = []

step_size = 0.01

for i in range(boxes.shape[0]):
    num_points_x = int(np.floor((maxs[i, 0] - mins[i, 0]) / step_size))
    num_points_y = int(np.floor((maxs[i, 1] - mins[i, 1]) / step_size))
    num_points_z = int(np.floor((maxs[i, 2] - mins[i, 2]) / step_size))

    x_coords, y_coords, z_coords = np.mgrid[mins[i, 0]:maxs[i, 0]:num_points_x * 1j,
                                            mins[i, 1]:maxs[i, 1]:num_points_y * 1j,
                                            mins[i, 2]:maxs[i, 2]:num_points_z * 1j]

    points = np.vstack([x_coords.ravel(), y_coords.ravel(), z_coords.ravel()]).T
    labels = np.repeat(boxes[i, 0, 0, 3], points.shape[0])  # Use boxes[i, 0, 0, 3] instead of labels[i]

    sampled_points.append(points)
    sampled_labels.append(labels)

字符串

相关问题