python讀取三維點雲球坐標數據並動態生成三維圖像與着色


關鍵步驟:

1.首先通過讀取.txt文本數據並進行一系列字符串處理,提取顯示所需要的相關數據矩陣

2.然后利用python的matplotlib庫來進行動態三維顯示

備注:matplotlib在顯示2d數據可視化方面有着絕對的優勢,但是在三維點雲顯示方面則存在很多問題,首先一個就是顯示幾千幾萬點以上甚至更多三維點的時候,電腦CPU明顯跟不上,計算機顯示明顯變得卡頓,所以當需要顯示更多的點的時候,建議使用python的另一個利用GPU渲染的庫vispy,本人親測,普通i5,GTX750台式機顯示個幾千萬個點是毫無壓力的

import numpy as np
import math
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
f=open('data.txt','r')
point=f.read()
f.close()
l1=point.replace('\n',',')
l2=l1.replace(' ',',')
l3=l2.split(',')
#print(l3)

m1=np.array(l3)
m2=m1.reshape(2664,6)

m3=[]
for each in m2:
	each_line=list(map(lambda x:float(x),each))
	m3.append(each_line)
m4=np.array(m3)
print(m4)

yaw=[i[0] for i in m4]
pitch=[i[1] for i in m4]
radius=[i[2] for i in m4]

c1=[i[3:6] for i in m4]
c2=np.array(c1)
c3=c2.reshape(2664,3)
print(c3)

x=[]
y=[]
z=[]
for i in range(len(m4)):
	x.append(radius[i]*math.sin(0.0174532924*pitch[i])*math.sin(0.0174532924*yaw[i]))
	y.append(radius[i]*math.cos(0.0174532924*pitch[i]))
	z.append(radius[i]*math.sin(0.0174532924*pitch[i])*math.cos(0.0174532924*yaw[i]))
# print(x)
# print(y)
# print(z)
def animate():
    return point
def init():
    return point
fig=plt.figure(figsize=(16,9),dpi=120)
ax=fig.add_subplot(111,projection='3d')

plt.title('point')
ax.set_xlabel('X Label')
ax.set_ylabel('Z Label')
ax.set_zlabel('Y Label')

anim = FuncAnimation(fig,animate, frames=np.arange(100), init_func=init,
                     interval=100, blit=True)
for i in range(2664):
	point=ax.scatter(z[i],x[i],y[i],c=(c3[i]/255),marker='.',s=10,linewidth=1,alpha=1,cmap='spectral')	
	plt.ion()
	plt.pause(0.01)
	plt.close
plt.show()

觀看視頻

數據點未進行着色時的情況顯示如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM