不同点云文件的读取,可视化方式总结。
直接将txt点云文件拖入打开的cloudcompare中,该软件会自动识别xyz及nxnynz。
txt点云文件,每行代表三维空间中的一个点,共6列,分别是xyz,nxnynz
#-*-coding:utf-8-*-
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def readXYZfile(filename, Separator):
data = [[], [], []]
f = open(filename,'r')
line = f.readline()
num = 0
while line: #按行读入点云(6列)
c,d,e,_,_,_ = line.split(Separator)
data[0].append(c) #X坐标
data[1].append(d) #Y坐标
data[2].append(e) #Z坐标
num = num + 1
line = f.readline()
f.close()
#string型转float型 ,下面的只有按照xzy的顺序才能正常显示,调换zy显示别扭,但是在cloudcompare软件中显示都正常
x = [ float(data[0] ) for data[0] in data[0] ]
z = [ float(data[1] ) for data[1] in data[1] ]
y = [ float(data[2] ) for data[2] in data[2] ]
print("读入点的个数为:{}个。".format(num))
point = [x,y,z]
return point
#三维离散点图显示点云
def displayPoint(data,title):
#解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#散点图参数设置
fig=plt.figure()
ax=Axes3D(fig)
ax.set_title(title)
ax.scatter3D(data[0], data[1],data[2], c = 'r', marker = '.')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
if __name__ == "__main__":
data = readXYZfile("airplane_0003.txt",',')
displayPoint(data, "airplane_0003")
#-*-coding:utf-8-*-
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from ipdb import set_trace
def readXYZfile(filename, Separator):
data = np.loadtxt(filename,delimiter = Separator)[:, 0:3]
point = [data[:,0].tolist(),data[:,1].tolist(),data[:,2].tolist()]
return point
#三维离散点图显示点云
def displayPoint(data,title):
#散点图参数设置
fig=plt.figure()
ax=Axes3D(fig)
ax.set_title(title)
ax.scatter3D(data[0], data[2], data[1], c = 'r', marker = '.')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
if __name__ == "__main__":
data = readXYZfile("airplane_0003.txt",',')
displayPoint(data, "airplane_0003")
#-*-coding:utf-8-*-
import numpy as np
import open3d as o3d
points = np.loadtxt('airplane_0003.txt', delimiter=',')[:, 0:3]
point_cloud = o3d.geometry.PointCloud()
point_cloud.points = o3d.utility.Vector3dVector(points)
# 设置点云显示的颜色(0-1)
point_cloud.paint_uniform_color([0, 1, 0])
o3d.visualization.draw_geometries([point_cloud])
# o3d.visualization.draw([point_cloud])
# 保存点云到ply
# o3d.io.write_point_cloud("test.ply", point_cloud)
简单总结了点云读取及可视化的方式,了解了部分点云格式的存储方式,未来了解更多点云格式后再继续补充。