python-pcl安装及使用

        在开始前,先说明一下,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

2. 安装Windows Gtk+ Download 

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()

 

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值