|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Sequence |
| 4 | +from typing import Final |
| 5 | +from typing import Literal |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +from numpy.typing import NDArray |
| 9 | + |
| 10 | +from . import _color |
| 11 | +from . import _label |
| 12 | +from . import _utils |
| 13 | +from . import components |
| 14 | +from . import draw as draw_module |
| 15 | + |
| 16 | + |
| 17 | +def flags2rgb( |
| 18 | + image: NDArray[np.uint8], |
| 19 | + flags: NDArray[np.bool_], |
| 20 | + centers: NDArray[np.floating], |
| 21 | + flag_names: Sequence[str], |
| 22 | + diameter: float = 30, |
| 23 | + flag_colors: NDArray[np.uint8] | None = None, |
| 24 | + wedges: Literal["on", "all"] = "on", |
| 25 | + font_size: int = 25, |
| 26 | + font_path: str | None = None, |
| 27 | + loc: Literal["lt", "rt", "lb", "rb"] = "rb", |
| 28 | +) -> NDArray[np.uint8]: |
| 29 | + """Visualize per-instance boolean flags as pie glyphs with a legend. |
| 30 | +
|
| 31 | + Wedge angle encodes flag identity, never quantity. With ``wedges="on"``, |
| 32 | + color identifies the flag and angle is just packing: only active flags get |
| 33 | + wedges, packed clockwise in flag-index order (zero active flags draws no |
| 34 | + pie, one draws a solid disc). With ``wedges="all"``, both color and angle |
| 35 | + identify the flag: every flag keeps a fixed wedge at a fixed angle, drawn |
| 36 | + light gray when off, and the legend gains one synthetic ("off", gray) |
| 37 | + entry. Around 5 wedges stay legible at the default diameter. |
| 38 | +
|
| 39 | + Args: |
| 40 | + image: RGB image with shape (H, W, 3). |
| 41 | + flags: Boolean flags with shape (N, F). |
| 42 | + centers: Pie centers with shape (N, 2). [(cy, cx), ...] |
| 43 | + flag_names: Flag names with length F. |
| 44 | + diameter: Diameter of each pie in pixels. |
| 45 | + flag_colors: Color for each flag with shape (F, 3). By default, |
| 46 | + :func:`~imgviz.label_colormap` rows 1 to F are used. |
| 47 | + wedges: Which flags get wedges ('on', 'all'). |
| 48 | + font_size: Font size of the legend. |
| 49 | + font_path: Font path. |
| 50 | + loc: Location of legend ('lt', 'rt', 'lb', 'rb'). |
| 51 | +
|
| 52 | + Returns: |
| 53 | + Visualized image with shape (H, W, 3). |
| 54 | + """ |
| 55 | + OFF_COLOR: Final = (200, 200, 200) |
| 56 | + |
| 57 | + if not isinstance(image, np.ndarray): |
| 58 | + raise TypeError(f"image must be a numpy array, but got {type(image).__name__}") |
| 59 | + if image.dtype != np.uint8: |
| 60 | + raise ValueError(f"image dtype must be np.uint8, but got {image.dtype}") |
| 61 | + if image.ndim == 2: |
| 62 | + image = _color.gray2rgb(image) |
| 63 | + if image.ndim != 3: |
| 64 | + raise ValueError(f"image must be 2 or 3 dimensional, but got {image.ndim}") |
| 65 | + |
| 66 | + if not isinstance(flags, np.ndarray): |
| 67 | + raise TypeError(f"flags must be a numpy array, but got {type(flags).__name__}") |
| 68 | + if flags.dtype != bool: |
| 69 | + raise ValueError(f"flags dtype must be bool, but got {flags.dtype}") |
| 70 | + if flags.ndim != 2: |
| 71 | + raise ValueError(f"flags must be 2 dimensional (N, F), but got {flags.ndim}") |
| 72 | + n_instances, n_flags = flags.shape |
| 73 | + |
| 74 | + centers = np.asarray(centers) |
| 75 | + if centers.shape != (n_instances, 2): |
| 76 | + raise ValueError( |
| 77 | + f"centers shape must be (N, 2) matching {n_instances} instances, " |
| 78 | + f"but got {centers.shape}" |
| 79 | + ) |
| 80 | + |
| 81 | + if len(flag_names) != n_flags: |
| 82 | + raise ValueError( |
| 83 | + f"flag_names must have one name per flag, " |
| 84 | + f"but got {len(flag_names)=}, {n_flags=}" |
| 85 | + ) |
| 86 | + |
| 87 | + if flag_colors is None: |
| 88 | + flag_colors = _label.label_colormap()[1 : n_flags + 1] |
| 89 | + if flag_colors.shape != (n_flags, 3): |
| 90 | + raise ValueError( |
| 91 | + f"flag_colors shape must be ({n_flags}, 3), but got {flag_colors.shape}" |
| 92 | + ) |
| 93 | + |
| 94 | + if wedges not in ("on", "all"): |
| 95 | + raise ValueError(f"unsupported wedges: {wedges}") |
| 96 | + |
| 97 | + dst = _utils.numpy_to_pillow(image) |
| 98 | + for i in range(n_instances): |
| 99 | + fills: list[draw_module.Ink] |
| 100 | + if wedges == "on": |
| 101 | + fills = [flag_colors[j] for j in range(n_flags) if flags[i, j]] |
| 102 | + else: |
| 103 | + fills = [ |
| 104 | + flag_colors[j] if flags[i, j] else OFF_COLOR for j in range(n_flags) |
| 105 | + ] |
| 106 | + if not fills: |
| 107 | + continue |
| 108 | + cy, cx = centers[i] |
| 109 | + draw_module.pie_( |
| 110 | + image=dst, |
| 111 | + center=(cy, cx), |
| 112 | + diameter=diameter, |
| 113 | + fills=fills, |
| 114 | + outline=(255, 255, 255), |
| 115 | + width=1, |
| 116 | + ) |
| 117 | + |
| 118 | + items: list[components.LegendItem] = list(zip(flag_names, flag_colors)) |
| 119 | + if wedges == "all": |
| 120 | + items.append(("off", OFF_COLOR)) |
| 121 | + components.legend_( |
| 122 | + image=dst, items=items, font_size=font_size, font_path=font_path, loc=loc |
| 123 | + ) |
| 124 | + return _utils.pillow_to_numpy(dst) |
0 commit comments