python 读写pcd
生活随笔
收集整理的這篇文章主要介紹了
python 读写pcd
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 讀點云的3種方式
#第一種 # pip3 install python-pcl import pcl #pcd_ndarray = pcl.load(args.pcd_path).to_array()[:, :3] #不要intensity pcd_ndarray = pcl.load_XYZI(args.pcd_path).to_array()[:, :4] point_num = pcd_ndarray.shape[0] # shape屬性可以獲取矩陣的形狀(例如二維數組的行列),獲取的結果是一個元組#第二種 def read_pcd(pcd_path):lines = []num_points = Nonewith open(pcd_path, 'r') as f:for line in f:lines.append(line.strip())if line.startswith('POINTS'):num_points = int(line.split()[-1])assert num_points is not Nonepoints = []for line in lines[-num_points:]:x, y, z, i = list(map(float, line.split()))#這里沒有把i放進去,也是為了后面 x, y, z 做矩陣變換的時候方面#但這個理解我選擇保留, 因為可能把i放進去也不影響代碼的簡易程度points.append((np.array([x, y, z, 1.0]), i))return points#第三種 def load_pcd_to_ndarray(pcd_path):with open(pcd_path) as f:while True:ln = f.readline().strip()if ln.startswith('DATA'):breakpoints = np.loadtxt(f)points = points[:, 0:4]return points #第四種(需要源碼安裝pypcd)鏈接在底部 import argparse from pypcd import pypcd import numpy as np parser = argparse.ArgumentParser() parser.add_argument('--pcd_path', default='', type=str) args = parser.parse_args()def read_pcd(pcd_path):pcd = pypcd.PointCloud.from_path(pcd_path)pcd_np_points = np.zeros((pcd.points, 5), dtype=np.float32)print(pcd.pc_data['x'])pcd_np_points[:, 0] = np.transpose(pcd.pc_data['x'])pcd_np_points[:, 1] = np.transpose(pcd.pc_data['y'])pcd_np_points[:, 2] = np.transpose(pcd.pc_data['z'])pcd_np_points[:, 3] = np.transpose(pcd.pc_data['intensity'])pcd_np_points[:, 4] = np.transpose(pcd.pc_data['is_ground'])del_index = np.where(np.isnan(pcd_np_points))[0]pcd_np_points = np.delete(pcd_np_points, del_index, axis=0)return pcd_np_pointsread_pcd(args.pcd_path)在python3中使用pypcd讀取點云數據
2. 寫點云的兩種方式
HEADER = '''\ # .PCD v0.7 - Point Cloud Data file format VERSION 0.7 FIELDS x y z intensity SIZE 4 4 4 4 TYPE F F F F COUNT 1 1 1 1 WIDTH {} HEIGHT 1 VIEWPOINT 0 0 0 1 0 0 0 POINTS {} DATA ascii '''def write_pcd(points, save_pcd_path):n = len(points)lines = []for i in range(n):x, y, z, i, is_g = points[i]lines.append('{:.6f} {:.6f} {:.6f} {}'.format( \x, y, z, i))with open(save_pcd_path, 'w') as f:f.write(HEADER.format(n, n))f.write('\n'.join(lines))def write_pcd(points, save_pcd_path):with open(save_pcd_path, 'w') as f:f.write(HEADER.format(len(points), len(points)) + '\n')np.savetxt(f, points, delimiter = ' ', fmt = '%f %f %f %d')總結
以上是生活随笔為你收集整理的python 读写pcd的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML中document的作用,htm
- 下一篇: 图灵测试其实已经过时了