任务内容
1、加载las点云数据;
2、给加载的las点云赋值随机色;
3、保存点云数据
4、要求批量处理
参考建议:
此过程我采用加载las文件的python库是:pylas
代码:
import pylas
import random
import os
def read_file(path,file_type): #path 是指需要提取的文件夹所在路径;file_type是指文件的拓展名;txt格式的文件则输入‘.txt’
filenames = os.listdir(path)
filenames1 = []
for filename in filenames:
if os.path.splitext(filename)[1] == file_type:
filenames1.append(filename)
return filenames1
###显示文件名####
#file_name=read_file()
#print(file_name)
########
path='E:\\0 云南师范大学\\桂林理工大学\\python学习\\2020.1221\\1mrgb\\'
list_dir = read_file(path,'.las')
# a=len(list_dir)
# print(list_dir[1])
for i in range(len(list_dir)):
print('正在处理第',i+1,'个!文件名为:',list_dir[i],)
# Directly read and write las
path_file=path+list_dir[i]
las = pylas.read(path_file)
las = pylas.convert(las, point_format_id=2)
# print(las.points)
# # Open data to inspect header and then read
# with pylas.open(path_file) as f:
# if f.header.point_count < 10 ** 8:
# las = f.read()
# print(las.vlrs)
# print(las.points)
R=random.randint(0,255)
G=random.randint(0,255)
B=random.randint(0,255)
for j in range(len(las.points)):
array=las.points
array[j][9]=R
array[j][10]=G
array[j][11]=B
# print(array)
out_path_file=path+"out\\"+list_dir[i]
las.write(out_path_file)
运行结果:
没有添加随机色之前
添加随机色之后