点云 KDtree 搜索并上色附近点(附open3d python代码)

Open3d使用FLANN构建KDTree以便进行快速最近邻检索。




函数search_knn_vector_3d返回锚点的k个最近邻居的索引
列表并将这些相邻点涂有蓝色。 请注意,为了能批量访问点颜色
我们将pcd.colors转换为numpy数组,并将所有选定点广播蓝色。 
我们跳过第一个索引,因为它是锚点本身。



同样,我们可以使用search_radius_vector_3d查询距锚点距离小
于给定半径的所有点并将这些点涂成绿色。

# -*-coding:utf-8 -*-
import numpy as np
import open3d as o3d

# 读取点云
pcd = o3d.io.read_point_cloud("kitti_p.pcd")
# 将点云上成灰色
pcd.paint_uniform_color([0.5, 0.5, 0.5])
# 对点云进行kdtree检索
pcd_tree = o3d.geometry.KDTreeFlann(pcd)
pcd.colors[1500] = [1, 0, 0]
# 检索喵点附近的200个最近邻点, 并将其涂上蓝色
[k, idx, _] = pcd_tree.search_knn_vector_3d(pcd.points[1500], 2000)
# [k, idx, _] = pcd_tree.search_hybrid_vector_3d(pcd.points[1500], 2000)
np.asarray(pcd.colors)[idx[1:], :] = [0, 0, 1]
# 检索以喵点为圆心半径小于0.2米的点云, 并将其涂上绿色
[k, idx, _] = pcd_tree.search_radius_vector_3d(pcd.points[12500], 0.72)
np.asarray(pcd.colors)[idx[1:], :] = [0, 1, 0]
# 可视化点云
o3d.visualization.draw_geometrie
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个简单的 Python 实现 K-D Tree 的示例代码: ```python import heapq class KdNode: def __init__(self, point, left, right): self.point = point self.left = left self.right = right class KdTree: def __init__(self, points): self.root = self.build(points) def build(self, points, depth=0): n = len(points) if n == 0: return None k = len(points[0]) axis = depth % k sorted_points = sorted(points, key=lambda point: point[axis]) mid = n // 2 return KdNode( point=sorted_points[mid], left=self.build(sorted_points[:mid], depth + 1), right=self.build(sorted_points[mid + 1:], depth + 1) ) def nearest(self, point, k=1): heap = [] self.search(self.root, point, heap, k) return [(-dist, node.point) for dist, node in sorted(heap)] def search(self, node, point, heap, k): if node is None: return dist = sum((p - q) ** 2 for p, q in zip(node.point, point)) if len(heap) < k: heapq.heappush(heap, (-dist, node)) else: heapq.heappushpop(heap, (-dist, node)) axis = len(point) % len(node.point) if point[axis] < node.point[axis]: self.search(node.left, point, heap, k) else: self.search(node.right, point, heap, k) ``` 使用方法: ```python points = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)] tree = KdTree(points) nearest = tree.nearest((6, 7), k=2) print(nearest) # [(-2, (5, 6)), (-5, (7, 8))] ``` 上面代码展示了如何使用 K-D Tree 寻找离一个最近的 k 个。在这个例子中,我们构建了一个包含 5 个的 K-D Tree,并查找距离 (6, 7) 最近的 2 个。输出结果为 [(-2, (5, 6)), (-5, (7, 8))],其中第一个元素表示距离,第二个元素表示的坐标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

点云-激光雷达-Slam-三维牙齿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值