def __init__(self, nr, np_point_cloud, o3d_pcd, nns, flann, wall_idxs, label_col, orig_labels, orig_indices, orig_counts, voxel_size, sample_size, voxel_mode): super(VoxScene, self).__init__(nr, np_point_cloud, o3d_pcd, nns, flann, wall_idxs, label_col, orig_labels, orig_indices, orig_counts) if voxel_mode == "None": points = pd.DataFrame(self.np_point_cloud[:, :3]) points.columns = ["x", "y", "z"] cloud = PyntCloud(points) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=voxel_size, n_y=voxel_size, n_z=voxel_size) self.voxelgrid = cloud.structures[voxelgrid_id] self.voxels = self.voxelgrid.get_feature_vector(mode="binary") elif voxel_mode == "Custom": points = pd.DataFrame(self.np_point_cloud[:, :7]) points.columns = ["x", "y", "z", "nx", "ny", "nz", "c"] cloud = PyntCloud(points) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=voxel_size, n_y=voxel_size, n_z=voxel_size) self.voxelgrid = cloud.structures[voxelgrid_id] _, self.voxels = self.voxelgrid.get_feature_vector(mode="custom")
def test_points(): """PyntCloud.points. - Points must be a pandas DataFrame - DataFrame must have at least "x", "y" and "z" named columns - When PyntCloud.points is re-assigned all structures must be removed """ points = np.random.rand(10, 3) # not dataframe with pytest.raises(TypeError): PyntCloud(points) points = pd.DataFrame(points) # not x, y, z with pytest.raises(ValueError): PyntCloud(points) points = pd.DataFrame(points.values, columns=["x", "y", "z"]) assert PyntCloud(points) cloud = PyntCloud(points) cloud.add_structure("voxelgrid") assert len(cloud.structures) == 1 # dummy filter x_above_05 = cloud.points["x"] > 0.5 cloud.points = cloud.points[x_above_05] assert len(cloud.structures) == 0
def filtre_ROR(point, k=10, r=5): ## Statistical Outlier Removal pointss = PyntCloud(points=point) kdtree_ids = pointss.add_structure("kdtree", leafsize=16) new_filtre = pointss.get_filter("ROR", kdtree_id=kdtree_ids, and_apply=False, k=k, r=r) return new_filtre
def test_regular_bounding_box_changes_the_shape_of_the_bounding_box(x, y, z): cloud = PyntCloud(pd.DataFrame( data={ "x": np.array(x, dtype=np.float32), "y": np.array(y, dtype=np.float32), "z": np.array(z, dtype=np.float32) })) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=2, n_y=2, n_z=2, regular_bounding_box=False) voxelgrid = cloud.structures[voxelgrid_id] irregular_last_centroid = voxelgrid.voxel_centers[-1] voxelgrid_id = cloud.add_structure("voxelgrid", n_x=2, n_y=2, n_z=2) voxelgrid = cloud.structures[voxelgrid_id] regular_last_centroid = voxelgrid.voxel_centers[-1] assert np.all(irregular_last_centroid <= regular_last_centroid)
def pyntcloud_with_clusters_and_voxelgrid_id(): xyz = np.random.rand(10, 3) xyz[:5, :] += 10 cloud = PyntCloud(pd.DataFrame( data=xyz, columns=["x", "y", "z"])) voxelgrid_id = cloud.add_structure("voxelgrid", sizes=[0.5, 0.5, 0.5]) return cloud, voxelgrid_id
def convertPlyToBinvox(cloud: PyntCloud) -> br.Voxels: voxelgrid_id = cloud.add_structure("voxelgrid", n_x=32, n_y=32, n_z=32) voxelgrid = cloud.structures[voxelgrid_id] x_cords = voxelgrid.voxel_x y_cords = voxelgrid.voxel_y z_cords = voxelgrid.voxel_z voxel = np.zeros((32, 32, 32)).astype(np.bool) for x, y, z in zip(x_cords, y_cords, z_cords): voxel[x][y][z] = True return br.Voxels(voxel, (32, 32, 32), (0, 0, 0), 1, "xyz")
def __getitem__(self, item): sample_idx = item // len(CAMS) sample = self.lyft_data.sample[sample_idx] sd_lidar = self.lyft_data.get('sample_data', sample['data']['LIDAR_TOP']) cs_lidar = self.lyft_data.get('calibrated_sensor', sd_lidar['calibrated_sensor_token']) pc = LidarPointCloud.from_file(self.lyft_data.data_path / sd_lidar['filename']) cam = CAMS[item % len(CAMS)] cam_token = sample['data'][cam] sd_cam = self.lyft_data.get('sample_data', cam_token) cs_cam = self.lyft_data.get('calibrated_sensor', sd_cam['calibrated_sensor_token']) img = Image.open(str(self.lyft_data.data_path / cam["filename"])) img = transform(img) lidar_2_ego = transform_matrix(cs_lidar['translation'], Quaternion(cs_lidar['rotation']), inverse=False) ego_2_cam = transform_matrix(cs_cam['translation'], Quaternion(cs_cam['rotation']), inverse=True) cam_2_bev = Quaternion(axis=[1, 0, 0], angle=-np.pi / 2).transformation_matrix # lidar_2_cam = ego_2_cam @ lidar_2_ego lidar_2_bevcam = cam_2_bev @ ego_2_cam @ lidar_2_ego points = view_points(pc.points[:3, :], lidar_2_bevcam, normalize=False) points = clip_points(points) points = pd.DataFrame(np.swapaxes(points, 0, 1), columns=['x', 'y', 'z']) points = PyntCloud(points) voxelgrid_id = points.add_structure("voxelgrid", size_x=0.1, size_y=0.1, size_z=0.2, regular_bounding_box=False) voxelgrid = points.structures[voxelgrid_id] occ_map = voxelgrid.get_feature_vector(mode='binary') padded_occ = np.zeros((800, 700, 30)) padded_occ[:occ_map.shape[0], :occ_map.shape[1], :occ_map. shape[2]] = occ_map return img, padded_occ
def voxalize_by_layer(event_cylindrical, layer, segments): event_cylindrical_layer_wise = event_cylindrical.loc[event_cylindrical.colors==layer, :] if event_cylindrical_layer_wise.shape[0] == 0: return np.zeros(shape=((len(segments[0])-1) * (len(segments[1])-1) * (len(segments[2])) )) ref_cloud = PyntCloud(event_cylindrical_layer_wise) voxelgrid_id = ref_cloud.add_structure("voxelgrid", segments=segments) feature_vector = ref_cloud.structures[voxelgrid_id].query_voxels(event_cylindrical_layer_wise.loc[:,['r','alpha','z']].values, event_cylindrical_layer_wise.loc[:,'E'].values) return feature_vector.reshape((-1,))
def _create_voxelgrid_from_pointcloud(self, pointcloud, augmentation=True): if self.voxelgrid_random_rotation == True and augmentation == True: pointcloud = self._rotate_point_cloud(pointcloud) # Create voxelgrid from pointcloud. pointcloud = PyntCloud(pointcloud) voxelgrid_id = pointcloud.add_structure("voxelgrid", size_x=self.voxel_size_meters, size_y=self.voxel_size_meters, size_z=self.voxel_size_meters) voxelgrid = pointcloud.structures[voxelgrid_id].get_feature_vector(mode="density") # Do the preprocessing. if preprocess == True: voxelgrid = utils.ensure_voxelgrid_shape(voxelgrid, self.voxelgrid_target_shape) assert voxelgrid.shape == self.voxelgrid_target_shape return voxelgrid
def pointcloud2mesh(self, pointcloud, mode="binary", n=24): ''' Generate mesh by point clouds :param pointcloud: Nx3 array original point clouds :param mode: method to generate mesh :param n: size of mesh :return: N*N*N array ''' point = pd.DataFrame(pointcloud) point.columns = ['x', 'y', 'z'] cloud = PyntCloud(point) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=n, n_y=n, n_z=n) voxelgrid = cloud.structures[voxelgrid_id] binary_feature_vector = voxelgrid.get_feature_vector(mode = mode) return binary_feature_vector
def convert_ptc_to_voxel(ptc, n_x=128, n_y=128, n_z=128): cloud = PyntCloud(ptc) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=n_x, n_y=n_y, n_z=n_z) voxelgrid = cloud.structures[voxelgrid_id] x_cords = voxelgrid.voxel_x y_cords = voxelgrid.voxel_y z_cords = voxelgrid.voxel_z voxel = np.zeros((n_x, n_y, n_z)).astype(np.bool) for x, y, z in zip(x_cords, y_cords, z_cords): voxel[x][y][z] = True # plot_voxel(voxel) return 1.0*voxel
class CorrespondingLidarPointCloud(): def __init__(self, pcl_path, pointsensor): self._points = np.fromfile(str(pcl_path), dtype=np.float32).reshape((-1, 5))[:, :3] self._pointsensor = pointsensor self._pc = PyntCloud(self._points.T) #[3, N] -> [N, 3] def getPointCloud(self): return self._points def getPointsensor(self): return self._pointsensor def getOccupancyMatrix(self, as_tensor=True): assert self._occupancy, "Call voxelize() first" if as_tensor: return torch.tensor(self._occupancy) return self._occupancy def getNeighbors(self): assert self._neighbors, "Call generateKNN() first" return self._neighbors def getKNN(self, k): return self._pc.get_neighbors(k=k) def nbr_points(self) -> int: return self._points.shape[1] def voxelize(self, size_x, size_y, size_z): voxelgrid_id = self._pc.add_structure("voxelgrid", size_x=size_x, size_y=size_y, size_z=size_z, regular_bounding_box=False) voxelgrid = self._pc.structures[voxelgrid_id] self._occupancy = voxelgrid.get_feature_vector(mode='binary') def generateKNN(self): self._neighbors = self._pc.get_neighbors() #(N, k) def translate(self, x): for i in range(3): self._points[i, :] = self._points[i, :] + x[i] def rotate(self, rot_matrix): self._points = np.dot(rot_matrix, self._points) def transform(self, transf_matrix): self._points = transf_matrix.dot(np.vstack((self._points, np.ones(self.nbr_points()))))
def voxelization_raw_data(self, human_body): dataset = pd.DataFrame({ 'x': human_body[:, 0], 'y': human_body[:, 1], 'z': human_body[:, 2] }) cloud = PyntCloud(dataset) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=32, n_y=32, n_z=32) voxelgrid = cloud.structures[voxelgrid_id] x_cords = voxelgrid.voxel_x y_cords = voxelgrid.voxel_y z_cords = voxelgrid.voxel_z voxel = np.zeros((32, 32, 32)).astype(np.bool) for x, y, z in zip(x_cords, y_cords, z_cords): voxel[x][y][z] = True return voxel
def voxelization(tensor, vox_size = 32): ''' input: # points * xyz output: ch * gridsize * gridsize * gridsize ch is occupacy indomation 1: oppupied, 0: null ''' bunny = pd.DataFrame({'x': tensor[:,0], 'y': tensor[:,1], 'z': tensor[:,2]}) cloud = PyntCloud(pd.DataFrame(bunny)) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=vox_size, n_y=vox_size, n_z=vox_size) voxelgrid = cloud.structures[voxelgrid_id] x_cords = voxelgrid.voxel_x y_cords = voxelgrid.voxel_y z_cords = voxelgrid.voxel_z voxel = np.zeros((1, vox_size, vox_size, vox_size)).astype(np.float) for x, y, z in zip(x_cords, y_cords, z_cords): voxel[0][x][y][z] = 1. return voxel
def uniform_mesh_sampling(V, F, num_samples=2048, grid_res=64, mesh_samples=10**4): X_ = sample_faces(V, F, n_samples=mesh_samples) cloud = np_mat_to_pandas(X_) cloud = PyntCloud(cloud) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=grid_res, n_y=grid_res, n_z=grid_res) # new_cloud = cloud.get_sample("voxelgrid_nearest", voxelgrid_id=voxelgrid_id, as_PyntCloud=True) new_cloud = cloud.get_sample("voxelgrid_centroids", voxelgrid_id=voxelgrid_id, as_PyntCloud=True) X = new_cloud.points.as_matrix() X = sample_points(X, num_samples) return X
def pcl2vox(self, pcl, pcl_feature, n=24): ''' Generate mesh feature by point clouds feature :param pcl: BxPx3 tensor original point clouds (P is the number of point in each pointcloud) :param pcl_feature: BxNxC tensor feature of point clouds :param N: size of mesh :return: BxNxNxNxC tensor ''' self.in_channels = pcl_feature.shape[2] mesh_feature = np.zeros( (pcl_feature.size(0), n, n, n, self.in_channels)) vox2point_id = np.zeros( (pcl_feature.size(0), pcl.size(1), pcl.size(2))) pcl_feature = pcl_feature.cpu().data.numpy() pcl = pcl.cpu().data.numpy() for i in range(len(mesh_feature)): point = pd.DataFrame(pcl[i], columns=['x', 'y', 'z']) cloud = PyntCloud(point) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=n, n_y=n, n_z=n) voxelgrid = cloud.structures[voxelgrid_id] x_vox = voxelgrid.voxel_x y_vox = voxelgrid.voxel_y z_vox = voxelgrid.voxel_z vox2point_npid = np.concatenate([ x_vox.reshape((-1, 1)), y_vox.reshape((-1, 1)), z_vox.reshape((-1, 1)) ], axis=1) vox2point_id[i] = vox2point_npid for j in range(vox2point_id.shape[1]): x, y, z = vox2point_npid[j] mesh_feature[i, x, y, z] += pcl_feature[i, j] return Variable( torch.Tensor(mesh_feature)).cuda(), vox2point_id.astype(int)
def voxelization_raw_data(self, human_body): dataset = pd.DataFrame({ 'x': human_body[:, 0], 'y': human_body[:, 1], 'z': human_body[:, 2] }) cloud = PyntCloud(dataset) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=32, n_y=32, n_z=32) voxelgrid = cloud.structures[voxelgrid_id] x_cords = voxelgrid.voxel_x y_cords = voxelgrid.voxel_y z_cords = voxelgrid.voxel_z voxel = np.zeros((32, 32, 32)).astype(np.bool) for x, y, z in zip(x_cords, y_cords, z_cords): voxel[x][y][z] = True if self.show_plt: fig = plt.figure() ax = fig.gca(projection='3d') ax.voxels(voxel) plt.show(block=False) return voxel
print(points.shape) axes_xlimit = 70 axes_ylimit = 40 x = points[:3, :] x = np.swapaxes(x, 0, 1) #x = x[np.where(([0, -40] <= x[:, :2]) & (x[:, :2] <= [70, 40]))] x = x[(x[:, 0] >= 0) & (x[:, 0] <= 70) & (x[:, 1] >= -40) & (x[:, 1] <= 40)] print(x.shape) df = pd.DataFrame(x, columns=['x', 'y', 'z']) print(df.describe()) cloud = PyntCloud(df) voxelgrid_id = cloud.add_structure("voxelgrid", n_x=512, n_y=448, n_z=32) voxelgrid = cloud.structures[voxelgrid_id] density_feature_vector = voxelgrid.get_feature_vector(mode="density") print(density_feature_vector.shape) new_cloud = cloud.get_sample("voxelgrid_nearest", n=8, voxelgrid_id=voxelgrid_id, as_PyntCloud=True) #new_cloud.plot() print(new_cloud) e_time = time.time() print(e_time - s_time) """ x_points = x[:, 0]
def processData(dataTemp): try: os.remove("cur_mesh.ply") os.remove("cur_mesh.obj") except: print("Fresh") f = open("data.txt", "w") print(dataTemp) data = dataTemp.decode("utf-8").replace(" ", "").split("\n") data = data[:max(0, len(data) - 2)] for d in range(len(data)): data[d] = data[d].split(",") data[d] = [float(e) for e in data[d]] data = np.array(data) average = np.array([np.average(data[:, i]) for i in range(3)]) average[2] = min(data[:, 2]) print(average) data = np.array([np.array(point) - average for point in data]) frame = pd.DataFrame(data=data, columns=['x', 'y', 'z']) pc = PyntCloud(frame) kd_id = pc.add_structure("kdtree") pc.get_filter("SOR", and_apply=True, kdtree_id=kd_id, k=20, z_max=0.4) convex_hull_id = pc.add_structure("convex_hull") convex_hull = pc.structures[convex_hull_id] pc.mesh = convex_hull.get_mesh() pc.to_file("cur_mesh.ply", also_save=["mesh"]) pc.to_file("cur_mesh.obj", also_save=["mesh", "points"]) for i in range(3): data[:, i] /= max(np.abs(data[:, i])) polarData = [] for r in range(len(data)): rho = np.sqrt(data[r][0]**2 + data[r][1]**2) if data[r][0] < 0: # if x < 0 phi = np.arctan2(data[r][1], data[r][0]) + math.pi else: phi = np.arctan2(data[r][1], data[r][0]) if phi < 0: phi += math.pi * 2 # phi from [0, 360) # round to nearest 20deg for now prop = 1.0 / 20.0 if phi < 0: phi += math.pi * 2 phi /= math.pi * 2.0 phi = min(phi // prop, 19) z = data[r][2] # round to 1/4 radius prop2 = 1.0 / 4.0 rho = min(rho // prop2, 3.0) # round to 1/6 height prop3 = 1.0 / 6.0 z = min(z // prop3, 5.0) polarData.append([int(rho), int(phi), int(z)]) #print(polarData) m_data = np.zeros((20, 4, 6)) for p in polarData: m_data[p[1], p[0], p[2]] += 1 string = "0b" for rows in range(m_data.shape[0]): for cols in range(m_data.shape[1]): for theta in range(m_data.shape[2]): if (m_data[rows, cols, theta] < 1): string += "0" m_data[rows, cols, theta] = 0 else: string += "1" m_data[rows, cols, theta] = 1 print(m_data) f.write(string) f.close()
#Cloud.points is a Pandas Dataframe. print(cloud.points.columns) #A grayscale column is formed for the GLCM calculations. cloud.points['grayscale'] = (cloud.points['red'] + cloud.points['green'] + cloud.points['blue']) / 3 cloud.points['grayscale'] = cloud.points['grayscale'].astype(int) #Voxelization of the point cloud to grid according to the sizes of the axis. #TODO : Understand and dry run the procedure being applied during the formation of this grid. #The cause of bad results. The sizes must be optimized according to the data to form good approximation of the point cloud. cloud.add_structure(name='voxelgrid', size_x=0.2, size_y=0.2, size_z=0.2) keys = [] for key in cloud.structures: keys.append(key) #Choosing the VoxelGrid from the structures of the Cloud object. voxel = cloud.structures[keys[0]] #Forming the 3D Matrices from the voxelization values. arr = np.zeros((voxel.x_y_z[0], voxel.x_y_z[1], voxel.x_y_z[2])) arr1 = np.zeros((voxel.x_y_z[0], voxel.x_y_z[1], voxel.x_y_z[2])) # for i,row in cloud.points.iterrows(): # # print(i) # # print(row['grayscale'])
p_z = npy_pointcloud[:, 2] df_pointcloud = pd.DataFrame(zip(p_x, p_y, p_z), columns=['x', 'y', 'z']) pyntcloud_scene = PyntCloud(df_pointcloud) # Get Number of Objects in the Scene category = npy_pointcloud[:, 3] category = np.unique(category) # counts only each unique number number_objects = category.shape[0] # ------------------------------------------ Voxilisation ----------------------------------------------------- # create Voxelgrid from Pointcloudscene voxelgrid_id = pyntcloud_scene.add_structure("voxelgrid", n_x=voxel_size, n_y=voxel_size, n_z=voxel_size) voxelscene = pyntcloud_scene.structures[voxelgrid_id] # Create binary array from Voxelscene binary_voxelscene = voxelscene.get_feature_vector(mode="binary") # Create a Voxelscene Container of Scenes scene_container_x[v] = binary_voxelscene scene_container_y[v] = number_objects v = v + 1 v = 0 print("Scene Creation done")
for feature in assets["features"]: properties = feature["properties"] if properties["FeatName"] + ".ply" in ref_folder: ply_file_path = properties["FeatName"] + ".ply" ref_ply_file = PyntCloud.from_file( os.path.join(ply_directory, "assets", "ref", ply_file_path)) test_ply_file = PyntCloud.from_file( os.path.join(ply_directory, "assets", "test", ply_file_path)) # Merge reference and test point clouds as pandas dataframes frames = pd.concat([ref_ply_file.points, test_ply_file.points]) merged_pcd = PyntCloud(frames) # Add voxel grid to merged file voxelgrid_id = merged_pcd.add_structure("voxelgrid", n_x=64, n_y=64, n_z=64) voxelgrid = merged_pcd.structures[voxelgrid_id] # Query voxel grid for those containing points ref_voxel_match = np.unique( voxelgrid.query(ref_ply_file.points[["x", "y", "z"]].values)) test_voxel_match = np.unique( voxelgrid.query(test_ply_file.points[["x", "y", "z"]].values)) # Intersect populated voxel grids intersected_voxels = np.intersect1d(ref_voxel_match, test_voxel_match, assume_unique=True) ref_only = np.setdiff1d(ref_voxel_match, test_voxel_match)
from pyntcloud import PyntCloud import numpy as np import pandas as pd num_points = 100 points = pd.DataFrame(np.random.rand(num_points, 6)) points.columns = ["x", "y", "z", "r", "g", "b"] #print(points.values) cloud = PyntCloud(points) #cloud = PyntCloud.from_file("D:/HSD/Libs/pyntcloud/examples/data/ankylosaurus_mesh.ply") voxelgrid_id = cloud.add_structure("voxelgrid", n_x=5, n_y=5, n_z=5) voxelgrid = cloud.structures[voxelgrid_id] #voxels, colors = voxelgrid.get_feature_vector(mode="color") voxelgrid.plot(d=4, mode="color", edge_color="none") #voxelgrid.plot(d=4, mode="binary") voxels, colors = voxelgrid.get_feature_vector(mode="color") # removing unneccesary alpha value mcolor = colors[:, :, :, :3] #print(mcolor.shape) #print(mcolor) print("Done")