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.

  • img (ImageContainer) – High-resolution image.

  • layer (Optional[str]) – Image layer in img that should be processed. If None and only 1 layer is present, it will be selected.

  • library_id (Union[str, Sequence[str], 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 (Union[str, SegmentationModel, Callable[..., ndarray[Any, dtype[Any]]]]) –

    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 (Optional[int]) – Channel index to use for segmentation. If None, use all channels.

  • chunks (Union[str, int, tuple[int, int], None]) – Number of chunks for dask. For automatic chunking, use chunks = 'auto'.

  • lazy (bool) – Whether to lazily compute the result or not. Only used when chunks != None.

  • layer_added (Optional[str]) – 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) – If True, return the result, otherwise save it to the image container.

  • kwargs (Any) – Keyword arguments for the underlying model.

Return type:



: If copy = True, returns a new container with the segmented image in '{layer_added}'.

Otherwise, modifies the img with the following key: