本文主要是介绍mayavi 显示点云 检测结果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
可视化代码:
mayavi 显示点云 检测结果
输入文件格式是hdf5
字段:
f = h5py.File(args.filename, 'r') pcls = f['point_cloud'] lidar_pose = f['lidar_pose'] vbbs = f['vehicle_boundingbox'] pbbs = f['pedestrian_boundingbox']
可视化代码:
import argparse
import numpy as np
import h5py
from mayavi import mlabdef getTransform(x, y, z, pitch, yaw, roll, degrees=True):'''Given location x,y,z and pitch, yaw, roll, obtain the matrix that convert from local to global CS using the left-handed system from UE4'''if degrees:pitch, yaw, roll = [np.radians(x) for x in [pitch, yaw, roll]]cy,sy = np.cos(yaw), np.sin(yaw)cr,sr = np.cos(roll), np.sin(roll)cp,sp = np.cos(pitch), np.sin(pitch)mat = np.array([cp * cy, cy * sp * sr - sy * cr, -cy * sp * cr - sy * sr, x, \cp * sy, sy * sp * sr + cy * cr, -sy * sp * cr + cy * sr, y, \sp, -cp * sr, cp * cr, z, \0, 0, 0, 1], dtype=np.float).reshape(4,4)return matdef transformPoints(transformMatrix, pts, inverse=False):'''Given a transformation matrix [4,4] convert pts [N,3] or [N,4] (last coordinate is intensity)'''#Check if intensity is availableif pts.shape[1] == 4:#split intensity from 3D coordinates, add homogeneus coordinateintensity = pts[:,-1,np.newaxis].copy()pts[:,-1] = 1else:#add homogeneus coordinateintensity = Nonepts = np.concatenate([pts, np.ones((pts.shape[0],1))], axis=1)#perform transformationmat = np.array(transformMatrix)if inverse:mat = np.linalg.inv(mat)ptst = pts @ mat.Tptst = ptst[:,:3]#merge intensity backif intensity is not None:ptst = np.concatenate([ptst,intensity], axis=1)return ptstdef updateBoundingBox(x,y,z,yaw,pitch,w,l,h, vis_bb):#Create 8 corner pointscpts = 0.5*np.array([[-1,1,-1],[1,1,-1],[1,-1,-1],[-1,-1,-1],[-1,1,1],[1,1,1],[1,-1,1],[-1,-1,1]])cpts *= np.array([[w,l,h]])cpts = transformPoints(getTransform(x,y,z,pitch,yaw,0), cpts)#list of 16 points to create whole BBpts = cpts[[0,3,7,3,2,6,2,1,5,1,0,4,7,6,5,4],:]#update visvis_bb.mlab_source.reset(x=pts[:,0], y=pts[:,1], z=pts[:,2])def main(args):#Load file/dataf = h5py.File(args.filename, 'r')pcls = f['point_cloud']lidar_pose = f['lidar_pose']vbbs = f['vehicle_boundingbox']pbbs = f['pedestrian_boundingbox']nframes = pcls.shape[0]nvehicles = pcls.shape[1]npedestrians = pbbs.shape[1]#Create Mayavi Visualisationfig = mlab.figure(size=(960,540), bgcolor=(0.05,0.05,0.05))zeros = np.zeros(pcls.shape[1]*pcls.shape[2])vis = mlab.points3d(zeros, zeros, zeros, zeros, mode='point', figure=fig)zeros = np.zeros(16)vis_vbbs = [mlab.plot3d(zeros, zeros, zeros, zeros, color=(0,1,0), tube_radius=None, line_width=1, figure=fig) for i in range(nvehicles)]vis_pbbs = [mlab.plot3d(zeros, zeros, zeros, zeros, color=(0,1,1), tube_radius=None, line_width=1, figure=fig) for i in range(npedestrians)]#Iterate through frames@mlab.animate(delay=100)def anim():for frame in range(nframes):print(f'Frame {frame}')#Update pedestrian bounding boxesfor i in range(npedestrians):updateBoundingBox(*pbbs[frame, i].tolist(), vis_pbbs[i])fusedPCL = []for i in range(nvehicles):#Get PCL for the given vehicle in the global Coordinate Systempcl = pcls[frame,i]transform = getTransform(*lidar_pose[frame,i].tolist())pcl_global = transformPoints(transform, pcl)fusedPCL.append(pcl_global)#Update the vehicle BB visualisationupdateBoundingBox(*vbbs[frame,i].tolist(), vis_vbbs[i])#Update PCL visualisation with MayavifusedPCL = np.concatenate(fusedPCL, axis=0)vis.mlab_source.set(x=fusedPCL[:,0], y=fusedPCL[:,1], z=fusedPCL[:,2], scalars=fusedPCL[:,3])#Set top-view if first frameif frame == 0:mlab.gcf().scene.z_plus_view()yieldanim()mlab.show()if __name__ == '__main__':argparser = argparse.ArgumentParser()argparser.add_argument('filename',type=str,help='Path to snippet to be visualised')args = argparser.parse_args()try:main(args)except KeyboardInterrupt:passfinally:print('Finished visualisation!')
这篇关于mayavi 显示点云 检测结果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!