The classes implemented in the masks module define how to retrieve a binary mask of the tissue from a Slide object. This step is necessary during the tiles extraction phase. The masks module already implements different approaches to retrieve specific tissue regions within the slide: the TissueMask class segments the whole tissue area in the slide leveraging a sequence of native filters, including conversion to grayscale, Otsu thresholding, binary dilation, small holes and small objects removal; the BiggestTissueBoxMask class applies the same chain of filters as TissueMask but it returns a binary mask corresponding to the bounding box of the largest connected tissue region within the slide. Alternatively, a custom binary mask can be defined with the BinaryMask class.

class BinaryMask[source]

Generic object for binary masks.

This object can be used to create a custom binary mask object.


>>> from histolab.slide import Slide
>>> class MyCustomMask(BinaryMask):
...     def _mask(self, slide):
...         my_mask = np.array([0,1])
...         return my_mask
>>> binary_mask = MyCustomMask()
>>> slide = Slide("path/to/slide") 
>>> binary_mask(slide) 

Call self as a function.

class BiggestTissueBoxMask[source]

Object that represents the box containing the largest contiguous tissue area.

Internally, this class automatically detects the tissue regions via a predefined sequence of filters, and then retain the largest connected component.

Call self as a function.

class TissueMask[source]

Object that represent the whole tissue area mask.

The tissue within the slide or tile is automatically detected through a predefined chain of filters.


Apply a predefined chain of filters to calculate the tissue area mask.

The applied filters will be different based on the type of obj, please see



obj (Union[Slide, Tile]) – Slide or Tile from which to compute the extraction mask.


Binary mask of the tissue area. The dimensions are those of the thumbnail in case obj is a Slide, otherwise they are the same as the tile.

Return type