Search code examples
python3dgeometryvoxelopen3d

Creating VoxelGrid using Voxels (Open3D)


I'm using open3d to voxelize two point clouds, then color the voxels based on the point count difference for each voxel. I do this by creating 2 VoxelGrid objects using o3d.geometry.VoxelGrid.create_from_point_cloud(). After I'm done with the calculations I end up with an array of Voxel objects, that have a color assigned. I want to assemble a new VoxelGrid with the color values.
Here, I'm just stuck since I'm unsure how to do this.

In the o3d documentation there is no indication of any function capable of doing this. I was thinking of getting the center coordinate of each Voxel that I want in the final grid, creating an o3d.geometry.PointCloud using these center coordinates with the desired colors, and then simply using o3d.geometry.VoxelGrid.create_from_point_cloud().

Is there maybe an operator overload or anything that I'm not aware of? I don't have a huge experience using o3d.

common_voxels = []
vox = self.pc_source.voxel_grid.get_voxels()
cmap = LinearSegmentedColormap.from_list('custom_colormap', [(0, 'green'), (0.5, 'yellow'), (1, 'red')])
norm = plt.Normalize(vmin=delta_min, vmax=delta_max)
sm = ScalarMappable(cmap=cmap, norm=norm)
colors = sm.to_rgba(point_count_delta)


for i, (voxel_idx, source_idx, target_idx, source_point_count, target_point_count) in enumerate(self.common_voxel_point_count_idx_map):
    vox[source_idx].color = colors[i][[0, 1, 2]]
    common_voxels.append(vox[source_idx])
    # vox_center = self.pc_source.voxel_grid.get_center_coordinate
    print(i)

vox_grid = o3d.geometry.VoxelGrid()
for i in range(len(common_voxels)):
    vox_grid += common_voxels[i]

This is the part in my code that creates the Voxel objects that I want to use to create the VoxelGrid.


Solution

  • You can use add_voxel or remove_voxel to add or remove voxels. See docs here and here. However, the python bindings of these functionalities are recently added to Open3d and are only available via latest release or version >= 0.18.0. Install via this to access this functionality.

    PR link that added this functionality - https://github.com/isl-org/Open3D/pull/6023