栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

基恩士CSV点云文件转PCD文件 PYTHON版

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基恩士CSV点云文件转PCD文件 PYTHON版

简介

将点云文件从矩阵形式存储的csv点云文件(CloudCompare 称为Matrix CSV)文件转换为PCD格式的点云文件

运行环境

Python3
运行需要的库 numpy open3d fire

环境配置
pip install numpy
pip install open3d
pip install fire
程序使用方法

    将全部需要转换的csv文件放到同一个文件夹下,比如C:\Data\pc_CSV

    想好转换后的pcd文件需要保存的路径,比如C:\Data\pc_PCD

    在命令行中运行 python csv2pcdascii.py convert C:\Data\pc_CSV C:\Data\pc_PCD

源代码
# 这个文件将基恩士文件转换为pcd点云文件
import numpy as np
import open3d as o3d
import os 
import fire
def isfloat(num):
    try:
        float(num)
        return True
    except ValueError:
        return False
step_size = 1
def csv2pcdascii(csvfile,pcdfile):
    global step_size
    pcddata = []
    print(csvfile)
    with open(csvfile) as f:
        y_step = 0
        for line in f:
            x_step = 0
            line = line.strip()
            stringarray = line.split(',')
            # print(stringarray)
            if len(stringarray)==0 or not isfloat(stringarray[0]):
                #print(stringarray)
                continue
            floatarray = np.array(stringarray).astype(float)
            #print(floatarray)
            for z_value in floatarray:
                x_ = x_step * step_size
                y_ = y_step * step_size
                z_ = z_value
                pcddata.append([x_,y_,z_])
                x_step = x_step + 1
            y_step = y_step + 1
            print(y_step)
    pcddata = np.array(pcddata)
    pcdtarget = o3d.geometry.PointCloud()
    pcdtarget.points = o3d.utility.Vector3dVector(pcddata)
    o3d.io.write_point_cloud(pcdfile,pcdtarget,write_ascii=True)


def convert(csvfolder,pcdfolder):
    cwd = os.getcwd()
    csvpath = os.path.join(cwd,csvfolder)
    pcdpath = pcdfolder
    csvfiles = os.listdir(csvpath)
    pcdpath = os.path.join(cwd,pcdfolder)
    if os.path.exists(pcdfolder):
        pass 
    else:
        os.makedirs(pcdfolder)
    for csvfile in csvfiles:
        (filebase,fileext) = os.path.splitext(csvfile)
        csvfile = os.path.join(csvpath,csvfile)
        pcdfile = os.path.join(pcdpath,filebase+".pcd")
        csv2pcdascii(csvfile,pcdfile)

if __name__ == "__main__":
    fire.Fire() 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/767409.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号