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

简介

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

运行环境

Python3
运行需要的库 numpy open3d fire

环境配置

pip install numpy
pip install open3d
pip install fire

程序使用方法

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

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

  3. 设置好 x和y轴步长,如x_stepsize=1,y_stepsize=2.64

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

源代码 (csv2pcdascii.py)

# 这个文件将基恩士文件转换为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
def csv2pcdascii(csvfile,pcdfile,x_stepsize,y_stepsize):
    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 * x_stepsize
                y_ = y_step * y_stepsize
                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,x_stepsize,y_stepsize):
    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,x_stepsize,y_stepsize)

if __name__ == "__main__":
    fire.Fire() 
  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值