【点云格式互转】ply转bin、任意点云格式转ply

        3D点云存储方式的种类较多,包括pcd、ply、txt、bin、obj等格式,各种点云格式的详细介绍请参考之前的博客:点云格式介绍(更新中,待补充)_Coding的叶子的博客-CSDN博客_txt点云格式。本节主要介绍ply格式转bin。

1 安装环境

        本文介绍的方法来源于mmdetection3d,依赖于python库plyfile,安装方法如下:

pip install plyfile

2 示例代码

        ply点云示例文件下载地址为:ply格式点云样例文件_pcd.estimate_normals-深度学习文档类资源-CSDN下载

# -*- coding: utf-8 -*-
"""
乐乐感知学堂公众号
@author: https://blog.csdn.net/suiyingy
"""

import numpy as np
import pandas as pd
from plyfile import PlyData


def convert_ply(input_path, output_path):
    plydata = PlyData.read(input_path)  # read file
    data = plydata.elements[0].data  # read data
    data_pd = pd.DataFrame(data)  # convert to DataFrame
    data_np = np.zeros(data_pd.shape, dtype=np.float)  # initialize array to store data
    property_names = data[0].dtype.names  # read names of properties
    for i, name in enumerate(
            property_names):  # read data by property
        data_np[:, i] = data_pd[name]
    data_np.astype(np.float32).tofile(output_path)


if __name__ == '__main__':
    convert_ply('bun_zipper.ply', 'bun_zipper.bin')
    

        可以看到,ply转bin的过程就是读取ply中的点云数据为numpy矩阵形式,然后直接保存为bin格式就可以了。因此,ply格式读取并不一定需要plyfile库,也可以是之前介绍的open3d库,或者不适用任何库,直接用with open打开文件读取即可。关于ply文件的各种读取方法,以及ply和bin格式的详细介绍请参考之前的博客。

3 其他格式转ply

        其他格式的点云文件 (例如:off, obj),可以使用 trimesh 将它们转化成 ply,也可以按照之前博客中用open3d保存为ply格式点云。同样地,必须首先安装trimesh,即pip install trimesh。

import trimesh

def to_ply(input_path, output_path, original_type):
    mesh = trimesh.load(input_path, file_type=original_type)  # read file
    mesh.export(output_path, file_type='ply')  # convert to ply
to_ply('./test.obj', './test.ply', 'obj')

【python三维深度学习】python三维点云从基础到深度学习_Coding的叶子的博客-CSDN博客_python三维点云重建

 更多三维、二维感知算法和金融量化分析算法请关注“乐乐感知学堂”微信公众号,并将持续进行更新。

猜你喜欢

转载自blog.csdn.net/suiyingy/article/details/125100716
今日推荐