wrl转换pcd格式Python实现

前言

wrl文件是纯ASCII文件,前半部分point数组存的是点的三维坐标,后半部分point数组存的是三角面片的三个顶点信息。

 coord DEF coord0 Coordinate
        {
          point
          [
            -5.4784340000000 -41.7573890000000 -62.3359950000000,
            ……………………

           ]
        }

 texCoord DEF texcoord0 TextureCoordinate
        {
          point
          [

           13499, 13496, 15210, -1,

            ……………………

           ]
        }



pcd文件 点击打开链接

# .PCD v.5 - Point Cloud Data file format
VERSION .5
FIELDS x y z
SIZE 4 4 4
TYPE F F F
COUNT 1 1 1
WIDTH 点个数
HEIGHT 1
POINTS 点个数
DATA ascii

            0.8830480000000 -36.0600430000000 -53.1687160000000,
            0.1566990000000 -37.2082290000000 -53.1687160000000,

            ……………………



将wrl转换成pcd只需提取出点的三维坐标

#coding:utf-8
#import函数库
import time, re, linecache
from sys import argv
#命令行输入脚本文件及所需转换文件
script ,filename = argv
print ("the input file name is:%r." %filename)

start = time.time()
print ("open the file...")
file = open(filename,"r+")


count = 0
keywords = "texCoord DEF texcoord0 TextureCoordinate" #wrl转换时自带的点索引

#统计源文件的点数,actually count-22才是真实的点云个数。count个数根据自己的wrl文件确定
for line in file:
    count=count+1
    if re.search(keywords, line):  
        #print ("from delete %d" %count)
        break


#output = open("out.pcd","w+")
f_prefix = filename.split('.')[0]
output_filename = '{prefix}.pcd'.format(prefix=f_prefix)
output = open(output_filename,"w+")

list = ['# .PCD v.5 - Point Cloud Data file format\n','VERSION .5\n','FIELDS x y z\n','SIZE 4 4 4\n','TYPE F F F\n','COUNT 1 1 1\n']

output.writelines(list)
output.write('WIDTH ') #注意后边有空格
output.write(str(count-37))
output.write('\nHEIGHT ')
output.write(str(1))  #强制类型转换,文件的输入只能是str格式
output.write('\nPOINTS ')
output.write(str(count-37))
output.write('\nDATA ascii\n')
file1 = open(filename,"r")
for line in  file1.readlines()[34:count-3]:  #同样,去掉不需要的数据
    output.write(line)


file.close()
output.close()
file1.close()

end = time.time()
print ("points: ", count-37)
print ("run time is: ", end-start)

运行示例

cmd输入 python,拖入.py文件以及wrl文件作为输入,同路径下可见转换后pcd文件

wrl转换pcd格式Python实现_第1张图片


你可能感兴趣的:(python)