segmentation_mask_to_domain#

segmentation_mask_to_domain(mask_path, background_value=0, x_scale=1, y_scale=1, domain_name=None, as_points=False, simplify_shapes=False, simplify_tolerance=1, collection_name='masked_objects', mask_label_name='object_label', return_muspan_and_mask_ids=False)#

Extracts masked objects from an image file and creates a MuSpAn domain with those objects as shapes or points, along with their corresponding masked labels. Objects are extracted based on their labels in the image file, and the contours of the labeled regions are used to create shapes in the MuSpAn domain.

Parameters:
mask_pathstr

Path to the input image file containing the masked image. The image can be any of the following formats: TIFF / TIF, PNG, JPEG / JPG, BMP.

background_valueint, optional

The value that represents the background in the masked image. Default is 0.

x_scalefloat, optional

Scaling factor for the x-axis. Default is 1 (no scaling).

y_scalefloat, optional

Scaling factor for the y-axis. Default is 1 (no scaling).

domain_namestr, optional

Name for the MuSpAn domain to be created. If None, the filename (without extension) will be used as the domain name. Default is None.

as_pointsbool, optional

If True, the label objects will be imported as points (using the centroid of the shape). If False, they will be imported as shapes. Default is False.

simplify_shapesbool, optional

Whether to simplify the shapes using the Ramer-Douglas-Peucker algorithm. Only used if as_points is False. Default is False.

simplify_tolerancefloat, optional

Tolerance for shape simplification (only used if simplify_shapes is True). Default is 1.

collection_namestr, optional

Name for the collection in the MuSpAn domain where the objects will be added. Default is ‘masked_objects’.

mask_label_namestr, optional

Name for the mask label in the MuSpAn domain. Default is ‘object_label’.

return_muspan_and_mask_idsbool, optional

If True, the function will return the MuSpAn object IDs and their corresponding mask label IDs in addition to the domain. Default is False.

Returns:
muspan.domain

A muspan domain object.

if return_muspan_and_mask_ids is True, also returns:
muspan_object_idsnp.ndarray

An array of MuSpAn object IDs corresponding to the imported objects.

mask_idsnp.ndarray

An array of mask label IDs corresponding to the imported objects, in the same order as muspan_object_ids.

Notes

  • This function assumes that the input is an 2D label image file where each pixel’s value corresponds to a label for the object it belongs to.

  • Mask IDs are added to the MuSpAn objects as a categorical annotation label, with the name specified by mask_label_name.

  • Enables the import of objects from generic segmentation masks from segmentation tools that output labeled image files such as Cellpose, Stardist, DeepCell, QuPath, ImageJ etc.