cKDTree中的query_ball_point()函数用法

文章介绍了如何在Python中利用scipy.spatial.cKDTree进行点云数据的邻域搜索,通过`query_ball_point`函数找出指定点周围一定半径内的点及其索引,如在二维空间中的例子显示了(2,0)邻域内距离为1的点查找。
摘要由CSDN通过智能技术生成

1. 用法

在这里插入图片描述
x可以是一个点也可以是一堆点,要找x邻域内的点。
r是搜索的半径。
eps是一个非负的float型小数,如果最近邻的点距离比r/(1+eps)还大,则不再进行搜索。
返回找到的点的索引。

from scipy.spatial import cKDTree
#point cloud data -> kdtree
pcl_kdtree = cKDTree(pcd_reconstruction_points)
indices = pcl_kdtree.query_ball_point(voxel_center, 50)
count = len(indices)

2. 举个例子

>>> import numpy as np
>>> from scipy import spatial
>>> x, y = np.mgrid[0:4, 0:4]
>>> x,y
(array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3]]), array([[0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3]]))
>>> x.ravel()
array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3])
>>> points = np.c_[x.ravel(), y.ravel()]
>>> points
array([[0, 0],
       [0, 1],
       [0, 2],
       [0, 3],
       [1, 0],
       [1, 1],
       [1, 2],
       [1, 3],
       [2, 0],
       [2, 1],
       [2, 2],
       [2, 3],
       [3, 0],
       [3, 1],
       [3, 2],
       [3, 3]])
>>> tree = spatial.cKDTree(points)
>>> tree
<scipy.spatial.ckdtree.cKDTree object at 0x000001A2EF116F20>
>>> tree.query_ball_point([2, 0], 1)
[4, 8, 9, 12]
>>> points[4]
array([1, 0])
>>> points[8]
array([2, 0])
>>> points[9]
array([2, 1])
>>> points[12]
array([3, 0])

上面是二维空间的例子,可以看到找(2,0)邻域内距离是1的点,返回了包括自身在内的4个点的索引。
在这里插入图片描述

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`scipy.spatial.cKDTree.query_ball_point`的`count`参数是一个布尔值,用于指是否只返回满足条件的点的数量,而不返回点索引。 当`count_only为`True`时,函数将返回满足条件的点的数量,而不返回点的索引。这在你只关心满足条件的点的数量而不关心具体点的情况下很有用。 当`count_only`为`False`时,函数将返回满足条件的点的索引列表。这对于需要知道具体满足条件的点的索引的情况很有用。 下面是一个示例: ```python import numpy as np from scipy.spatial import cKDTree # 创建一个包含10个二维点的数组 points = np.random.rand(10, 2) # 创建一个cKDTree对象 tree = cKDTree(points) # 查询距离原点(0, 0)距离小于0.5的点的数量 count = tree.query_ball_point([0, 0], 0.5, count_only=True) print("满足条件的点的数量:", count) # 查询距离原点(0, 0)距离小于0.5的点的索引列表 indices = tree.query_ball_point([0, 0], 0.5, count_only=False) print("满足条件的点的索引列表:", indices) ``` 输出结果示例: ``` 满足条件的点的数量: 3 满足条件的点的索引列表: [0, 2, 6] ``` 在上面的示例,我们创建了一个包含10个二维点的数组,并使用`cKDTree`构建了一个树。然后,我们使用`query_ball_point`方法查询距离原点(0, 0)距离小于0.5的点。通过设置`count_only`参数为`True`,我们只获取满足条件的点的数量;通过设置为`False`,我们获取满足条件的点的索引列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一位不愿暴露自己的小可爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值