import os
import numpy as np
import fire
def read_pcd(filepath):
lidar = []
with open(filepath,'r') as f:
line = f.readline().strip()
while line:
linestr = line.split(" ")
if len(linestr) == 4:
linestr_convert = list(map(float, linestr))
lidar.append(linestr_convert)
line = f.readline().strip()
return np.array(lidar)
def convert(pcdfolder, binfolder):
current_path = os.getcwd()
ori_path = os.path.join(current_path, pcdfolder)
file_list = os.listdir(ori_path)
des_path = os.path.join(current_path, binfolder)
if os.path.exists(des_path):
pass
else:
os.makedirs(des_path)
for file in file_list:
(filename,extension) = os.path.splitext(file)
velodyne_file = os.path.join(ori_path, filename) + '.pcd'
pl = read_pcd(velodyne_file)
pl = pl.reshape(-1, 4).astype(np.float32)
velodyne_file_new = os.path.join(des_path, filename) + '.bin'
pl.tofile(velodyne_file_new)
if __name__ == "__main__":
fire.Fire()
2.Python txt转pcd(带RGB值,点云)
1、有numpy就能运行,假如需要转换的TXT文件有header,需要删除header才能运行;
2、直接运行代码,按运行提示输入TXT文件路径和PCD文件保存路径即可,不需要修改代码。
import os
import sys
import numpy as np
def creat_pcd(input_path, output_path):
#Lodaing txt
Full_Data = np.loadtxt(input_path)
#Creating pcd
if os.path.exists(output_path):
os.remove(output_path)
Output_Data = open(output_path, 'a')
Output_Data.write('# .PCD v0.7 - Point Cloud Data file format\nVERSION 0.7\nFIELDS x y z rgba\nSIZE 4 4 4 4\nTYPE F F F U\nCOUNT 1 1 1 1')
string = '\nWIDTH ' + str(Full_Data.shape[0])
Output_Data.write(string)
Output_Data.write('\nHEIGHT 1\nVIEWPOINT 0 0 0 1 0 0 0')
string = '\nPOINTS ' + str(Full_Data.shape[0])
Output_Data.write(string)
Output_Data.write('\nDATA ascii')
for j in range(Full_Data.shape[0]):
R=Full_Data[j,3]
G=Full_Data[j,4]
B=Full_Data[j,5]
value = (int(R) << 16 | int(G) << 8 | int(B))
string = ('\n' + str(Full_Data[j,0]) + ' ' + str(Full_Data[j, 1]) + ' ' +str(Full_Data[j, 2]) + ' ' + str(value))
Output_Data.write(string)
Output_Data.close()
print('--------------Completed--------------')
a = input("请输入TXT文件路径:")#文件路径中斜杆使用"/",比如:D:/pcl/points.txt
b = input("请输入PCD文件保存路径:")#比如:D:/pcl/points.pcd
creat_pcd(a, b)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)