雷達點云數據.pcd格式轉.bin格式
注意,方法1原則上可行,但是本人沒整好pypcd的環境
方法2是絕對可以的。
方法1
1 源碼如下:
def pcb2bin1(): # save as bin format''''''import os# import pypcdfrom pypcd import pypcdimport numpy as npfrom tqdm import tqdmpcd_pth = r'./data/002140.pcd'bin_pth = r'./data/002140.bin'files = os.listdir(pcd_pth)files = [f for f in files if f[-4:]=='.pcd']# for f in files:for ic in tqdm(range(len(files)), desc='進度 '):f = files[ic]binname = os.path.join(pcd_pth, f)binname = os.path.join(bin_pth, f[:-4]+'.bin')# Load pcd from pypcdpc = pypcd.PointCloud.from_path(binname)np_x = (np.array(pc.pc_data['x'], dtype=np.float32)).astype(np.float32)np_y = (np.array(pc.pc_data['y'], dtype=np.float32)).astype(np.float32)np_z = (np.array(pc.pc_data['z'], dtype=np.float32)).astype(np.float32)np_i = (np.array(pc.pc_data['intensity'], dtype=np.float32)).astype(np.float32) / 256points_32 = np.transpose(np.vstack((np_x, np_y, np_z, np_i)))with open(bin_pth, 'w') as f: # Save as bin formatpoints_32.tofile(f)
2 調用pypcd庫,但是安裝好之后,報錯如下
pc = pypcd.PointCloud.from_path(binname)
AttributeError: module 'pypcd' has no attribute 'PointCloud'
此問題是版本不匹配的問題
3 其它人的解決方法
3.1 下載官方源碼
git clone https://github.com/dimatura/pypcd
3.2 如下流程
(1)
cd pypcd
git fetch origin pull/9/head:python3
上述 git 命令嘗試了很多遍才成功,打印如下
remote: Enumerating objects: 36, done.
remote: Counting objects: 100% (22/22), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 36 (delta 19), reused 17 (delta 17), pack-reused 14
Unpacking objects: 100% (36/36), 12.82 KiB | 3.00 KiB/s, done.
From https://github.com/dimatura/pypcd* [new ref] refs/pull/9/head -> python3未成功的打印報錯如下
fatal: unable to access 'https://github.com/dimatura/pypcd/': HTTP/2 stream 1 was not closed cleanly before end of the underlying stream
(2)
git checkout python3
(3)
python3 setup.py install --user
方法2
1 源碼如下:
def pcd2bin2():import numpy as npimport open3d as o3dpcd_path = './data/002140.pcd'bin_path = './data/002140.bin'# 讀取PCD文件 pcd = o3d.io.read_point_cloud(pcd_path)# print(pcd)points = np.asarray(pcd.points)# 查看點云圖像# o3d.visualization.draw_geometries([pcd])# 將PCD格式保存為BIN格式points.tofile(bin_path)# o3d.io.write_point_cloud(bin_path, pcd) # 正常執行,但是不保存數據
2 環境
需要包含open3d的環境(注意,這里的python的環境必須是3.7的版本,親測3.8的python環境一直無法import open3d)
conda create -n pcd2bin python=3.7
conda activate pcd2bin
pip install open3d
參考:
1 python3中pypcd讀取點云數據
2 [python pcd2bin_51CTO博客](https://blog.51cto.com/u_16175471/7581334#:~:text=journey title Python PCD轉BIN 實現步驟 section 準備工作 清理環境,將讀取到的點云數據轉換為numpy數組 section 保存為BIN文件 定義保存路徑 使用numpy庫將點云數據保存為BIN文件 section 完成轉換 打印轉換成功消息)