| 1 |
import warnings |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
|
| 5 |
from . import color as color_module |
| 6 |
from . import draw as draw_module |
| 7 |
from . import label as label_module |
| 8 |
from . import utils |
| 9 |
|
| 10 |
|
| 11 |
def mask_to_bbox(masks): |
| 12 |
warnings.warn( |
| 13 |
"'mask_to_bbox' is deprecated. Use 'masks_to_bboxes'", |
| 14 |
DeprecationWarning, |
| 15 |
) |
| 16 |
bboxes = masks_to_bboxes(masks) |
| 17 |
bboxes[:, 2:] += 1 |
| 18 |
return bboxes |
| 19 |
|
| 20 |
|
| 21 |
def masks_to_bboxes(masks): |
| 22 |
"""Convert mask to tight bounding box. |
| 23 |
|
| 24 |
Parameters |
| 25 |
---------- |
| 26 |
masks: numpy.ndarray, (N, H, W), bool |
| 27 |
Boolean masks. |
| 28 |
|
| 29 |
Returns |
| 30 |
------- |
| 31 |
bboxes: numpy.ndarray, (N, 4), float |
| 32 |
Tight bounding boxes. [(ymin, xmin, ymax, xmax), ...] |
| 33 |
where both left-top and right-bottom are inclusive. |
| 34 |
""" |
| 35 |
bboxes = np.zeros((len(masks), 4), dtype=float) |
| 36 |
for i, mask in enumerate(masks): |
| 37 |
if mask.sum() == 0: |
| 38 |
continue |
| 39 |
where = np.argwhere(mask) |
| 40 |
(ymin, xmin), (ymax, xmax) = where.min(axis=0), where.max(axis=0) |
| 41 |
bbox = ymin, xmin, ymax, xmax |
| 42 |
bboxes[i] = bbox |
| 43 |
return bboxes |
| 44 |
|
| 45 |
|
| 46 |
def instances2rgb( |
| 47 |
image, |
| 48 |
labels, |
| 49 |
bboxes=None, |
| 50 |
masks=None, |
| 51 |
captions=None, |
| 52 |
font_size=25, |
| 53 |
line_width=5, |
| 54 |
boundary_width=1, |
| 55 |
alpha=0.7, |
| 56 |
colormap=None, |
| 57 |
font_path=None, |
| 58 |
): |
| 59 |
"""Convert instances to rgb. |
| 60 |
|
| 61 |
Parameters |
| 62 |
---------- |
| 63 |
image: numpy.ndarray, (H, W, 3), numpy.uint8 |
| 64 |
RGB image. |
| 65 |
labels: list of int, (N,) |
| 66 |
Labels. |
| 67 |
bboxes: list of numpy.ndarray, (N, 4), float |
| 68 |
Bounding boxes. |
| 69 |
masks: numpy.ndarray, (N, H, W), bool |
| 70 |
Masks. |
| 71 |
captions: list of str |
| 72 |
Captions. |
| 73 |
font_size: int |
| 74 |
Font size. |
| 75 |
line_width: int |
| 76 |
Line width. |
| 77 |
alpha: float |
| 78 |
Alpha of RGB. |
| 79 |
colormap: numpy.ndarray, (M, 3), numpy.uint8 |
| 80 |
Label id to RGB color. |
| 81 |
|
| 82 |
Returns |
| 83 |
------- |
| 84 |
dst: numpy.ndarray, (H, W, 3), numpy.uint8 |
| 85 |
Visualized image. |
| 86 |
|
| 87 |
""" |
| 88 |
assert isinstance(image, np.ndarray) |
| 89 |
assert image.dtype == np.uint8 |
| 90 |
|
| 91 |
if image.ndim == 2: |
| 92 |
image = color_module.gray2rgb(image) |
| 93 |
assert image.ndim == 3 |
| 94 |
|
| 95 |
assert all(label_i >= 0 for label_i in labels) |
| 96 |
|
| 97 |
n_instance = len(labels) |
| 98 |
|
| 99 |
if masks is None: |
| 100 |
assert bboxes is not None |
| 101 |
masks = [None] * n_instance |
| 102 |
if bboxes is None: |
| 103 |
assert masks is not None |
| 104 |
bboxes = masks_to_bboxes(masks) |
| 105 |
if captions is None: |
| 106 |
captions = [None] * n_instance |
| 107 |
|
| 108 |
assert len(masks) == len(bboxes) == len(captions) == n_instance |
| 109 |
|
| 110 |
if colormap is None: |
| 111 |
colormap = label_module.label_colormap() |
| 112 |
|
| 113 |
dst = image |
| 114 |
|
| 115 |
for instance_id in range(n_instance): |
| 116 |
mask = masks[instance_id] |
| 117 |
|
| 118 |
if mask is None or mask.sum() == 0: |
| 119 |
continue |
| 120 |
|
| 121 |
color_ins = colormap[1:][instance_id % len(colormap[1:])] |
| 122 |
|
| 123 |
maskviz = mask[:, :, None] * color_ins.astype(float) |
| 124 |
dst = dst.copy() |
| 125 |
dst[mask] = (1 - alpha) * image[mask].astype(float) + alpha * maskviz[mask] |
| 126 |
|
| 127 |
try: |
| 128 |
import skimage.segmentation |
| 129 |
|
| 130 |
boundary = skimage.segmentation.find_boundaries(mask, connectivity=2) |
| 131 |
for _ in range(boundary_width - 1): |
| 132 |
boundary = skimage.morphology.binary_dilation(boundary) |
| 133 |
dst[boundary] = (200, 200, 200) |
| 134 |
except ImportError: |
| 135 |
pass |
| 136 |
|
| 137 |
dst = utils.numpy_to_pillow(dst) |
| 138 |
for instance_id in range(n_instance): |
| 139 |
bbox = bboxes[instance_id] |
| 140 |
label = labels[instance_id] |
| 141 |
caption = captions[instance_id] |
| 142 |
|
| 143 |
color_cls = colormap[label % len(colormap)] |
| 144 |
|
| 145 |
y1, x1, y2, x2 = bbox |
| 146 |
if (y2 - y1) * (x2 - x1) == 0: |
| 147 |
continue |
| 148 |
|
| 149 |
aabb1 = np.array([y1, x1], dtype=int) |
| 150 |
aabb2 = np.array([y2, x2], dtype=int) |
| 151 |
draw_module.rectangle_( |
| 152 |
dst, |
| 153 |
aabb1, |
| 154 |
aabb2, |
| 155 |
outline=color_cls, |
| 156 |
width=line_width, |
| 157 |
) |
| 158 |
|
| 159 |
if caption is not None: |
| 160 |
for loc in ["lt+", "lt"]: |
| 161 |
y1, x1, y2, x2 = draw_module.text_in_rectangle_aabb( |
| 162 |
img_shape=(dst.height, dst.width), |
| 163 |
loc=loc, |
| 164 |
text=caption, |
| 165 |
size=font_size, |
| 166 |
aabb1=aabb1, |
| 167 |
aabb2=aabb2, |
| 168 |
font_path=font_path, |
| 169 |
) |
| 170 |
if y1 >= 0 and x1 >= 0 and y2 < dst.height and x2 < dst.width: |
| 171 |
break |
| 172 |
draw_module.text_in_rectangle_( |
| 173 |
img=dst, |
| 174 |
loc=loc, |
| 175 |
text=caption, |
| 176 |
size=font_size, |
| 177 |
background=color_cls, |
| 178 |
aabb1=aabb1, |
| 179 |
aabb2=aabb2, |
| 180 |
font_path=font_path, |
| 181 |
) |
| 182 |
return utils.pillow_to_numpy(dst) |