segment

segment#

squidpy.im.segment(img, layer=None, library_id=None, method='watershed', channel=0, chunks=None, lazy=False, layer_added=None, copy=False, **kwargs)[source]#

Segment an image.

Parameters:
img ImageContainer

High-resolution image.

layer str | None (default: None)

Image layer in img that should be processed. If None and only 1 layer is present, it will be selected.

library_id str | Sequence[str] | None (default: None)

Name of the Z-dimension(s) that this function should be applied to. For not specified Z-dimensions, the identity function is applied. If None, all Z-dimensions are segmented separately.

method Literal['log', 'dog', 'doh', 'watershed'] | SegmentationModel | Callable[..., NDArray[Any]] (default: 'watershed')

Segmentation method to use. Valid options are:

Alternatively, any callable() can be passed as long as it has the following signature: numpy.ndarray (height, width, channels) -> numpy.ndarray (height, width[, channels]).

channel int | None (default: 0)

Channel index to use for segmentation. If None, use all channels.

chunks str | int | tuple[int, int] | None (default: None)

Number of chunks for Dask. For automatic chunking, use chunks = 'auto'.

lazy bool (default: False)

Whether to lazily compute the result or not. Only used when chunks != None.

layer_added str | None (default: None)

Layer of new image layer to add into img object. If None, use 'segmented_{model}'.

thresh

Threshold for creation of masked image. The areas to segment should be contained in this mask. If None, it is determined by Otsu’s method. Only used if method = 'watershed'.

geq

Treat thresh as upper or lower bound for defining areas to segment. If geq = True, mask is defined as mask = arr >= thresh, meaning high values in arr denote areas to segment. Only used if method = 'watershed'.

copy bool (default: False)

If True, return the result, otherwise save it to the image container.

kwargs Any

Keyword arguments for the underlying model.

Returns:

ImageContainer | None – If copy = True, returns a new container with the segmented image in '{layer_added}'.

Otherwise, modifies the img with the following key: