open3d 小操作汇总

目录

1. 点云拷贝

2.新建点云


1.云拷贝

import open3d as o3d

if __name__ == "__main__":
    # 1. pcd
    print("Load a ply point cloud, print it, and render it")
    sample_ply_data = o3d.data.PLYPointCloud()
    pcd = o3d.io.read_point_cloud(sample_ply_data.path)
    
    new_pcd = o3d.geometry.PointCloud(pcd)
    o3d.visualization.draw([new_pcd])

2.新建点云

import open3d as o3d

if __name__ == "__main__":
    # 1. read pcd
    # Compute ISS Keypoints on armadillo pointcloud.
    armadillo_data = o3d.data.ArmadilloMesh()
    mesh = o3d.io.read_triangle_mesh(armadillo_data.path)

    new_pcd = o3d.geometry.PointCloud()
    new_pcd.points = mesh.vertices

    o3d.visualization.draw([new_pcd])

3. 点云两点平均距离

import numpy as np
import open3d as o3d

import matplotlib.pyplot as plt


def get_best_distance_threshold(point_cloud):
    """
    Calculates the best distance threshold value for a given point cloud.
    可以检查是否存在异常值,一般认为超过两个标准差的数据,就是异常值。用这种方法确定异常值,一般要求数据服从正太分布。
    加减三个标准差见的多些。包含至少99%的分布。俗称3 sigma event. 还有加减0.5标准差的,minimum difference.
    Args:
        point_cloud (open3d.geometry.PointCloud): Point cloud to calculate threshold for.

    Returns:
        float: Best distance threshold value. 两点距离。
    """
    distances = point_cloud.compute_nearest_neighbor_distance()  # 返回每个点的最近点之间的距离
    mean_dist = np.mean(distances)  # 所有两点距离的平均值
    std_dist = np.std(distances)  # 所有两点距离的标准差
    threshold = mean_dist + 0.5 * std_dist

    return threshold


if __name__ == '__main__':
    sample_ply_data = o3d.data.PLYPointCloud()
    pcd = o3d.io.read_point_cloud(sample_ply_data.path)
    pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
    print(pcd)
    o3d.visualization.draw([pcd])

    d_threshold = get_best_distance_threshold(pcd)  # 两点平均距离
    print(d_threshold)
    labels = np.array(pcd.cluster_dbscan(eps=d_threshold*3, min_points=10))
    print(np.unique(labels))

    # view
    max_label = labels.max()  # 最大的类别值
    print(f"point cloud has {max_label + 1} clusters")
    colors = plt.get_cmap("tab20")(labels / (max_label if max_label > 0 else 1))
    colors[labels < 0] = 0  # 类别为0的,颜色设置为黑色
    pcd.colors = o3d.utility.Vector3dVector(colors[:, :3])  # ndarray to vector3d
    o3d.visualization.draw([pcd])

待续。。。

猜你喜欢

转载自blog.csdn.net/jizhidexiaoming/article/details/131020593
今日推荐