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])

 

待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值