在开始前,先说明一下,pcl的库安装不简单,不仅需要pip安装,还需要安装C++的pcl库,所以整个流程会比较漫长。鉴于官方参考网站的python-pcl太麻烦,我会用pclpy来代替python-pcl
官方参考网站:https://github.com/strawlab/python-pcl
1. 安装C++的pcl库PCL All-In-One Installer
http://pointclouds.org/downloads/windows.html
http://www.tarnyko.net/dl/gtk.htm
3. 安装pclpy,这里用pclpy代替python-pcl,避免新手要编译的麻烦
pip install pclpy
最后展示一下使用例程,这里包含三个方法:
1. 将pcd转成bin(kitti格式)
2. pcd两种保存格式(binary/ascii),将binary转成ascii格式
3. 显示pcd格式
import os
import numpy as np
import pclpy
def read_pcd(filepath):
lidar = []
with open(filepath, 'r', encoding='utf-8') 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 pcl_binary_ascii(pcd_src_path, pcd_dst_path):
''' pcd中binary转ascii'''
pcl_file = pclpy.pcl.PointCloud.PointXYZI()
pclpy.pcl.io.loadPCDFile(pcd_src_path, pcl_file)
pclpy.pcl.io.savePCDFileASCII(pcd_dst_path, pcl_file)
def batch_pcl_binary_ascii(pcd_src_path, bin_dst_path):
''' 批量将pcd中bin转成ascii'''
for index, name in enumerate(os.listdir(pcd_src_path)):
print('%d-%s' % (index, name))
pcd_full_src_path = os.path.join(pcd_src_path, name)
pcd_full_dst_path = os.path.join(bin_dst_path, name)
pcl_binary_ascii(pcd_full_src_path, pcd_full_dst_path)
def convert(pcdfolder, pcd_ascii, binfolder):
if not os.path.exists(pcd_ascii):
os.mkdir(pcd_ascii)
if not os.path.exists(binfolder):
os.mkdir(binfolder)
batch_pcl_binary_ascii(pcdfolder, pcd_ascii)
pcdfolder = pcd_ascii
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__":
convert(r'C:\Users\27664\Desktop\1', r'C:\Users\27664\Desktop\1-ascii', r'C:\Users\27664\Desktop\1-bin')
# fire.Fire()