python可视化点云数据

版权声明:未经本人许可,不得用于商业用途及传统媒体。转载请注明出处! https://blog.csdn.net/qikaihuting/article/details/82628006

requirements

代码

#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")

可视化结果

mayavi

图1.mayavi可视化结果
meshlab
图2.meshlab可视化结果

猜你喜欢

转载自blog.csdn.net/qikaihuting/article/details/82628006
今日推荐