TXT生成PCD文件
生活随笔
收集整理的這篇文章主要介紹了
TXT生成PCD文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
TXT生成PCD文件(Python實現(xiàn))
代碼功能:將保存點云信息的TXT文件轉(zhuǎn)化為pcl可讀的pcd文件。
''' This code is transform .txt to the .pcd. and this is the original point cloud 2019.5.23 '''import os import sys import numpy as npBASE_DIR = os.path.dirname(os.path.abspath(__file__)) ROOT_DIR = os.path.dirname(os.path.dirname(BASE_DIR)) sys.path.append(BASE_DIR) sys.path.append(ROOT_DIR) ROOT_DIR = os.path.join(ROOT_DIR, 'pointnet(enhence_11)/sem_seg/log11')colour_map = (255,0,0,0,255,0,0,0,255,156,102,31,255,192,203,255,0,255,0,255,255,255,255,0,51,161,201,128,42,42,48,128,20,160,32,240,255,128,0)def creat_real_pcd(input_path, output_path):# loda dataFull_Data = np.loadtxt(input_path)# creat output fileif os.path.exists(output_path):os.remove(output_path)Output_Data = open(output_path, 'a')# headersOutput_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')# pack RGBfor 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()def creat_pred_pcd(input_path, output_path):# loda dataFull_Data = np.loadtxt(input_path)# creat output fileif os.path.exists(output_path):os.remove(output_path)Output_Data = open(output_path, 'a')# headersOutput_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')# pack RGBfor j in range(Full_Data.shape[0]):index = Full_Data[j,7]R=colour_map[int(index)]G=colour_map[int(index+1)]B=colour_map[int(index+2)]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()def creat_gt_pcd(input_data_path, input_label_path, output_path):# loda dataFull_Data = np.loadtxt(input_data_path)Label_Data = np.loadtxt(input_label_path)# creat output fileif os.path.exists(output_path):os.remove(output_path)Output_Data = open(output_path, 'a')# headersOutput_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')# pack RGBfor j in range(Full_Data.shape[0]):index = Label_Data[j]R=colour_map[int(index)]G=colour_map[int(index+1)]B=colour_map[int(index+2)]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()def creat_err_pcd(input_data_path, input_label_path, output_path):# loda dataFull_Data = np.loadtxt(input_data_path)Label_Data = np.loadtxt(input_label_path)# creat output fileif os.path.exists(output_path):os.remove(output_path)Output_Data = open(output_path, 'a')# headersOutput_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')# pack RGBfor j in range(Full_Data.shape[0]):index = int(Label_Data[j])index1 = int(Full_Data[j,7])if (index != index1):R=int(255)G=int(0)B=int(0)else:R=int(234)G=int(234)B=int(234)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()if __name__=='__main__': OUTPUT_PATH_LIST = [os.path.join(ROOT_DIR,line.rstrip()) for line in open(os.path.join(ROOT_DIR, 'output_filelist.txt'))]for i in range(len(OUTPUT_PATH_LIST)):print('Processing: %d/%d'%(i,len(OUTPUT_PATH_LIST)))input_data_path = OUTPUT_PATH_LIST[i]input_label_path = (OUTPUT_PATH_LIST[i])[:-8] + 'gt.txt'output_real_path = os.path.join(ROOT_DIR,'PCD_file/epoch_80', os.path.basename(OUTPUT_PATH_LIST[i])[:-8] + 'real.pcd')output_pred_path = os.path.join(ROOT_DIR,'PCD_file/epoch_80', os.path.basename(OUTPUT_PATH_LIST[i])[:-8] + 'pred.pcd')output_gt_path = os.path.join(ROOT_DIR,'PCD_file/epoch_80', os.path.basename(OUTPUT_PATH_LIST[i])[:-8] + 'gt.pcd')output_err_path = os.path.join(ROOT_DIR,'PCD_file/epoch_80', os.path.basename(OUTPUT_PATH_LIST[i])[:-8] + 'err.pcd')creat_real_pcd(input_data_path, output_real_path)creat_pred_pcd(input_data_path, output_pred_path)creat_gt_pcd(input_data_path, input_label_path, output_gt_path)creat_err_pcd(input_data_path, input_label_path, output_err_path)總結(jié)
以上是生活随笔為你收集整理的TXT生成PCD文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript 学习笔记 ——do
- 下一篇: X16 - 999、中国近代史纲要、03