【发布时间】:2021-03-11 17:25:05
【问题描述】:
我有一大堆积分(x, y, z)。
points = np.random.rand(999).reshape(333, 3)
我还有两个点代表兴趣点的 3D 边界框的最小和最大角
min_point = np.random.rand(3)
min_x, min_y, min_z = min_point[0], min_point[1], min_point[2]
max_point = np.random.rand(3)
max_x, max_y, max_z = max_point[0], max_point[1], max_point[2]
我正在尝试在points 中选择位于此边界框内但遇到问题的索引。我最初尝试使用np.where
poi_inds = np.where(
points[:, 0] > min_x and points[:, 0] < max_x and
points[:, 1] > min_y and points[:, 1] < max_y and
points[:, 2] > min_z and points[:, 2] < max_z
)
虽然这会导致
ValueError: The truth value of an array with more than one element is ambiguous.
因为and'ing 每次比较的结果都是模棱两可的(据我了解,numpy 无法决定逐元素还是整个数组 and)。
我找到了一个提供 a solution in the 2D case 的 SO 答案,并且我尝试将 to expand it 用于 3D 案例
poi_inds = np.all(np.logical_and.reduce((
points[:, 0] > min_x, points[:, 0] < max_x,
points[:, 1] > min_y, points[:, 1] < max_y,
points[:, 2] > min_z, points[:, 2] < max_z)))
尽管这似乎总是导致没有点被选中(poid_inds = (False),如果我删除np.all,我发现所有索引都是False),即使在多次运行以下SSCCE之后也是如此。
import numpy as np
points = np.random.rand(999).reshape(333, 3)
rand_pt0 = np.random.rand(3)
rand_pt1 = np.random.rand(3)
# Ensure the minimum point is always less than the maximum
min_point = np.array([
rand_pt0[0] if rand_pt0[0] < rand_pt1[0] else rand_pt1[0],
rand_pt0[1] if rand_pt0[1] < rand_pt1[1] else rand_pt1[1],
rand_pt0[2] if rand_pt0[2] < rand_pt1[2] else rand_pt1[2],
])
max_point = np.array([
rand_pt1[0] if rand_pt0[0] < rand_pt1[0] else rand_pt0[0],
rand_pt1[1] if rand_pt0[1] < rand_pt1[1] else rand_pt0[1],
rand_pt1[2] if rand_pt0[2] < rand_pt1[2] else rand_pt0[2],
])
min_x, min_y, min_z = min_point[0], min_point[1], min_point[2]
max_x, max_y, max_z = max_point[0], max_point[1], max_point[2]
# poi_inds = np.where(
# points[:, 0] > min_x and points[:, 0] < max_x and
# points[:, 1] > min_y and points[:, 1] < max_y and
# points[:, 2] > min_z and points[:, 2] < max_z
# )
poi_inds = np.logical_and.reduce((
points[:, 0] > min_x, points[:, 0] < max_x,
points[:, 1] > min_y, points[:, 1] < max_y,
points[:, 2] > min_z, points[:, 2] < max_z))
在这种情况下正确使用np.where 或np.logical_and 来定位所描述的边界框内的点?
【问题讨论】: