open3d-KD树及其操作

open3d提供了一个用于KD树的数据结构 KDTreeFlann

class KDTreeFlann(__pybind11_builtins.pybind11_object):
    def __init__(self, *args, **kwargs):
        """
        __init__(*args, **kwargs)
        Overloaded function.
        1. __init__(self: open3d.cpu.pybind.geometry.KDTreeFlann) -> None
        2. __init__(self: open3d.cpu.pybind.geometry.KDTreeFlann, data: numpy.ndarray[float64[m, n]]) -> None
        3. __init__(self: open3d.cpu.pybind.geometry.KDTreeFlann, geometry: open3d.cpu.pybind.geometry.Geometry) -> None
        4. __init__(self: open3d.cpu.pybind.geometry.KDTreeFlann, feature: open3d::pipelines::registration::Feature) -> None
        """
    # 搜索指定半径内的n个点
    def search_hybrid_vector_3d(self, query, radius, max_nn):
    def search_hybrid_vector_xd(self, query, radius, max_nn):
    # 搜索最近的n个点
    def search_knn_vector_3d(self, query, knn):
    def search_knn_vector_xd(self, query, knn):
    # 搜索指定半径内的所有点
    def search_radius_vector_3d(self, query, radius):
    def search_radius_vector_xd(self, query, radius):
    # 搜索点。 需指定搜索方式(还是上面那三种)
    def search_vector_3d(self, query, search_param):
    def search_vector_xd(self, query, search_param): 
    def set_feature(self, feature):
    def set_geometry(self, geometry):
    def set_matrix_data(self, data):
import open3d as o3d
import numpy as np
from open3d.visualization import draw_geometries
from open3d.web_visualizer import draw
pcd = o3d.io.read_point_cloud("datas/knot.ply")
print(pcd)
-> PointCloud with 1440 points.
if not pcd.has_colors():
    pcd.paint_uniform_color([0.5, 0.5, 0.5])
draw(pcd)

1. 从点云构建kd树

kdt = o3d.geometry.KDTreeFlann(pcd)
print(kdt)
-> <open3d.cpu.pybind.geometry.KDTreeFlann object at 0x7eff65913a40>

2. 查找近邻点

2.1 设置锚点,将第300个点颜色设置为红色(共1440个点)

pcd.colors[300] = [1, 0, 0]
draw(pcd)

2.2 查找最近的n个点

[k, ind, _] = kdt.search_knn_vector_3d(pcd.points[300], 50)
np.asarray(pcd.colors)[ind[1:]]=[0,1,0]
draw(pcd)

2.3 查找指定半径内的点

[k, idx, _] = kdt.search_radius_vector_3d(pcd.points[300], 30)
np.asarray(pcd.colors)[idx[1:], :] = [0, 0, 1]
draw(pcd)