过滤4D numpy数组

时间:2018-08-21 23:06:19

标签: python numpy

我目前正在处理4D numpy数组形式的点云数据。数据是XYZ坐标的列表,我试图找到一个内置的numpy函数来过滤出Z值大于某个阈值的点。

只需使用Python即可轻松实现,但速度非常慢,并且需要一个新的数组:

#xyz_arr is a list of points in the form [x, y, z]
xyz_filtered = []
for point in xyz_arr:
    if point[2] > threshold:
        xyz_filtered.append(point)

我尝试使用numpy.where,但无法弄清楚如何仅查看数组中的一个值。

是否有一种更简单,更麻木的方式?

1 个答案:

答案 0 :(得分:2)

使用Boolean masking

import numpy as np

xyz_arr = [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]]
threshold = 3.5

xyz_arr = np.asarray(xyz_arr)

xyz_filtered = xyz_arr[xyz_arr[:, 2] > threshold]

print(xyz_filtered)
# [[2 3 4]
#  [3 4 5]]