目录
1 加载点云并建立kdTree
import open3d as o3d
import numpy as np
# 1 Build KDTree from point cloud
print("Testing kdtree in Open3D...")
print("Load a point cloud and paint it gray.")
sample_pcd_data = o3d.data.PCDPointCloud()
pcd = o3d.io.read_point_cloud(sample_pcd_data.path)
pcd.paint_uniform_color([0.5, 0.5, 0.5])
# 建立kdTree
pcd_tree = o3d.geometry.KDTreeFlann(pcd)
2 设置查询点颜色
# 2 Find neighboring points
# We pick the 9501st (arrays are 0-indexed) point as the anchor point and paint it red.
print("Paint the 1501st point red.")
pcd.colors[9500] = [1, 0, 0] # 当前anchor point设置为红色
3 寻找指定个数的临近点
# 2.1 Using search_knn_vector_3d:指定个数 knn
print("Find its 2000 nearest neighbors, and paint them blue.")
[k, idx, dist] = pcd_tree.search_knn_vector_3d(query=pcd.points[9500], knn=2000)
np.asarray(pcd.colors)[idx[1:], :] = [0, 0, 1] # 临近点点设置为蓝色,第一个是本身
print("Visualize the point cloud.")
o3d.visualization.draw_geometries([pcd])
4 寻找指定范围内的临近点
# 2.2 Using search_radius_vector_3d:指定半径 rnn
print("Find its neighbors with distance less than 0.2, and paint them green.")
[k, idx, _] = pcd_tree.search_radius_vector_3d(query=pcd.points[9500], radius=0.2)
np.asarray(pcd.colors)[idx[1:], :] = [0, 1, 0]
print("Visualize the point cloud.")
o3d.visualization.draw_geometries([pcd])