import open3d as o3d
# 读取pcd文件
pcd = o3d.io.read_point_cloud(r"D:\PythonProjects\Codes\paper_images\back_point\voxel.pcd")
# 计算法向量
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=30))
# 选择要绘制法向量的点云索引
selected_points = [1014,1015,471,472,473,474,475,476,477,477,478,479,480,481,512,133,134,135,136,483,137]
# 创建法向量线
lines = []
colors = [[1, 0, 0] for i in range(len(selected_points))] # 设置法向量颜色为红色
for i, idx in enumerate(selected_points):
point = pcd.points[idx]
normal = -pcd.normals[idx] * 0.05 # 乘以-1并乘以0.1以调整法向量长度
lines.append([point, point + normal])
# 创建Open3D点云对象
line_set = o3d.geometry.LineSet()
points = []
for line in lines:
points.extend(line)
line_set.points = o3d.utility.Vector3dVector(points)
# 创建线集索引
lines_idx = []
for i in range(len(selected_points)):
lines_idx.append([2*i, 2*i+1])
line_set.lines = o3d.utility.Vector2iVector(lines_idx)
# 设置线条颜色
line_set.colors = o3d.utility.Vector3dVector(colors)
# 绘制点云和法向量
o3d.visualization.draw_geometries([pcd, line_set])
结果如图所示: