wrl转换pcd格式Python实现
2017-01-05 09:13
776 查看
前言
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文件是纯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文件
相关文章推荐
- Python实现把utf-8格式的文件转换成gbk格式的文件
- Python实现把json格式转换成文本或sql文件
- python实现通过pil模块对图片格式进行转换的方法
- python实现将pvr格式转换成pvr.ccz的方法
- python实现txt文件格式转换为arff格式
- python实现将txt文件格式转换为arff格式
- Python实现把json格式转换成文本或sql文件
- Python基于pandas实现json格式转换成dataframe的方法
- Python3实现转换Image图片格式
- 用Python实现CSV格式文件转换为HTML文件[Python3程序开发指南实现]
- Python实现把utf-8格式的文件转换成gbk格式的文件
- Python实现迅雷看看xv格式转换为flv格式
- Python实现将HTML转换成doc格式文件的方法示例
- BCB实现BMP图片的RGB分解,BMP与JPG格式的相互转换
- 利用ffmpeg实现视频格式转换成flv,保证能成功运行
- 日期类型的数据,格式转换的几种实现方式比较
- Python模块--struct 数据格式转换
- Asp.net+Extjs实现对GridPanel分页、行内编辑、增加、删除、弹出新窗体增加、数据验证、编辑指定列、数据格式转换等功能
- [VB.NET]请问高手们:如何在VB.NET中实现320*240的图片转换为240*320的格式?
- python中将阿拉伯数字转换成中文的实现代码