Skip to content

modosaic.segmentation.visualization

modosaic.segmentation.visualization

SegmentationVisualizationService

Visualization helpers for generated segmentation masks.

instance_map_from_masks staticmethod

instance_map_from_masks(masks, H, W)

Build an integer instance map from masks.

Parameters:

Name Type Description Default
masks list[ndarray]

Boolean masks with shape HxW.

required
H int

Output height.

required
W int

Output width.

required

Returns:

Type Description
ndarray

uint16 instance map where 0 is background and positive values

ndarray

identify masks.

Source code in modosaic/segmentation/visualization.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@staticmethod
def instance_map_from_masks(masks: list[np.ndarray], H: int, W: int) -> np.ndarray:
    """Build an integer instance map from masks.

    Args:
        masks: Boolean masks with shape `HxW`.
        H: Output height.
        W: Output width.

    Returns:
        `uint16` instance map where `0` is background and positive values
        identify masks.
    """
    inst = np.zeros((H, W), dtype=np.uint16)
    masks_sorted = sorted(masks, key=lambda m: int(m.sum()), reverse=True)
    idx = 1
    for mask in masks_sorted:
        m = np.asarray(mask).squeeze().astype(bool)
        if m.shape != (H, W):
            continue
        if m.sum() == 0:
            continue
        inst[m] = idx
        idx += 1
        if idx >= np.iinfo(np.uint16).max:
            break
    return inst

overlay_masks staticmethod

overlay_masks(pil, masks, alpha=0.55, seed=0)

Overlay segmentation masks on an image.

Parameters:

Name Type Description Default
pil Image

Source image.

required
masks list[ndarray]

Boolean masks with shape HxW.

required
alpha float

Mask-color blend factor.

0.55
seed int

Seed used for deterministic mask colors.

0

Returns:

Type Description
Image

RGB image with colored masks overlaid.

Source code in modosaic/segmentation/visualization.py
10
11
12
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
51
@staticmethod
def overlay_masks(
        pil: Image.Image,
        masks: list[np.ndarray],
        alpha: float = 0.55,
        seed: int = 0,
) -> Image.Image:
    """Overlay segmentation masks on an image.

    Args:
        pil: Source image.
        masks: Boolean masks with shape `HxW`.
        alpha: Mask-color blend factor.
        seed: Seed used for deterministic mask colors.

    Returns:
        RGB image with colored masks overlaid.
    """
    rng = random.Random(seed)
    img = np.array(pil.convert("RGB"), dtype=np.uint8)
    H, W = img.shape[:2]

    if not masks:
        return pil

    # Sort masks by area DESC so big regions go first (usually nicer)
    masks_sorted = sorted(masks, key=lambda m: int(m.sum()), reverse=True)

    out = img.astype(np.float32)
    for mask in masks_sorted:
        m = np.asarray(mask).squeeze().astype(bool)
        if m.shape != (H, W):
            continue
        if m.sum() == 0:
            continue
        color = np.array(
            [rng.randint(0, 255), rng.randint(0, 255), rng.randint(0, 255)],
            dtype=np.float32,
        )
        out[m] = (1 - alpha) * out[m] + alpha * color

    return Image.fromarray(out.clip(0, 255).astype(np.uint8))