torch_staintools.functional.tissue_mask package

Module contents

exception torch_staintools.functional.tissue_mask.TissueMaskException

Bases: Exception

torch_staintools.functional.tissue_mask.get_tissue_mask(image: Tensor, luminosity_threshold=0.8, mask: Tensor | None = None, throw_error: bool = True, true_when_empty: bool = False) Tensor

Get a binary mask where true denotes pixels with a luminosity less than the specified threshold.

Typically, we use to identify tissue in the image and exclude the bright white background. If luminosity_threshold is None then entire image region is considered as tissue

Parameters:
  • image – RGB [0, 1]. -> BCHW

  • luminosity_threshold – threshold of luminosity in range of [0, 1]. Pixels with intensity within (0, threshold) are considered as tissue. If None then all pixels are considered as tissue, effectively bypass this step.

  • mask – An optional custom mask to override the luminosity-based tissue masking, if present. [B x 1 x H x W]

  • throw_error – whether to throw error

  • true_when_empty – if True, then return an all-True mask if no tissue are detected. Effectively bypass the tissue detection. Note that in certain cases, both Vahadane and Macenko may either obtain low quality results or even crash if the nearly entire input image is background.

Returns:

mask (B1HW)

Raises:

TissueMaskException – Raised if the entire batch is empty. Handle it to bypass the batch. If part of the batch is empty (no tissue) it will go through and handled by solvers differently. Macenko may return an all black/white tensor depending on the concentration solvers. Vahadane may return the empty patch with color distorted. Overall it is recommended to clean the data first as background patches result in unstable numerical solution.