detect_tissue#
- squidpy.experimental.im.detect_tissue(sdata, *, image_key, scale='auto', method='otsu', method_params=None, channel_format='infer', corners_are_background=True, corner_size_pct=0.01, border_margin_px=0, min_specimen_area_frac=0.01, n_samples=None, auto_max_pixels=5000000, close_holes_smaller_than_frac=0.0001, mask_smoothing_cycles=0, new_labels_key=None, inplace=True)[source]#
Detect tissue regions in an image and optionally store an integer-labeled mask.
- Parameters:
- sdata
SpatialData SpatialData object containing the image.
- image_key
str Key of the image in
sdata.imagesto detect tissue from.- scale
str(default:'auto') Scale level to use for processing. If “auto”, uses the smallest available scale. Otherwise, must be a valid scale level present in the image.
- method
Literal['otsu','felzenszwalb','weka'] (default:'otsu') Tissue detection method. Valid options are:
”otsu” - Otsu thresholding with background detection.
”felzenszwalb” - Felzenszwalb superpixel segmentation.
”weka” - Trainable segmentation with corner background priors and RGB multiscale features.
- method_params
FelzenszwalbParams|WekaParams|Mapping[str,Any] |None(default:None) Optional parameters specific to the selected method. For “felzenszwalb”, provide a mapping of
FelzenszwalbParamskeys. For “weka”, provide a mapping ofWekaParamskeys. Passing values whenmethod="otsu"is not supported.- channel_format
Literal['infer','rgb','rgba','multichannel'] (default:'infer') Expected format of image channels. Valid options are:
”infer” - Automatically infer from image shape.
”rgb” - RGB image.
”rgba” - RGBA image.
”multichannel” - Multi-channel image.
- corners_are_background
bool|Sequence[bool] (default:True) Whether corners are considered background regions. Used for orienting threshold. Pass four bools to set
(top-left, top-right, bottom-left, bottom-right)separately. If no corners are flagged True, orientation falls back to bright background.- corner_size_pct
float(default:0.01) Corner box size as a fraction of height/width.
- border_margin_px
int|Sequence[int] (default:0) Ignore a border when seeding and predicting tissue. Can be:
a single int applied to all sides, or
a sequence of four ints
(top, bottom, left, right).
Useful for masking out fiducial rings or slide edges. Applied consistently across all methods.
- min_specimen_area_frac
float(default:0.01) Minimum fraction of image area for a region to be considered a specimen.
- n_samples
int|None(default:None) Maximum number of specimen regions to keep. If None, uses Otsu thresholding on log10(area) to separate specimens from artifacts.
- auto_max_pixels
int(default:5000000) Maximum number of pixels to process automatically. Images larger than this will be downscaled before processing.
- close_holes_smaller_than_frac
float(default:0.0001) Fraction of image area below which holes in the tissue mask are filled.
- mask_smoothing_cycles
int(default:0) Number of morphological closing cycles to apply for boundary smoothing.
- new_labels_key
str|None(default:None) Key to store the resulting labels in
sdata.labels. If None, uses “{image_key}_tissue”.- inplace
bool(default:True) If True, stores labels in
sdata.labels. If False, returns the mask array.
- sdata
- Returns:
ndarray|None– If inplace = False, returns a NumPy array of shape (y, x) with integer labels where 0 represents background and 1..K represent different specimen regions. Otherwise, returns None and stores the labels insdata.labels.
Notes
The function produces an integer-labeled mask where:
Label 0 represents background.
Labels 1..K represent different specimen regions.
Processing is performed at an appropriate resolution and then upscaled to match the original image dimensions.