矢量化NumPy时出现问题

2024-10-02 00:35:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图为一个使用激光雷达的学校项目编写一些高效的代码。目标是过滤掉点云中超过10米的任何内容,并将其传递出去。我可以写一个for循环来实现这一点,但它不是很优化。我的目标是通过NumPy有效地实现这一点

def get_distance(input_array):
        return np.sqrt(np.sum(np.square(input_array)))

def filter_by_distance(cloud, distance=10):
     cloud1 = [item for item in cloud if get_distance(item) <= distance]
     return np.asarray(cloud1)

云是一个多维np数组,包含[X,Y,Z,I]

[[23.157  0.032  0.992  0.34 ]
 [23.219  0.105  0.994  0.29 ]
 [23.282  0.179  0.996  0.26 ]
 ...
 [ 1.548 -1.101 -0.77   0.   ]
 [ 1.388 -0.978 -0.676  0.   ]
 [ 1.42  -0.964 -0.684  0.   ]]

我正在使用get_distance(cloud[:,0:2])来获取x,y距离,但是我似乎找不到一种方法在没有for循环的情况下使用此修剪原始点云。我将感谢任何帮助


Tags: 项目cloud目标forinputgetreturndef
3条回答

计算内部行的平方和,无需sqrt,直接与平方距离比较

def filter_by_distance(cloud, distance=10):
    # np.sum is implemented c and very fast
    # axis = 1 for summing row
    # no need to sqaure root (save computation)
    # : stand for row, 0:3 stand for column 0, 1, 2
    umask = np.sum(cloud[:, 0:3]**2, axis=1) < distance**2
    # umask is boolean array, whereever it is false, that cloud point will not be shown
    return cloud[umask]

您可以将True{}布尔的索引(或掩码)作为选择条件传递给ndarray。使用显示的方法获取与原点的距离。只需将其展开,即可在云阵列上按行操作。参见sum documentation并在axis下查看。您需要每一行的总和,因为您已经将数据存储为一行X,Y,Z,I。如果在将来对其进行转置,请对列使用axis=0

dist = np.sqrt(np.sum(np.square(cloud[:,0:2]), axis = 1))

这将为您提供与点云中的点数相同的距离向量。现在使用以下内容制作索引:

index = dist <= 10

如果打印此索引,您将看到一系列True{}。现在进行过滤

filtered_cloud = cloud[index,:]

当然,这是最丑陋的一行。如果你这样做…请发表评论,让人们知道你做了什么

filtered_cloud = cloud[np.sqrt(np.sum(np.square(cloud[:,0:2]), axis = 1)) <= 10, :]

奇怪的是,为什么只使用x和y表示距离

你就快到了。你可以添加或删除z,我不确定我代表了什么

points = cloud[:,0:3]
points = np.sqrt(np.sum(np.square(points), axis=1))
points_filtered = points[points<=distance]

加法是一个轴,用于沿每个点的x、y和z值求和,并包含在numpy中进行过滤points<=distance返回用作掩码的布尔数组

相关问题 更多 >

    热门问题