np.unique()函数及其应用(VoxelNet)

  • 用于从lidar点云生成无重复的体素

对于一维数组或者列表,去除其中重复的元素 ,并由小到大返回返回新数组或列表;

numpy.unique(arr, return_index, return_inverse, return_counts)

arr:输入数组,如果不是一维数组则会展开
return_index:如果为 true,返回新列表元素在旧列表中的位置(下标),并以列表形式存储。
return_inverse:如果为true,返回旧列表元素在新列表中的位置(下标),并以列表形式存储。
return_counts:如果为 true,返回去重数组中的元素在原数组中的出现次数。、

  • 简单的例子:
>>> b
array([[ 1.        ,  0.63512393,  1.        ],
       [ 1.        ,  0.87220436,  0.82533101],
       [ 0.04865729, -0.83059518,  1.51482085],
       [-1.26829021,  0.121988  , -0.24346367]])
>>> c, i, v = np.unique(b, axis=0, return_inverse=True, return_counts=True)
>>> c
array([[-1.26829021,  0.121988  , -0.24346367],
       [ 0.04865729, -0.83059518,  1.51482085],
       [ 1.        ,  1.        ,  1.        ]])
>>> i
array([2, 2, 1, 0])
>>> v
array([1, 1, 2])
  • 源码中例子:
    def preprocess(self, lidar):

        # shuffling the points
        np.random.shuffle(lidar)
        # pts在voxel中的坐标,在preprocess之前已经进行范围滤波,排除规定距离外的点云
        voxel_coords = ((lidar[:, :3] - np.array([self.xrange[0], self.yrange[0], self.zrange[0]])) / (
                        self.vw, self.vh, self.vd)).astype(np.int32)    # 转换成int整数类型

        # convert to  (D,W,H)也就是在voxel坐标系中的坐标
        voxel_coords = voxel_coords[:,[2,1,0]]
        # compute pts belongs to same x,y coords,由小到大排序D值,去除在axis=0,也就是[x,y,z]三个xyz坐标完全重复的体素
        voxel_coords, inv_ind, voxel_counts = np.unique(voxel_coords, axis=0, \
                                                  return_inverse=True, return_counts=True)
        # 这里生成的inv-ind是以索引的形式,从0~不重复的点云总量-1,以[2,0,0,...1,1,1]这种形式排列
        voxel_features = []
        # 遍历每一个voxel,并聚合其包含在内的所有原始点云特征
        for i in range(len(voxel_coords)):
            voxel = np.zeros((self.T, 7), dtype=np.float32)    # [35, 7]
            pts = lidar[inv_ind == i]   # 包含在体素内的点云
            if voxel_counts[i] > self.T:    # if pts > max buffer, reduce to max-num:T
                pts = pts[:self.T, :]
                voxel_counts[i] = self.T
            # augment the points: concatenate pts with means offset
            voxel[:pts.shape[0], :] = np.concatenate((pts, pts[:, :3] - np.mean(pts[:, :3], 0)), axis=1)
            voxel_features.append(voxel)

        return np.array(voxel_features), voxel_coords   # 每个voxel对应[35, 7]的点云,为3D数据[voxel_nums, 35, 7],缺少的以0补齐,多的则剔除

参考:https://blog.csdn.net/weixin_44211968/article/details/123772201

猜你喜欢

转载自blog.csdn.net/weixin_43253464/article/details/127104335