Skip to content

residual_artifacts_and_coverage

Creates a binary mask of residual artifacts.

Parameters:

Name Type Description Default
img RGBImage

Image of a tissue.

required
conversion ColorConversion

Conversion that describes the used staining protocol.

required
nucleus_area int

Approximate area of a single cell nucleus in pixels.

required
res_index int

Index of the residual channel in the used staining conversion.

required
threshold float

Threshold that determines if a given pixel is an artifact. Currently recommended threshold value for H&E stained slides is 0.005. This value was declared emprirically and should provide a strict detection of artifacts. Depending on the match between the tissue and the expected staining protocol, slightly higher values could also provide reasonable results.

required

Returns:

Type Description
ResidualArtifacts

Dictionary with a binary coverage mask and a coverage number.

Note

The returned dictionary contains the following values:

Key Description
coverage_mask Binary mask of the detected residual artifacts.
coverage A number that states what portion of the image's foreground area is covered by the artifacts.

Examples:

from skimage.data import immunohistochemistry

from rationai.qc import residual_artifacts_and_coverage
from rationai.staining import ColorConversion


img = immunohistochemistry()

result = residual_artifacts_and_coverage(
    img, ColorConversion.RGB2HDR, nucleus_area=150, res_index=2, threshold=0.013
)

mask = result["coverage_mask"]  # Contains values 0 and 1
print(result["coverage"])

Source code in rationai/qc/residual_artifacts/residual_artifacts_and_coverage.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def residual_artifacts_and_coverage(
    img: RGBImage,
    conversion: ColorConversion,
    nucleus_area: int,
    res_index: int,
    threshold: float,
) -> ResidualArtifacts:
    """Creates a binary mask of residual artifacts.

    Args:
        img: Image of a tissue.
        conversion: Conversion that describes the used staining protocol.
        nucleus_area: Approximate area of a single cell nucleus in pixels.
        res_index: Index of the residual channel in the used staining conversion.
        threshold: Threshold that determines if a given pixel is an artifact.
            Currently recommended threshold value for H&E stained slides is `0.005`.
            This value was declared emprirically and should provide a strict
            detection of artifacts.
            Depending on the match between the tissue and the expected staining protocol,
            slightly higher values could also provide reasonable results.

    Returns:
        Dictionary with a binary coverage mask and a coverage number.

    Note:
        The returned dictionary contains the following values:

        | Key               | Description                                                                                   |
        |-------------------|-----------------------------------------------------------------------------------------------|
        | `coverage_mask`   | Binary mask of the detected residual artifacts.                                               |
        | `coverage`        | A number that states what portion of the image's foreground area is covered by the artifacts. |

    Examples:
    ```python
    from skimage.data import immunohistochemistry

    from rationai.qc import residual_artifacts_and_coverage
    from rationai.staining import ColorConversion


    img = immunohistochemistry()

    result = residual_artifacts_and_coverage(
        img, ColorConversion.RGB2HDR, nucleus_area=150, res_index=2, threshold=0.013
    )

    mask = result["coverage_mask"]  # Contains values 0 and 1
    print(result["coverage"])
    ```
    """
    coverage, cov_heatmap = _get_debris_coverage(
        img=img,
        conv=conversion,
        nucleus_area=nucleus_area,
        res_index=res_index,
        threshold=threshold,
    )

    result: ResidualArtifacts = {
        "coverage_mask": cov_heatmap,
        "coverage": coverage,
    }

    return result