点云体素化
- def preprocess(self, lidar):
- #[N,4]
- # This func cluster the points in the same voxel.
- # shuffling the points
- np.random.shuffle(lidar)
- #把每个点分配到体素里,点坐标减去起始,除以每个体素的大小
- voxel_coords = ((lidar[:, :3] - np.array([self.xrange[0], self.yrange[0], self.zrange[0]])) / (
- self.vw, self.vh, self.vd)).astype(np.int32)
-
- # convert to (D, H, W) [N,3]
- voxel_coords = voxel_coords[:,[2,1,0]]
- #np.unique该函数是去除数组中的重复数字,并进行排序之后输出。axis=0(返回二维数组的唯一行)return_inverse返回旧列表里面的值在新列表里的索引
- #return_counts返回新列表里面的行在旧列表里面的个数。
- voxel_coords, inv_ind, voxel_counts = np.unique(voxel_coords, axis=0, \
- return_inverse=True, return_counts=True)
- #[N1,3],[N,1],[N1,1]N1表示非空体素的个数,voxel_counts统计每个非空体素里面的点个数
-
- voxel_features = []
-
- for i in range(len(voxel_coords)):
- #self.T每个体素里面最多点数
- voxel = np.zeros((self.T, 7), dtype=np.float32)
- #得到这个体素里面的点
- pts = lidar[inv_ind == i]
- if voxel_counts[i] > self.T:
- pts = pts[:self.T, :]
- voxel_counts[i] = self.T
- # 进行数据扩充,np.concatenate不增加维度对两向量相加
- voxel[:pts.shape[0], :] = np.concatenate((pts, pts[:, :3] - np.mean(pts[:, :3], 0)), axis=1)
- voxel_features.append(voxel)
- #voxel_features[N1,35,7],voxel_coords[N1,3]
- return np.array(voxel_features), voxel_coords
密集特征得到二维伪图像特征
- def voxel_indexing(self, sparse_features, coords):
- #sparse_features[B,N1,C] coords[N1,4]
- dim = sparse_features.shape[-1]
-
- dense_feature = torch.zeros(cfg.N, cfg.D, cfg.H, cfg.W, dim).to(cfg.device)
-
- dense_feature[coords[:,0], coords[:,1], coords[:,2], coords[:,3], :]= sparse_features
-
- return dense_feature.permute(0, 4, 1, 2, 3)