Skip to content

Masks from meshes (2D)

Rebuild an approximate label image from a compressed segmentation (a 2-D mesh, its seeds and the image shape).

The rebuild draws every mesh edge into a pixel mask (Bresenham's line between the two ends, each rounded to the nearest pixel centre), then grows one region per seed by a watershed on the distance to the drawn lines. It is an approximation: measured on one shipped mask, 95.6 % of the pixels agree with the mask the mesh was built from. dw3d's rebuild of a 3-D mask is exact; dw2d's is not, yet.

The names mirror dw3d's: reconstruct_mask_from_dict(dictionary) and reconstruct_mask_from_saved_file_dict(filename).

reconstruct_mask_from_dict

reconstruct_mask_from_dict(dict_mask: dict, plot: bool = False) -> NDArray[np.int64]

Rebuild an approximate label image from a compressed segmentation dictionary.

Parameters:

Name Type Description Default
dict_mask dict

the dictionary MeshReconstructionAlgorithm.last_compressed_segmentation returns (keys points, segments, labels, seeds, image_shape), or one saved by dw2d 0.1.5's export_segmentation (keys Verts, Edges, seeds, image_shape). The points must be in pixels of the image (read the dictionary before any scaling). Deprecated, through 0.8.x: a file name, read with numpy.load; use reconstruct_mask_from_saved_file_dict.

required
plot bool

draw the result with matplotlib. Defaults to False.

False

Returns:

Type Description
NDArray[int64]

NDArray[np.int64]: the label image; the seed at row k of seeds fills label k (seeds are stored one per label, in ascending label order, label 0 first).

reconstruct_mask_from_saved_file_dict

reconstruct_mask_from_saved_file_dict(filename: str | Path) -> NDArray[np.int64]

Rebuild an approximate label image from a compressed segmentation saved with dw2d.save_compressed_segmentation.

Parameters:

Name Type Description Default
filename str | Path

the .npy file (a pickled dictionary; see reconstruct_mask_from_dict).

required

Returns:

Type Description
NDArray[int64]

NDArray[np.int64]: the label image, as reconstruct_mask_from_dict returns it.