When extracting the tile dataset from the WSI collection, we may want to consider only a portion of the tissue, rather than the whole slide. For example, a single WSI can include multiple individual slices of tissue, or we may have pathologist annotations with the regions of interest (ROIs) we need to consider.

In this tutorial, we will see how to define different tissue masks in order to refine the tile extraction procedure.

First, we need to load some modules and an example Slide; here we will consider the Kidney WSI available in the data module.

from histolab.slide import Slide
from histolab.data import ihc_kidney
import os

BASE_PATH = os.getcwd()

PROCESS_PATH = os.path.join(BASE_PATH, 'kidney', 'processed')

ihc_kidney_svs, ihc_kidney_path = ihc_kidney_svs()
ihc_kidney_slide = Slide(ihc_kidney_path, processed_path=PROCESS_PATH)

ihc_kidney_slide.thumbnail


From our Slide object we can now retrieve a binary mask considering specific regions of the tissue. Notice that available masks are defined in the masks module.

As a diagnostic check to visualize the mask, we can call the locate mask method on the Slide, which outlines the boundaries of the selected mask on the slide’s thumbnail.

If we want to account for all the tissue detected on the slide, the TissueMask is what we need:

from histolab.masks import TissueMask


The BiggestTissueBoxMask keeps only the largest connected component of the tissue, and returns the bounding box including that region:

from histolab.masks import BiggestTissueBoxMask


It is also possible to define a custom binary mask by subclassing the BinaryMask object. For example, we can limit a rectangular region with upper-left coordinates (400, 280) and bottom-right coordinates (300, 320):

from histolab.masks import BinaryMask
from histolab.types import CP

thumb = slide.thumbnail



## Tile extraction within the mask¶

We can finally pass our mask to the extract method of our Tiler object, and visualize the location of the extracted tiles:

from histolab.tiler import RandomTiler

rtiler = RandomTiler(
tile_size=(128, 128),
n_tiles=50,
level=0,
tissue_percent=90,
seed=0,
)