KD Tree

kd_tree_feature_matching.py

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# The MIT License (MIT)
 5#
 6# Copyright (c) 2018-2021 www.open3d.org
 7#
 8# Permission is hereby granted, free of charge, to any person obtaining a copy
 9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import numpy as np
28import open3d as o3d
29
30if __name__ == "__main__":
31
32    print("Load two aligned point clouds.")
33    demo_data = o3d.data.DemoFeatureMatchingPointClouds()
34    pcd0 = o3d.io.read_point_cloud(demo_data.point_cloud_paths[0])
35    pcd1 = o3d.io.read_point_cloud(demo_data.point_cloud_paths[1])
36
37    pcd0.paint_uniform_color([1, 0.706, 0])
38    pcd1.paint_uniform_color([0, 0.651, 0.929])
39    o3d.visualization.draw_geometries([pcd0, pcd1])
40    print("Load their FPFH feature and evaluate.")
41    print("Black : matching distance > 0.2")
42    print("White : matching distance = 0")
43    feature0 = o3d.io.read_feature(demo_data.fpfh_feature_paths[0])
44    feature1 = o3d.io.read_feature(demo_data.fpfh_feature_paths[1])
45
46    fpfh_tree = o3d.geometry.KDTreeFlann(feature1)
47    for i in range(len(pcd0.points)):
48        [_, idx, _] = fpfh_tree.search_knn_vector_xd(feature0.data[:, i], 1)
49        dis = np.linalg.norm(pcd0.points[i] - pcd1.points[idx[0]])
50        c = (0.2 - np.fmin(dis, 0.2)) / 0.2
51        pcd0.colors[i] = [c, c, c]
52    o3d.visualization.draw_geometries([pcd0])
53    print("")
54
55    print("Load their L32D feature and evaluate.")
56    print("Black : matching distance > 0.2")
57    print("White : matching distance = 0")
58    feature0 = o3d.io.read_feature(demo_data.l32d_feature_paths[0])
59    feature1 = o3d.io.read_feature(demo_data.l32d_feature_paths[1])
60
61    fpfh_tree = o3d.geometry.KDTreeFlann(feature1)
62    for i in range(len(pcd0.points)):
63        [_, idx, _] = fpfh_tree.search_knn_vector_xd(feature0.data[:, i], 1)
64        dis = np.linalg.norm(pcd0.points[i] - pcd1.points[idx[0]])
65        c = (0.2 - np.fmin(dis, 0.2)) / 0.2
66        pcd0.colors[i] = [c, c, c]
67    o3d.visualization.draw_geometries([pcd0])
68    print("")

kd_tree_search.py

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# The MIT License (MIT)
 5#
 6# Copyright (c) 2018-2021 www.open3d.org
 7#
 8# Permission is hereby granted, free of charge, to any person obtaining a copy
 9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26"""Build a KDTree and use it for neighbour search"""
27
28import open3d as o3d
29import numpy as np
30
31
32def radius_search():
33    print("Loading pointcloud ...")
34    sample_pcd_data = o3d.data.PCDPointCloud()
35    pcd = o3d.io.read_point_cloud(sample_pcd_data.path)
36    pcd_tree = o3d.geometry.KDTreeFlann(pcd)
37
38    print(
39        "Find the neighbors of 50000th point with distance less than 0.2, and painting them green ..."
40    )
41    [k, idx, _] = pcd_tree.search_radius_vector_3d(pcd.points[50000], 0.2)
42    np.asarray(pcd.colors)[idx[1:], :] = [0, 1, 0]
43
44    print("Displaying the final point cloud ...\n")
45    o3d.visualization.draw([pcd])
46
47
48def knn_search():
49    print("Loading pointcloud ...")
50    sample_pcd = o3d.data.PCDPointCloud()
51    pcd = o3d.io.read_point_cloud(sample_pcd.path)
52    pcd_tree = o3d.geometry.KDTreeFlann(pcd)
53
54    print(
55        "Find the 2000 nearest neighbors of 50000th point, and painting them red ..."
56    )
57    [k, idx, _] = pcd_tree.search_knn_vector_3d(pcd.points[50000], 2000)
58    np.asarray(pcd.colors)[idx[1:], :] = [1, 0, 0]
59
60    print("Displaying the final point cloud ...\n")
61    o3d.visualization.draw([pcd])
62
63
64if __name__ == "__main__":
65    knn_search()
66    radius_search()