cKDTree中的query_ball_point()函数用法

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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个点的索引。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我现在强的可怕~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值