python 在3D网格中填充框

x7rlezfr  于 2024-01-05  发布在  Python
关注(0)|答案(2)|浏览(152)

我得到了一个n盒子的数组,其中每个盒子有8个4D坐标,即一个(x, y, z, l),其中(x,y,z)是坐标,l是一些标签,比如“汽车”。

  1. boxes.shape = (4, 8, 3, 1)

字符串
例如,位置以米为单位给出,标签是一个简单的整数。因此,数组的一个元素可能如下所示:

  1. boxes[0]
  2. [
  3. [0.0, 0.0, 0.0, 1],
  4. [2.0, 0.0, 0.0, 1],
  5. [2.0, 3.0, 0.0, 1],
  6. [0.0, 3.0, 0.0, 1],
  7. [0.0, 0.0, 1.0, 1],
  8. [2.0, 0.0, 1.0, 1],
  9. [2.0, 3.0, 1.0, 1],
  10. [0.0, 3.0, 1.0, 1]
  11. ]


我想采样点(即(x,y,z)坐标和labels分别),每step_size米的盒子内。例如,我想在盒子内的所有点,这是0.01米的距离,并将它们添加到一个列表中,与相应数量的标签。我目前使用以下方法:

  1. mins = np.min(boxes, axis=1)
  2. maxs = np.max(boxes, axis=1)
  3. # collect all new points
  4. sampled_points = []
  5. sampled_labels = []
  6. # get the label for each box
  7. labels = boxes[:, 3]
  8. # distance between each point, equal for all dimensions
  9. step_size = 0.01
  10. # number of points we want to inlcude for each dimension
  11. num_points_x = np.floor((maxs[:, 0] - mins[:, 0]) / step_size).astype(int)
  12. num_points_y = np.floor((maxs[:, 1] - mins[:, 1]) / step_size).astype(int)
  13. num_points_z = np.floor((maxs[:, 2] - mins[:, 2]) / step_size).astype(int)
  14. # loop over all boxes and create the points
  15. for i in range(boxes.shape[0]):
  16. 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
  17. mins[i, 1]:maxs[i, 1]:num_points_y[i]*1j,
  18. mins[i, 2]:maxs[i, 2]:num_points_z[i]*1j]
  19. points = np.vstack([x_coords.ravel(), y_coords.ravel(), z_coords.ravel()]).T
  20. labels = np.repeat(labels[i], points.shape[0])
  21. sampled_points.append(points)
  22. sampled_labels.append(labels)


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

yxyvkwin

yxyvkwin1#

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

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

字符串
范例:

  1. >>> from itertools import product, repeat
  2. >>> import numpy as np
  3. >>> step_size = 0.6
  4. >>> label = 7
  5. >>> box = np.hstack([np.array(list(product(*repeat(range(2), 3)))), np.ones((8,1)) * label])
  6. >>> box
  7. array([[0., 0., 0., 7.],
  8. [0., 0., 1., 7.],
  9. [0., 1., 0., 7.],
  10. [0., 1., 1., 7.],
  11. [1., 0., 0., 7.],
  12. [1., 0., 1., 7.],
  13. [1., 1., 0., 7.],
  14. [1., 1., 1., 7.]])
  15. >>> points = np.mgrid[
  16. ... np.min(box[:, 0]):np.max(box[:, 0]):step_size,
  17. ... np.min(box[:, 1]):np.max(box[:, 1]):step_size,
  18. ... np.min(box[:, 2]):np.max(box[:, 2]):step_size,
  19. ... label:label + 1
  20. ... ].reshape(4, -1).T
  21. >>> points
  22. array([[0. , 0. , 0. , 7. ],
  23. [0. , 0. , 0.6, 7. ],
  24. [0. , 0.6, 0. , 7. ],
  25. [0. , 0.6, 0.6, 7. ],
  26. [0.6, 0. , 0. , 7. ],
  27. [0.6, 0. , 0.6, 7. ],
  28. [0.6, 0.6, 0. , 7. ],
  29. [0.6, 0.6, 0.6, 7. ]])

展开查看全部
9rbhqvlz

9rbhqvlz2#

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

  1. mins = np.min(boxes, axis=(1, 2))
  2. maxs = np.max(boxes, axis=(1, 2))
  3. sampled_points = []
  4. sampled_labels = []
  5. step_size = 0.01
  6. for i in range(boxes.shape[0]):
  7. num_points_x = int(np.floor((maxs[i, 0] - mins[i, 0]) / step_size))
  8. num_points_y = int(np.floor((maxs[i, 1] - mins[i, 1]) / step_size))
  9. num_points_z = int(np.floor((maxs[i, 2] - mins[i, 2]) / step_size))
  10. x_coords, y_coords, z_coords = np.mgrid[mins[i, 0]:maxs[i, 0]:num_points_x * 1j,
  11. mins[i, 1]:maxs[i, 1]:num_points_y * 1j,
  12. mins[i, 2]:maxs[i, 2]:num_points_z * 1j]
  13. points = np.vstack([x_coords.ravel(), y_coords.ravel(), z_coords.ravel()]).T
  14. labels = np.repeat(boxes[i, 0, 0, 3], points.shape[0]) # Use boxes[i, 0, 0, 3] instead of labels[i]
  15. sampled_points.append(points)
  16. sampled_labels.append(labels)

字符串

展开查看全部

相关问题