TXT生成PCD文件(Python实现)
代码功能:将保存点云信息的TXT文件转化为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 np
BASE_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 data
Full_Data = np.loadtxt(input_path)
# creat output file
if os.path.exists(output_path):
os.remove(output_path)
Output_Data = open(output_path, 'a')
# headers
Output_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 RGB
for 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 data
Full_Data = np.loadtxt(input_path)
# creat output file
if os.path.exists(output_path):
os.remove(output_path)
Output_Data = open(output_path, 'a')
# headers
Output_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 RGB
for 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 data
Full_Data = np.loadtxt(input_data_path)
Label_Data = np.loadtxt(input_label_path)
# creat output file
if os.path.exists(output_path):
os.remove(output_path)
Output_Data = open(output_path, 'a')
# headers
Output_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 RGB
for 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 data
Full_Data = np.loadtxt(input_data_path)
Label_Data = np.loadtxt(input_label_path)
# creat output file
if os.path.exists(output_path):
os.remove(output_path)
Output_Data = open(output_path, 'a')
# headers
Output_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 RGB
for 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)