KD Tree#

kd_tree_feature_matching.py#

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# Copyright (c) 2018-2024 www.open3d.org
 5# SPDX-License-Identifier: MIT
 6# ----------------------------------------------------------------------------
 7
 8import numpy as np
 9import open3d as o3d
10
11if __name__ == "__main__":
12
13    print("Load two aligned point clouds.")
14    demo_data = o3d.data.DemoFeatureMatchingPointClouds()
15    pcd0 = o3d.io.read_point_cloud(demo_data.point_cloud_paths[0])
16    pcd1 = o3d.io.read_point_cloud(demo_data.point_cloud_paths[1])
17
18    pcd0.paint_uniform_color([1, 0.706, 0])
19    pcd1.paint_uniform_color([0, 0.651, 0.929])
20    o3d.visualization.draw_geometries([pcd0, pcd1])
21    print("Load their FPFH feature and evaluate.")
22    print("Black : matching distance > 0.2")
23    print("White : matching distance = 0")
24    feature0 = o3d.io.read_feature(demo_data.fpfh_feature_paths[0])
25    feature1 = o3d.io.read_feature(demo_data.fpfh_feature_paths[1])
26
27    fpfh_tree = o3d.geometry.KDTreeFlann(feature1)
28    for i in range(len(pcd0.points)):
29        [_, idx, _] = fpfh_tree.search_knn_vector_xd(feature0.data[:, i], 1)
30        dis = np.linalg.norm(pcd0.points[i] - pcd1.points[idx[0]])
31        c = (0.2 - np.fmin(dis, 0.2)) / 0.2
32        pcd0.colors[i] = [c, c, c]
33    o3d.visualization.draw_geometries([pcd0])
34    print("")
35
36    print("Load their L32D feature and evaluate.")
37    print("Black : matching distance > 0.2")
38    print("White : matching distance = 0")
39    feature0 = o3d.io.read_feature(demo_data.l32d_feature_paths[0])
40    feature1 = o3d.io.read_feature(demo_data.l32d_feature_paths[1])
41
42    fpfh_tree = o3d.geometry.KDTreeFlann(feature1)
43    for i in range(len(pcd0.points)):
44        [_, idx, _] = fpfh_tree.search_knn_vector_xd(feature0.data[:, i], 1)
45        dis = np.linalg.norm(pcd0.points[i] - pcd1.points[idx[0]])
46        c = (0.2 - np.fmin(dis, 0.2)) / 0.2
47        pcd0.colors[i] = [c, c, c]
48    o3d.visualization.draw_geometries([pcd0])
49    print("")

kd_tree_search.py#

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# Copyright (c) 2018-2024 www.open3d.org
 5# SPDX-License-Identifier: MIT
 6# ----------------------------------------------------------------------------
 7"""Build a KDTree and use it for neighbour search"""
 8
 9import open3d as o3d
10import numpy as np
11
12
13def radius_search():
14    print("Loading pointcloud ...")
15    sample_pcd_data = o3d.data.PCDPointCloud()
16    pcd = o3d.io.read_point_cloud(sample_pcd_data.path)
17    pcd_tree = o3d.geometry.KDTreeFlann(pcd)
18
19    print(
20        "Find the neighbors of 50000th point with distance less than 0.2, and painting them green ..."
21    )
22    [k, idx, _] = pcd_tree.search_radius_vector_3d(pcd.points[50000], 0.2)
23    np.asarray(pcd.colors)[idx[1:], :] = [0, 1, 0]
24
25    print("Displaying the final point cloud ...\n")
26    o3d.visualization.draw([pcd])
27
28
29def knn_search():
30    print("Loading pointcloud ...")
31    sample_pcd = o3d.data.PCDPointCloud()
32    pcd = o3d.io.read_point_cloud(sample_pcd.path)
33    pcd_tree = o3d.geometry.KDTreeFlann(pcd)
34
35    print(
36        "Find the 2000 nearest neighbors of 50000th point, and painting them red ..."
37    )
38    [k, idx, _] = pcd_tree.search_knn_vector_3d(pcd.points[50000], 2000)
39    np.asarray(pcd.colors)[idx[1:], :] = [1, 0, 0]
40
41    print("Displaying the final point cloud ...\n")
42    o3d.visualization.draw([pcd])
43
44
45if __name__ == "__main__":
46    knn_search()
47    radius_search()