点云学习(2)-密度计算&最近点距离&八叉树

0.需要用的库

import open3d as o3d
import numpy as np
from matplotlib import pyplot as plt

1.密度计算

# -------------------------读取点云-----------------------------
pcd = o3d.io.read_point_cloud("xxx.pcd")
# ------------------------计算平均密度--------------------------
nndist = pcd.compute_nearest_neighbor_distance()
nndist = np.array(nndist)
density = np.mean(nndist)  # 计算平均密度
print("点云密度为 denstity=", density)


伪颜色显示最近邻点

# ---------------------使用伪颜色显示最近邻点--------------------
density_colors = plt.get_cmap('hot')(
    (nndist - nndist.min()) / (nndist.max() - nndist.min()))
density_colors = density_colors[:, :3]
pcd.colors = o3d.utility.Vector3dVector(density_colors)
o3d.visualization.draw_geometries([pcd], window_name="计算平均密度",
                                  width=1024, height=768,
                                  left=50, top=50,
                                  mesh_show_back_face=False)

2.计算最近点距离

欧氏距离

# -----------------------计算最近邻点距离------------------------
nndist = pcd.compute_nearest_neighbor_distance()
nndist = np.array(nndist)
print(nndist)

马氏距离

# -----------------------计算马氏距离------------------------
madist = pcd.compute_mahalanobis_distance()
madist = np.array(madist)
print(madist)

3.构建八叉树

pcd = o3d.io.read_point_cloud("pointcloud.pcd")
point = np.asarray(pcd.points)
N = point.shape[0]
# 点云随机着色
pcd.colors = o3d.utility.Vector3dVector(np.random.uniform(0, 1, size=(N, 3)))
# 可视化点云
o3d.visualization.draw_geometries([pcd], window_name="原始点云",
                                  width=1024, height=768,
                                  left=50, top=50,
                                  mesh_show_back_face=False)
# 创建八叉树, 树深为9
octree = o3d.geometry.Octree(max_depth=9)
# 从点云中构建八叉树,适当扩展边界0.001m
octree.convert_from_point_cloud(pcd, size_expand=0.001)
# 可视化八叉树
o3d.visualization.draw_geometries([octree], window_name="可视化八叉树",
                                  width=1024, height=768,
                                  left=50, top=50,
                                  mesh_show_back_face=False)

你可能感兴趣的:(#,Open3D学习,学习,python,机器学习)