使用逆索引实现更快的体素化

问题描述 投票:0回答:1

我正在使用以下代码来获取体素化点云带有逆索引,以便我可以计算例如对下采样点云进行掩模,然后通过逆索引获取原始点,将其应用到原始点云上。

def voxelize(points, voxel_size):
    voxel_indices = np.floor(points / voxel_size).astype(np.int32)
    unique_voxel_indices, inverse_indices = np.unique(
        voxel_indices, axis=0, return_inverse=True
    )
    voxel_centers = (unique_voxel_indices + 0.5) * voxel_size
    return voxel_indices, unique_voxel_indices, voxel_centers, inverse_indices

但是,这段代码正在成为我的瓶颈,因为它被执行了数千次。

类似的问题,我找到了建议的解决方案来加快速度但没有逆索引。 Numba 也没有实现此版本的

np.unique
。有没有我缺少的任何方法,该方法明显更快,但也提供索引计算。

python
1个回答
0
投票

您也许可以做您需要的事情,而无需

np.unique

voxel_indices = np.floor(points / voxel_size)
将所有点映射到下采样网格中相应体素的角点坐标。我们有
voxel_indices.shape == (N, 3)
N
点。

正如变量名称

voxel_indices
所示,该数组包含每个点在下采样体素网格中的索引。假设您定义了下采样体素网格,使得下采样空间中具有角坐标
[i,j,k]
的体素对应于点空间中的角坐标
[i*voxel_size, j*voxel_size, k*voxel_size]
。 (无论如何,您都可以通过相对于原点适当移动点云来构建来轻松实现这一点)。

然后,您在下采样的体素网格上定义一个掩模。我们称之为

mask_downsampled
。它具有形状
(I,J,K)
,并且具有
mask_downsampled[i,j,k] == True
当且仅当具有角坐标
[i,j,k]
的下采样体素应被屏蔽时的属性。

您可以将面膜应用到您的原始点,如下所示:

# get the mask for each point - shape (N, 3)
mask = mask_downsampled[voxel_indices[:, 0], voxel_indices[:, 1], voxel_indices[:, 2]]

# keep all points that are not masked out
unmasked_points = points[~mask]
© www.soinside.com 2019 - 2024. All rights reserved.