Skip to content

ratiopath.masks.tissue_mask

tissue_mask(slide, mpp, filter=None)

Generates a tissue mask from a whole-slide image (WSI) using saturation channel extraction and morphological operations, and saves the mask as a TIFF image.

The function extracts the saturation channel from the WSI, applies Otsu thresholding to identify tissue regions, and performs morphological operations (closing and opening) to refine the mask.

Parameters:

Name Type Description Default
slide Image

whole-slide image (WSI) pyvips file handler.

required
mpp Res

Resolution of the mask in µm/px.

required
filter VipsFilter | None

A VipsFilter object that defines the operations to be applied to the image. The default filter includes saturation channel extraction, Otsu thresholding, closing, and opening operations. The disk factor for morphological operations using the default filter is 10.

None

Returns:

Type Description
Image

The generated tissue mask as pyvips.Image.

Source code in ratiopath/masks/tissue_mask.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def tissue_mask(
    slide: pyvips.Image, mpp: Res, filter: VipsFilter | None = None
) -> pyvips.Image:
    """Generates a tissue mask from a whole-slide image (WSI) using saturation channel extraction and morphological operations, and saves the mask as a TIFF image.

    The function extracts the saturation channel from the WSI, applies Otsu thresholding to
    identify tissue regions, and performs morphological operations (closing and opening)
    to refine the mask.

    Args:
        slide: whole-slide image (WSI) pyvips file handler.
        mpp: Resolution of the mask in µm/px.
        filter: A VipsFilter object that defines the operations to be applied to the image.
            The default filter includes saturation channel extraction, Otsu thresholding, closing, and opening operations.
            The disk factor for morphological operations using the default filter is 10.

    Returns:
        The generated tissue mask as pyvips.Image.
    """
    from ratiopath.masks.vips_filters import (
        VipsClosing,
        VipsCompose,
        VipsGrayScaleFilter,
        VipsOpening,
        VipsOtsu,
    )

    if filter is None:
        filter = VipsCompose(
            [
                VipsGrayScaleFilter(),
                VipsOtsu(),
                VipsClosing(),
                VipsOpening(),
            ]
        )

    return filter(slide, mpp)[0]