taif:可视化pcd多视角

windows下运行pointnet(全)_taifyang的博客-CSDN博客_pointnet运行

GitHub - charlesq34/pointnet: PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation

#!/usr/bin/env python# coding: utf-8import open3d as o3dimport numpy as npimport osimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Ddef pts2pcd(pts,nrm=None,col=None): assert (len(pts) == 0 or len(pts[0]) == 3) pcd = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(pts)) if nrm is not None: assert (len(nrm) == 0 or len(nrm[0]) == 3) pcd.normals = o3d.utility.Vector3dVector(nrm) if col is not None: assert (len(col[0]) == 3) pcd.colors = o3d.utility.Vector3dVector(col) return pcddef read_pcd_label(file_pcd): # PCD ascii file format # https://pcl.readthedocs.io/projects/tutorials/en/latest/pcd_file_format.html if not os.path.exists(file_pcd): raise Exception(f'[ERROR] file not exists {file_pcd}') with open(file_pcd) as f: lines_header = [f.readline()[:-1].split() for _ in range(10)] version_number = lines_header[0] assert(version_number[0]=='VERSION' and version_number[1] == '.7') data_format = lines_header[9] assert (data_format[0] == 'DATA' and data_format[1] == 'ascii') fields_format = lines_header[1] assert (fields_format[0] == 'FIELDS' and fields_format[1] == 'x' and fields_format[2] == 'y' and fields_format[3] == 'z') idx_properties = 4 has_colors = fields_format[idx_properties] == 'rgb' if has_colors: idx_properties += 1 has_labels = fields_format[idx_properties] == 'label' if has_labels: idx_properties += 1 has_object = fields_format[idx_properties] == 'object' if has_object: idx_properties += 1 points_number = lines_header[7] assert (points_number[0] == 'POINTS') points_size = int(points_number[1]) pts = np.loadtxt(file_pcd, skiprows=10) if len(pts) != points_size: raise Exception('[ERROR] problem reading points.') idx_properties = 3 colors = None if has_colors: colors = pts[:, idx_properties].astype(int) idx_properties += 1 # TODO: mask integer RGB to obtain each 8-bit channel colors -= colors.min() if np.count_nonzero(colors) != 0: if len(colors) != points_size: raise Exception('[ERROR] problem reading colors.') colors /= colors.max() colors = np.stack([colors] * 3, axis=2) else: print('[WARNING] all points have same color.') colors = None labels = None if has_labels: labels = pts[:, idx_properties].astype(int) idx_properties += 1 if len(labels) != points_size: raise Exception('[ERROR] problem reading labels.') pcd = pts2pcd(pts[:, :3],col=colors) pcd.estimate_normals() pcd.orient_normals_towards_camera_location() return pcd, labelsif __name__ == "__main__": viz = False path = "" save_result_path = "result" if not os.path.exists(save_result_path): os.makedirs(save_result_path) lines = os.listdir(path) for line in lines: filename = os.path.join(path, line) # pcd = o3d.io.read_point_cloud(filename) # pts = np.asarray(pcd.points) # print('pts',pts.shape) pcd,labels = read_pcd_label(filename) pts = np.asarray(pcd.points) print('pts',pts.shape) print('labels',labels.shape) unique_labels = np.unique(labels) print('unique_labels',unique_labels) # show only one view ''' xs,ys,zs = pts[:,0],pts[:,1],pts[:,2] fig = plt.figure(figsize=[20,10]) ax = fig.add_subplot(projection='3d') ax.scatter(xs,ys,zs,c=labels) ax.set(xlabel='X',ylabel='Y',zlabel='Z') ax.view_init(elev=-60, azim=80) if viz: plt.show() fig.savefig(os.path.join(save_result_path, line + '_viz1' + '.jpg'), bbox_inches='tight') plt.close(fig) ''' xs,ys,zs = pts[:,0],pts[:,1],pts[:,2] # change these variables according to: # https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.html grid = 3,3 el_min,el_max = -100, 80 az_min,az_max = -80, 120 fig = plt.figure(figsize=[25,15], tight_layout=True) #fig = plt.figure(figsize=[20,10]) for i in range(grid[0]): for j in range(grid[1]): nr_view = i*grid[1]+j+1 ax = fig.add_subplot(grid[0],grid[1],nr_view,projection='3d') ax.view_init(el_min + i*(el_max-el_min)/grid[0], az_min + j*(az_max-az_min)/grid[1]) ax.scatter(xs,ys,zs,c=labels) #ax.set(xlabel='X',ylabel='Y',zlabel='Z') #ax.set_title(f'view {nr_view}') ax.grid(False) ax.axis('off') ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) if viz: plt.show() # save plot to image fig.savefig(os.path.join(save_result_path, line + '_viz2' + '.jpg'), bbox_inches='tight') plt.close(fig)

相关推荐

相关文章