Skip to content

draw_thing_contours

Overlay coloured contours on a background image from an instance labelled raster mask.

Note

If a semantic type_map is provided, the contours will be coloured according to the type.

Parameters:

Name Type Description Default
image ndarray

Original image. Shape (H, W, 3).

required
inst_map ndarray

Instance segmentation map. Shape (H, W).

required
type_map ndarray

Semantic segmentation map. Shape (H, W).

required
thickness int

Thickness of the contour lines

2

Returns:

Type Description
ndarray

np.ndarray: The contours overlaid on top of original image. Shape: (H, W, 3).

Examples:

>>> import matplotlib.pyplot as plt
>>> from histolytics.utils.plot import draw_thing_contours
>>> from histolytics.data import (
...     hgsc_cancer_he,
...     hgsc_cancer_inst_mask,
...     hgsc_cancer_type_mask,
... )
>>> # Load the HE image, instance mask and type mask
>>> he_image = hgsc_cancer_he()
>>> inst_mask = hgsc_cancer_inst_mask()
>>> type_mask = hgsc_cancer_type_mask()
>>> # Draw contours of the instance segmentation mask
>>> overlay = draw_thing_contours(
...     he_image,
...     inst_mask,
...     type_mask,
...     thickness=2,
... )
>>> # Display the overlay
>>> fig, ax = plt.subplots(figsize=(5, 5))
>>> ax.imshow(overlay)
>>> ax.set_axis_off()

out

Source code in src/histolytics/utils/plot.py
def draw_thing_contours(
    image: np.ndarray,
    inst_map: np.ndarray,
    type_map: np.ndarray,
    thickness: int = 2,
) -> np.ndarray:
    """Overlay coloured contours on a background image from an instance labelled raster mask.

    Note:
        If a semantic `type_map` is provided, the contours will be coloured according to the type.

    Parameters:
        image (np.ndarray):
            Original image. Shape (H, W, 3).
        inst_map (np.ndarray):
            Instance segmentation map. Shape (H, W).
        type_map (np.ndarray):
            Semantic segmentation map. Shape (H, W).
        thickness (int):
            Thickness of the contour lines

    Returns:
        np.ndarray:
            The contours overlaid on top of original image. Shape: (H, W, 3).

    Examples:
        >>> import matplotlib.pyplot as plt
        >>> from histolytics.utils.plot import draw_thing_contours
        >>> from histolytics.data import (
        ...     hgsc_cancer_he,
        ...     hgsc_cancer_inst_mask,
        ...     hgsc_cancer_type_mask,
        ... )
        >>> # Load the HE image, instance mask and type mask
        >>> he_image = hgsc_cancer_he()
        >>> inst_mask = hgsc_cancer_inst_mask()
        >>> type_mask = hgsc_cancer_type_mask()
        >>> # Draw contours of the instance segmentation mask
        >>> overlay = draw_thing_contours(
        ...     he_image,
        ...     inst_mask,
        ...     type_mask,
        ...     thickness=2,
        ... )
        >>> # Display the overlay
        >>> fig, ax = plt.subplots(figsize=(5, 5))
        >>> ax.imshow(overlay)
        >>> ax.set_axis_off()
    ![out](../../img/overlay.png)
    """
    bg = np.copy(image)

    shape = inst_map.shape[:2]
    nuc_list = list(np.unique(inst_map))

    if 0 in nuc_list:
        nuc_list.remove(0)  # 0 is background

    for _, nuc_id in enumerate(nuc_list):
        inst = np.array(inst_map == nuc_id, np.uint8)

        y1, y2, x1, x2 = bounding_box(inst)
        y1 = y1 - 2 if y1 - 2 >= 0 else y1
        x1 = x1 - 2 if x1 - 2 >= 0 else x1
        x2 = x2 + 2 if x2 + 2 <= shape[1] - 1 else x2
        y2 = y2 + 2 if y2 + 2 <= shape[0] - 1 else y2

        inst_crop = inst[y1:y2, x1:x2]
        inst_bg_crop = bg[y1:y2, x1:x2]
        contours = cv2.findContours(inst_crop, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[
            0
        ]

        type_crop = type_map[y1:y2, x1:x2]
        type = np.unique(type_crop[inst_crop > 0])[0]
        inst_color = NUM_COLORS[type]

        cv2.drawContours(
            inst_bg_crop,
            contours,
            contourIdx=-1,
            color=inst_color,
            thickness=thickness,
        )

        bg[y1:y2, x1:x2] = inst_bg_crop

    return bg