简介
将点云文件从矩阵形式存储的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
源代码
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(',')
if len(stringarray)==0 or not isfloat(stringarray[0]):
continue
floatarray = np.array(stringarray).astype(float)
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()
|