open3d 常用库学习记录

一、读取点云文件常用的函数

1.读取bin(二进制)
(1)读取包括有法向量信息的点云数据

def read_point_cloud_bin(bin_path):
    """
    Read point cloud in bin format

    Parameters
    ----------
    bin_path: str
        Input path of Oxford point cloud bin

    Returns
    ----------

    """
    data = np.fromfile(bin_path, dtype=np.float32)

    # format:
    N, D = data.shape[0]// 6, 6
    point_cloud_with_normal = np.reshape(data, (N, D))

    point_cloud = o3d.geometry.PointCloud()
    point_cloud.points = o3d.utility.Vector3dVector(point_cloud_with_normal[:, 0:3])
    point_cloud.normals = o3d.utility.Vector3dVector(point_cloud_with_normal[:, 3:6])

    return point_cloud

(2)或者用read_oxford_bin()

data = read_oxford_bin(bin_path)
point_cloud = o3d.geometry.PointCloud()
point_cloud.points = o3d.utility.Vector3dVector(data[:, 0:3])
point_cloud.normals = o3d.utility.Vector3dVector(data[:, 3:6])

2.读txt格式的点云文件
用到panda中的DataFrame,和pyntcloud

data = np.genfromtxt(txt_path,delimiter = ",")
data = DataFrame(data[:,0:3])
data.columns = ['x','y','z']
pcd_pynt = Pyntcloud(data)
pcd = pcd_pynt.to_instance("open3d",mesh = False)
point_cloud = o3d.geometry.PointCloud()

3.直接读取、写pcd、mesh文件

pcd = open3d.io.read_point_cloud(pcd_file_path)
open3d.io.write_point_cloud("filename.pcd",pcd)

mesh = open3d.io.triange_mesh(mesh_file_path)
open3d.io.write_triange_mesh("filename.ply",mesh)

二、kd树、近邻搜索等

1.建kd树,常用的点云搜索是使用kd树进行搜索的,因此需要对输入点云建立kd树
混合了knn和邻域内搜索:Search_hybrid_vector_3d,

import open3d as o3d
pcd_kd_tree = o3d.geometry.KDTreeFlann(input_pcd)
[k,idx,_] = pcd_kd_tree.search_knn_vector_3d(centralPoint, K_num) #查找centralpoint点邻域最近的K_num个点)
[k,idx,_] = pcd_kd_tree.search_radius_vector_3d(centralPoint,radius) #查找centralpoint点radius范围邻域内的点)
[k, idx, _] = pcd_tree.search_hybrid_vector_3d(centralPoint,radius,K_num) #最多返回k个和被查询点距离小于给定半径的最近邻点。

三、可视化

1.使用open3d.visualization.draw_geometries(pcd or line) 可以显示点云或者划线等
上色:设置点云属性:pcd.paint_uniform_color([0,0,1])

2.创建一条线,LineSet 必须有一组点,以及对应连线的索引组

points = [
        [0, 0, 0],
        [1, 0, 0],
        [0, 1, 0],
        [1, 1, 0],
        [0, 0, 1],
        [1, 0, 1],
        [0, 1, 1],
        [1, 1, 1],
]
lines = [
   [0, 1],
   [0, 2],
   [1, 3],
   [2, 3],
   [4, 5],
   [4, 6],
   [5, 7],
   [6, 7],
   [0, 4],
   [1, 5],
   [2, 6],
   [3, 7],
]
colors = [[1, 0, 0] for i in range(len(lines))]
line_set = o3d.geometry.LineSet(
    points=o3d.utility.Vector3dVector(points),
    lines=o3d.utility.Vector2iVector(lines),
)
line_set.colors = o3d.utility.Vector3dVector(colors)
o3d.visualization.draw_geometries([line_set])

四、法向量

求法向量,是通过该点的邻域内的协方差矩阵,找出特质值最小的对应特征向量作为法向量

pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01,max_nn=30))

你可能感兴趣的:(点云学习,各种库学习,学习,python,人工智能)