Skip to content

Masks from meshes

Rebuild a segmentation mask from a compressed segmentation (mesh, seeds and image shape).

Every voxel centre is assigned to the region of space the mesh's triangles enclose it in, and each region is named by the seed it holds (see dw3d.exact_voxelisation). The decision is exact and uses only the mesh's own triangles, so the rebuilt mask does not depend on the NumPy or SciPy version, nor on the machine. It approximates the original mask as closely as the mesh approximates the mask's interfaces; it is not a lossless copy.

Sacha Ichbiah, 2021. Matthieu Perez, 2024. Herve Turlier's group, 2026.

reconstruct_mask_from_dict

reconstruct_mask_from_dict(dict_mask: dict[str]) -> NDArray[np.int64]

Reconstruct a segmentation image from a saved dict of points, triangles, seeds coords and desired image shape.

Each voxel centre takes the label of the region of space the mesh's triangles enclose it in: two neighbouring centres are in the same region when the segment between them crosses no triangle, and a region takes the index of the seed it holds. A centre lying exactly on a triangle belongs to the region it enters when moved infinitesimally towards increasing index along axis 0, then 1, then 2 (towards decreasing index on the last voxel of an axis, so that a cell cut by the image box keeps its outermost voxels). A region holding no seed (a few centres cut off by a sliver thinner than a voxel) takes the label of the region it shares the most neighbouring centres with. Every test is exact, so the result does not depend on the NumPy or SciPy version.

Parameters:

Name Type Description Default
dict_mask dict[str]

the dictionary MeshReconstructionAlgorithm.last_compressed_segmentation returns, with keys points, triangles, seeds and image_shape. The points must be in voxel units of the mask, as the reconstruction returns them (not scaled by set_pixel_size or built by construct_mesh_in_physical_units). After construct_mesh_in_physical_units, divide the points by the resampled spacing (the shortest spacing) first: the seeds and image_shape are those of the resampled cubic grid, and that is the mask this rebuilds.

required

Returns:

Type Description
NDArray[int64]

NDArray[np.int64]: the rebuilt mask, of shape image_shape, in 64-bit integer labels (not the original mask's dtype). Seed k (in the ascending label order of the original mask, label 0 included when present) fills its region with label k, so the labels are those of the original mask only when these were 0, 1, ..., n - 1.

reconstruct_mask_from_saved_file_dict

reconstruct_mask_from_saved_file_dict(filename_dict: str) -> NDArray[np.int64]

Reconstruct a segmentation image from a saved dict of points, triangles, seeds coords and desired image shape.

Parameters:

Name Type Description Default
filename_dict str

a .npy file written by dw3d.save_compressed_segmentation. It is unpickled (allow_pickle=True), so read only files you trust.

required

Returns:

Type Description
NDArray[int64]

NDArray[np.int64]: the rebuilt mask, as reconstruct_mask_from_dict returns it.