在写一个重建算法的时候需要用到点坐标和法向的数据文件,于是向利用pcl中的法向计算模块来生成法向。输出后法向文件中包含曲率信息,但是这是不需要的。于是自己写了一个python小脚本实现格式转换。
#--coding:utf-8-- import time import numpy as np from sys import argv script, input_file = argv input_data = open(input_file,"r") output_data = open("outdata.asc","w") doc = ''' 本脚本用于数据转换,将PCL生成的文件进行转换 输入为 点云数据+法向信息+曲率 PCD文件 输出为 点云数据+法向信息 ASC文件 转换后曲率信息被滤除,并将格式转换为通用的asc文件格式 ''' print(doc) #转换函数 def transform(input_data, output_data): pointList = [] for line in input_data: data = line.split() if is_number(data[0]): output_data.write("%f %f %f %f %f %f " %((float)(data[0]), (float)(data[1]), (float)(data[2]), (float)(data[3]), (float)(data[4]), (float)(data[5]))) else: pass #判断字符串是否为数字 def is_number(s): try: float(s) return True except ValueError: pass return False transform(input_data, output_data)
原文件内容为:
# .PCD v0.7 - Point Cloud Data file format VERSION 0.7 FIELDS x y z normal_x normal_y normal_z curvature SIZE 4 4 4 4 4 4 4 TYPE F F F F F F F COUNT 1 1 1 1 1 1 1 WIDTH 26381 HEIGHT 1 VIEWPOINT 0 0 0 1 0 0 0 POINTS 26381 DATA ascii 52.713619 44.306831 2.500001 -0.67111164 -0.08553692 0.73640519 0.00061548286 52.54211 45.728249 2.500001 -0.67243981 -0.090179443 0.73463756 0.00069258711 52.30574 47.160099 2.500001 -0.67485178 -0.10609017 0.73028761 0.00043992131 52.022919 48.57592 2.500001 -0.67386091 -0.11686694 0.72955716 0.00037569425 51.720371 49.97781 2.500001 -0.66632128 -0.11713342 0.73640722 0.00062433031 51.405071 51.394611 2.500001 -0.65537727 -0.11913724 0.74584651 0.00090703077 51.068241 52.847191 2.500001 -0.6404919 -0.1198745 0.75855136 0.001082067 50.717159 54.314861 2.500001 -0.62284762 -0.120147 0.77306247 0.0012497745 50.349918 55.789589 2.500001 -0.60351694 -0.12188542 0.7879793 0.0013898062 49.98164 57.264919 2.500001 -0.56129658 -0.1375796 0.81609923 0.0014789379 49.60405 58.715778 2.500001 -0.53091556 -0.14410463 0.83508235 0.0021452638 49.167542 60.149521 2.500001 -0.45673963 -0.16522408 0.87412238 0.0064957533 48.665119 61.58548 2.500001 -0.27892584 -0.12767911 0.95178694 0.015811486
将该文件命名为transfor.py并在终端执行指令:
$python transfor.py input.pcd
Python中的split函数的默认参数是空格, 即对每行数据安装空格分割, 不管空格的数量是一个还是多个, 全部忽略. 但是一旦添加自定义的参数, 则严格按照定义参数分割. 比如, 当定义的参数为一个空格时, 将按一个空格分割, 两个空格时将按两个空格分割. 该处需要注意.
转换后的文件内容为:
52.713619 44.306831 2.500001 -0.671112 -0.085537 0.736405 52.542110 45.728249 2.500001 -0.672440 -0.090179 0.734638 52.305740 47.160099 2.500001 -0.674852 -0.106090 0.730288 52.022919 48.575920 2.500001 -0.673861 -0.116867 0.729557 51.720371 49.977810 2.500001 -0.666321 -0.117133 0.736407 51.405071 51.394611 2.500001 -0.655377 -0.119137 0.745847 51.068241 52.847191 2.500001 -0.640492 -0.119874 0.758551 50.717159 54.314861 2.500001 -0.622848 -0.120147 0.773062 50.349918 55.789589 2.500001 -0.603517 -0.121885 0.787979 49.981640 57.264919 2.500001 -0.561297 -0.137580 0.816099 49.604050 58.715778 2.500001 -0.530916 -0.144105 0.835082 49.167542 60.149521 2.500001 -0.456740 -0.165224 0.874122 48.665119 61.585480 2.500001 -0.278926 -0.127679 0.951787 48.067780 62.904530 2.500001 -0.263447 -0.142550 0.954083 47.309990 63.906979 2.500001 -0.082274 -0.155248 0.984443 46.385601 64.398064 2.500001 -0.025878 -0.196875 0.980087 45.383808 64.317551 2.500001 0.015420 -0.199873 0.979701 44.408440 63.701160 2.500001 -0.232693 0.125587 -0.964408 43.552479 62.677849 2.500001 -0.300091 0.156567 -0.940974 42.826420 61.414768 2.500001 -0.369369 0.196475 -0.908275 42.207859 60.036179 2.500001 -0.555614 0.201118 -0.806749 41.684139 58.620060 2.500001 -0.607999 0.191823 -0.770416