sparrow.im.segment#
- sparrow.im.segment(sdata, img_layer, model=<function _cellpose>, output_labels_layer='segmentation_mask', output_shapes_layer='segmentation_mask_boundaries', labels_layer_align=None, depth=100, chunks='auto', boundary='reflect', trim=False, iou=True, iou_depth=2, iou_threshold=0.7, crd=None, to_coordinate_system='global', scale_factors=None, overwrite=False, **kwargs)#
Segment images using a provided model and add segmentation results (labels layer and shapes layer) to the SpatialData object.
- Parameters:
sdata (
SpatialData
) – The SpatialData object containing the image layer to segment.img_layer (
str
) – The image layer insdata
to be segmented.model (
Callable
[...
,NDArray
] (default:<function _cellpose at 0x7fe4ba6d37f0>
)) – The segmentation model function used to process the images. Callable should take as input numpy arrays of dimension(z,y,x,c)
and return labels of dimension(z,y,x,c)
. It can have an arbitrary number of other parameters.output_labels_layer (
str
|list
[str
] (default:'segmentation_mask'
)) – Name of the label layer in which segmentation results will be stored insdata
. Can be a list of strings, ifmodel
returns multi channel mask. If provided as a list, its length should match thec
dimension of the output ofmodel
.output_shapes_layer (
str
|list
[str
] |None
(default:'segmentation_mask_boundaries'
)) – Name of the shapes layer where boundaries obtained output_labels_layer will be stored. If set to None, shapes won’t be stored. Can be a list of strings, ifmodel
returns multi channel mask. If provided as a list, its length should match thec
dimension of the output ofmodel
.labels_layer_align (
Optional
[str
] (default:None
)) – Name of the labels layer inoutput_labels_layer
to align to ifmodel
retuns multi channel mask.depth (
tuple
[int
,int
] |int
(default:100
)) – The depth iny
and x dimension. The depth parameter is passed todask.array.map_overlap
. If trim is set toFalse
, it’s recommended to set the depth to a value greater than twice the estimated diameter of the cells/nulcei.chunks (
str
|int
|tuple
[int
,int
] |None
(default:'auto'
)) – Chunk sizes for processing. Can be a string, integer or tuple of integers. If chunks is atuple
, they contain the chunk size that will be used in y and x dimension. Chunking inz
orc
dimension is not supported.boundary (
str
(default:'reflect'
)) – Boundary parameter passed todask.array.map_overlap
.trim (
bool
(default:False
)) – If set toTrue
, overlapping regions will be processed using thesquidpy
algorithm. If set toFalse
, thesparrow
algorithm will be employed instead. For dense cell distributions, we recommend setting trim toFalse
.iou (
bool
(default:True
)) – If set toTrue
, will try to harmonize labels across chunks using a label adjacency graph with an iou threshold (seesparrow.image.segmentation.utils._link_labels
). If set toFalse
, conflicts will be resolved using an algorithm that only retains masks with the center in the chunk. Settingiou
toFalse
gives good results if there is reasonable agreement of the predicted labels accross adjacent chunks.iou_depth (
tuple
[int
,int
] |int
(default:2
)) – iou depth used for harmonizing labels across chunks. Note that iflabels_layer_align
is specified,iou_depth
will also be used for harmonizing labels between different chunks.iou_threshold (
float
(default:0.7
)) – iou threshold used for harmonizing labels across chunks. Note that iflabels_layer_align
is specified,iou_threshold
will also be used for harmonizing labels between different chunks.crd (
Optional
[tuple
[int
,int
,int
,int
]] (default:None
)) – The coordinates specifying the region of the image to be segmented. Defines the bounds(x_min, x_max, y_min, y_max)
.to_coordinate_system (
str
(default:'global'
)) – The coordinate system to which thecrd
is specified. Ignored ifcrd
is None.scale_factors (
Optional
[Sequence
[Union
[dict
[str
,int
],int
]]] (default:None
)) – Scale factors to apply for multiscale.overwrite (
bool
(default:False
)) – IfTrue
, overwrites the existing layers if they exist. Otherwise, raises an error if the layers exist.**kwargs (
Any
) – Additional keyword arguments passed to the providedmodel
.
- Return type:
SpatialData
- Returns:
: Updated
sdata
object containing the segmentation results.- Raises:
TypeError – If the provided
model
is not a callable.