python學習筆記之視覺化點雲
阿新 • • 發佈:2018-12-09
requirements
- meshlab,下載地址
- 注意事項
- 以上方法二選一,mayavi安裝可能比較麻煩,如果要安裝,建議看官網教程;
程式碼
#coding=utf-8 import os #視覺化依賴的庫是pyside,也可以是pyqt os.environ['QT_API']='pyside' import numpy as np from mayavi import mlab def get_image(file_name,height=160,width=160): """這裡的點雲資料的每一個通道大小為160x160; Construct an array from data in a text or binary file. """ file_data = np.fromfile(file_name, np.float32) # file_data.shape image = np.reshape(file_data, [width,height,3]) return image def show(file_path,save_dir = None): XYZ = get_image(file_path) #len(X)=size,len(X[index])=size print(np.max(XYZ[:,:,2])) print(np.min(XYZ[:,:,2])) x = np.squeeze(XYZ[:,:,0]).tolist() y = np.squeeze(XYZ[:,:,1]).tolist() z = np.squeeze(XYZ[:,:,2]).tolist() #print("max value is {},min value is {}".format(max(z),min(z))) if save_dir != None: if not os.path.exists(save_dir): os.mkdir(save_dir) temp = os.path.splitext(file_path)[0] pre_name = os.path.basename(temp) sub_dir = file_path.split("\\")[-2] new_subdir = os.path.join(save_dir,sub_dir) if not os.path.exists(new_subdir): os.mkdir(new_subdir) new_name = os.path.join(new_subdir,pre_name) #將每一個維度的對應位置的(x,y,z)值存入txt,並且每一行開頭新增字母v,表示三維頂點,值之間用空格隔開。 #隨後將.txt副檔名改為.obj,這樣meshlab就可以讀取.obj檔案,視覺化點雲圖像。 with open(new_name+'.txt','w') as f: for i in range(len(x)): for j in range(len(x)): f.write("v"+" "+str(x[i][j])+" "+str(y[i][j])+" "+str(z[i][j])+"\n") #直接用mayavi視覺化 mlab.points3d(x,y,z,mode='point') mlab.show() if __name__ == '__main__': show(r"\yourpath\XXX.bin",r"\savepath\pointclouds_obj")