资讯专栏INFORMATION COLUMN

python将指定点云文件(asc)转换为PCD格式

cyrils / 2461人阅读

摘要:起因由于自己大部分的点云文件都是格式的,但最近用做点云方面的研究,从文件到文件手动转化太麻烦,而且效率较低,故此写一个不太成熟的脚本实现从文件到格式文件的转换。

起因

由于自己大部分的点云文件都是.asc格式的,但最近用pcl做点云方面的研究,从asc文件到pcd文件手动转化太麻烦,而且效率较低,故此写一个不太成熟的python脚本实现从asc文件到pcd格式文件的转换。
ps此脚本只适用于ASCII编码的文件,并且只适用于散乱点云

着手写作

分析pcd文件的格式可知,从asc到pcd转换最根本要求就是其文件开头符合pcd格式要求,其中最主要的问题是的是如何动态设置WIDTHPOINTS的值,对于散乱点云,这两个值都可以表示点数.点数的获得可用asc文件的行数表示.
代码如下:

</>复制代码

  1. #coding:utf-8
  2. import time
  3. from sys import argv
  4. script ,filename = argv
  5. print ("the input file name is:%r." %filename)
  6. start = time.time()
  7. print ("open the file...")
  8. file = open(filename,"r+")
  9. count = 0
  10. #统计源文件的点数
  11. for line in file:
  12. count=count+1
  13. print ("size is %d" %count)
  14. file.close()
  15. #output = open("out.pcd","w+")
  16. f_prefix = filename.split(".")[0]
  17. output_filename = "{prefix}.pcd".format(prefix=f_prefix)
  18. output = open(output_filename,"w+")
  19. list = ["# .PCD v.5 - Point Cloud Data file format
  20. ","VERSION .5
  21. ","FIELDS x y z
  22. ","SIZE 4 4 4
  23. ","TYPE F F F
  24. ","COUNT 1 1 1
  25. "]
  26. output.writelines(list)
  27. output.write("WIDTH ") #注意后边有空格
  28. output.write(str(count))
  29. output.write("
  30. HEIGHT")
  31. output.write(str(1)) #强制类型转换,文件的输入只能是str格式
  32. output.write("
  33. POINTS ")
  34. output.write(str(count))
  35. output.write("
  36. DATA ascii
  37. ")
  38. file1 = open(filename,"r")
  39. all = file1.read()
  40. output.write(all)
  41. output.close()
  42. file1.close()
  43. end = time.time()
  44. print ("run time is: ", end-start)
实例

以20万左右的点云为例,该脚本运行时间大约在0.14s左右,基本可以满足自己的需求

运行以上脚本,便可自动将example.asc转化为example.pcd

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/45525.html

相关文章

发表评论

0条评论

最新活动
阅读需要支付1元查看
<