Python converts the specified point cloud file (asc) to PCD format

Time:2019-4-30

cause

Because most of my point cloud files are.asc格式的,但最近用pcl做点云方面的研究,从ascFile topcdManual conversion of files is too cumbersome and inefficient, so we write an immature Python script to convert ASC files to PCD format files.
psThis script applies only to ASCII-encoded files and only to scattered point clouds

Start writing

From the analysis of the format of PCD files, we can see that the most fundamental requirement of the conversion from ASC to PCD is that the beginning of the file meets the requirements of PCD format. The most important problem is how to dynamically set it up.WIDTHandPOINTSFor scattered point clouds, both values can be expressedPointAcquisition of pointsCan be represented by the number of lines in ASC files.
The code is as follows:

#coding:utf-8
import time
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
# Statistical Source File Points
for line in file:
    count=count+1
print ("size is %d" %count)
file.close()

#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') # Note that there are spaces behind it
output.write(str(count))
output.write('\nHEIGHT')
Output. write (str (1)# mandatory type conversion, file input can only be STR format
output.write('\nPOINTS ')
output.write(str(count))
output.write('\nDATA ascii\n')
file1 = open(filename,"r")
all = file1.read()
output.write(all)
output.close()
file1.close()

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

Example

For example, the running time of the script is about 0.14s, which can basically meet its own needs.
Python converts the specified point cloud file (asc) to PCD format

Python converts the specified point cloud file (asc) to PCD format

Running the above script will automaticallyexample.asctranslate intoexample.pcd

Recommended Today

Air quality of major cities in China using Baidu map official website in Vue – Baidu map case code

1. Add in the directory public/index.html <script></script> 2. Add the following code to the page <template> <div id=”MapChart” ref=”MapChart” style=”width: 600px;height:400px;”></div> </template> <script> import * as echarts from ‘echarts’ require(‘echarts/extension/bmap/bmap’) export default { name: ‘MapDemo’, components: {}, props: {}, data () { return { } }, computed: {}, watch: {}, created () {}, mounted () […]