|
a |
|
b/utils/vis_utils.py |
|
|
1 |
import numpy as np |
|
|
2 |
import colorsys |
|
|
3 |
import random |
|
|
4 |
import cv2 |
|
|
5 |
|
|
|
6 |
def get_n_hls_colors(num): |
|
|
7 |
hls_colors = [] |
|
|
8 |
i = 0 |
|
|
9 |
step = 360.0 / num |
|
|
10 |
while i < 360: |
|
|
11 |
h = i |
|
|
12 |
s = 90 + random.random() * 10 |
|
|
13 |
l = 50 + random.random() * 10 |
|
|
14 |
_hlsc = [h / 360.0, l / 100.0, s / 100.0] |
|
|
15 |
hls_colors.append(_hlsc) |
|
|
16 |
i += step |
|
|
17 |
|
|
|
18 |
return hls_colors |
|
|
19 |
|
|
|
20 |
def ncolors(num): |
|
|
21 |
rgb_colors = [] |
|
|
22 |
if num < 1: |
|
|
23 |
return np.array(rgb_colors) |
|
|
24 |
hls_colors = get_n_hls_colors(num) |
|
|
25 |
for hlsc in hls_colors: |
|
|
26 |
_r, _g, _b = colorsys.hls_to_rgb(hlsc[0], hlsc[1], hlsc[2]) |
|
|
27 |
rgb_colors.append([_r, _g, _b]) |
|
|
28 |
# r, g, b = [int(x * 255.0) for x in (_r, _g, _b)] |
|
|
29 |
# rgb_colors.append([r, g, b]) |
|
|
30 |
|
|
|
31 |
return np.array(rgb_colors) |
|
|
32 |
|
|
|
33 |
def random_colors(N, bright=True): |
|
|
34 |
""" |
|
|
35 |
Generate random colors. |
|
|
36 |
To get visually distinct colors, generate them in HSV space then |
|
|
37 |
convert to RGB. |
|
|
38 |
""" |
|
|
39 |
brightness = 1.0 if bright else 0.7 |
|
|
40 |
hsv = [(i / N, 1, brightness) for i in range(N)] |
|
|
41 |
colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv)) |
|
|
42 |
# random.shuffle(colors) |
|
|
43 |
return colors |
|
|
44 |
|
|
|
45 |
def mask2png(mask, file_name=None, suffix="png"): |
|
|
46 |
""" mask: (w, h) |
|
|
47 |
img_rgb: (w, h, rgb) |
|
|
48 |
""" |
|
|
49 |
nums = np.unique(mask)[1:] |
|
|
50 |
if len(nums) < 1: |
|
|
51 |
colors = np.array([[0,0,0]]) |
|
|
52 |
else: |
|
|
53 |
# colors = ncolors(len(nums)) |
|
|
54 |
colors = (np.array(random_colors(len(nums))) * 255).astype(int) |
|
|
55 |
colors = np.insert(colors, 0, [0,0,0], 0) |
|
|
56 |
|
|
|
57 |
# 保证mask中的值为1-N连续 |
|
|
58 |
mask_ordered = np.zeros_like(mask) |
|
|
59 |
for cnt, l in enumerate(nums, 1): |
|
|
60 |
mask_ordered[mask==l] = cnt |
|
|
61 |
|
|
|
62 |
im_rgb = colors[mask_ordered.astype(int)] |
|
|
63 |
if file_name is not None: |
|
|
64 |
cv2.imwrite(file_name+"."+suffix, im_rgb[:, :, ::-1]) |
|
|
65 |
return im_rgb |
|
|
66 |
|
|
|
67 |
def apply_mask(image, mask, color, alpha=0.5, scale=1): |
|
|
68 |
"""Apply the given mask to the image. |
|
|
69 |
""" |
|
|
70 |
for c in range(3): |
|
|
71 |
image[:, :, c] = np.where(mask == 1, |
|
|
72 |
image[:, :, c] * |
|
|
73 |
(1 - alpha) + alpha * color[c] * scale, |
|
|
74 |
image[:, :, c]) |
|
|
75 |
return image |
|
|
76 |
|
|
|
77 |
def img_mask_png(image, mask, file_name=None, alpha=0.5, suffix="png"): |
|
|
78 |
""" mask: (h, w) |
|
|
79 |
image: (h, w, rgb) |
|
|
80 |
""" |
|
|
81 |
nums = np.unique(mask)[1:] |
|
|
82 |
if len(nums) < 1: |
|
|
83 |
colors = np.array([[0,0,0]]) |
|
|
84 |
else: |
|
|
85 |
colors = ncolors(len(nums)) |
|
|
86 |
colors = np.insert(colors, 0, [0,0,0], 0) |
|
|
87 |
|
|
|
88 |
# 保证mask中的值为1-N连续 |
|
|
89 |
mask_ordered = np.zeros_like(mask) |
|
|
90 |
for cnt, l in enumerate(nums, 1): |
|
|
91 |
mask_ordered[mask==l] = cnt |
|
|
92 |
|
|
|
93 |
# mask_rgb = colors[mask_ordered.astype(int)] |
|
|
94 |
mix_im = image.copy() |
|
|
95 |
for i in np.unique(mask_ordered)[1:]: |
|
|
96 |
mix_im = apply_mask(mix_im, mask_ordered==i, colors[int(i)], alpha=alpha, scale=255) |
|
|
97 |
|
|
|
98 |
if file_name is not None: |
|
|
99 |
cv2.imwrite(file_name+"."+suffix, mix_im[:, :, ::-1]) |
|
|
100 |
return mix_im |
|
|
101 |
|
|
|
102 |
def _find_contour(mask): |
|
|
103 |
# _, contours, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 顶点 |
|
|
104 |
_, contours, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) |
|
|
105 |
|
|
|
106 |
cont = np.zeros_like(mask) |
|
|
107 |
for contour in contours: |
|
|
108 |
cont[contour[:,:,1], contour[:,:,0]] = 1 |
|
|
109 |
return cont |
|
|
110 |
|
|
|
111 |
def masks_to_contours(masks): |
|
|
112 |
# 包含多个区域 |
|
|
113 |
nums = np.unique(masks)[1:] |
|
|
114 |
cont_mask = np.zeros_like(masks) |
|
|
115 |
for i in nums: |
|
|
116 |
cont_mask += _find_contour(masks==i) |
|
|
117 |
return (cont_mask>0).astype(int) |
|
|
118 |
|
|
|
119 |
def batchToColorImg(batch, minv=None, maxv=None, scale=255.): |
|
|
120 |
""" batch: (N, H, W, C) |
|
|
121 |
""" |
|
|
122 |
if batch.ndim == 3: |
|
|
123 |
N, H, W = batch.shape |
|
|
124 |
elif batch.ndim == 4: |
|
|
125 |
N, H, W, _ = batch.shape |
|
|
126 |
colorImg = np.zeros(shape=(N, H, W, 3)) |
|
|
127 |
for i in range(N): |
|
|
128 |
if minv is None: |
|
|
129 |
a = (batch[i] - batch[i].min()) / (batch[i].max() - batch[i].min()) * 255 |
|
|
130 |
else: |
|
|
131 |
a = (batch[i] - minv) / (maxv - minv) * scale |
|
|
132 |
a = cv2.applyColorMap(a.astype(np.uint8), cv2.COLORMAP_JET) |
|
|
133 |
colorImg[i, ...] = a[..., ::-1] / 255. |
|
|
134 |
return colorImg |
|
|
135 |
|
|
|
136 |
if __name__ == "__main__": |
|
|
137 |
a = np.zeros((100, 100)) |
|
|
138 |
a[0:5, 3:8] = 1 |
|
|
139 |
a[75:85, 85:95] = 2 |
|
|
140 |
|
|
|
141 |
# colors = ncolors(2)[::-1] |
|
|
142 |
colors = np.array(random_colors(2)) * 255 |
|
|
143 |
colors = np.insert(colors, 0, [0, 0, 0], 0) |
|
|
144 |
|
|
|
145 |
b = colors[a.astype(int)].astype(np.uint8) |
|
|
146 |
import cv2, skimage |
|
|
147 |
# skimage.io.imsave("test_io.png", b) |
|
|
148 |
# # cv2.imwrite("test.jpg", b[:, :, ::-1]) |
|
|
149 |
# print() |
|
|
150 |
|
|
|
151 |
# mask2png(a, "test") |
|
|
152 |
|
|
|
153 |
################################################ |
|
|
154 |
# img_mask_png(b, a, "test") |
|
|
155 |
|
|
|
156 |
############################################# |
|
|
157 |
# cont_mask = find_contours(a) |
|
|
158 |
# print() |
|
|
159 |
# # skimage.io.imsave("test_cont.png", cont_mask) |
|
|
160 |
# b[cont_mask>0, :] = [255, 255, 255] |
|
|
161 |
# skimage.io.imsave("test_cont.png", b) |
|
|
162 |
|
|
|
163 |
gt0 = skimage.io.imread("gt0.png", as_gray=False) |
|
|
164 |
print() |
|
|
165 |
gt0[gt0==54] = 0 |
|
|
166 |
# cont_mask = find_contours(gt0==237) # Array([ 18, 54, 73, 237], dtype=uint8) |
|
|
167 |
# cont_mask += find_contours(gt0==18) |
|
|
168 |
# cont_mask += find_contours(gt0==73) |
|
|
169 |
cont_mask = masks_to_contours(gt0) |
|
|
170 |
|
|
|
171 |
colors = np.array(random_colors(1)) * 255 |
|
|
172 |
colors = np.insert(colors, 0, [0, 0, 0], 0) |
|
|
173 |
cont_mask = colors[cont_mask.astype(int)].astype(np.uint8) |
|
|
174 |
|
|
|
175 |
skimage.io.imsave("test_cont.png", cont_mask) |