PASCAL VOC 2012调色板 color map生成源代码分析
def putpalette(mask):
colormap = [[0, 0, 0], [128, 0, 0], [0, 128, 0], [128, 128, 0], [0, 0, 128],
[128, 0, 128], [0, 128, 128], [128, 128, 128], [64, 0, 0],
[192, 0, 0], [64, 128, 0], [192, 128, 0], [64, 0, 128],
[192, 0, 128], [64, 128, 128], [192, 128, 128], [0, 64, 0],
[128, 64, 0], [0, 192, 0], [128, 192, 0], [0, 64, 128]]
r = mask.copy()
g = mask.copy()
b = mask.copy()
for cls in range(21):
r[mask == cls] = colormap[cls][0]
g[mask == cls] = colormap[cls][1]
b[mask == cls] = colormap[cls][2]
# b[mask == cls] = self.colormap[color_cls][2]
rgb = np.zeros((mask.shape[0], mask.shape[1], 3))
rgb[:, :, 0] = b
rgb[:, :, 1] = g
rgb[:, :, 2] = r
return rgb.astype('uint8')
# 传入图像的 name 获取图像和标注的路径
dir_img = os.path.join(args.voc12_root, args.img_dir, name+'.jpg')
dir_gt = os.path.join(args.gt_dir, name+'.png')
# 读入灰度图像
img = mmcv.imread(dir_img)
gt = mmcv.imread(dir_gt, flag='grayscale')
# 为灰色标注上色
colored_mask = putpalette(mask)
# 将彩色标注与原图融合
vis_mask = cv2.addWeighted(img, 0.5, colored_mask, 0.5, gamma=0.1)
vis = concat_two_img(img, vis_mask)
1
2
3
4
5
6
7
8
9
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
2
3
4
5
6
7
8
9
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
np 的 阈值化和 torch 的阈值化
threshold, upper, lower = 0.5, 1, 0
a = np.random.rand(4, 4)
a_np = np.where(a > threshold, upper, lower)
a_tensor = torch.from_numpy(a)
a_tensor_torch = torch.where(a_tensor > threshold, upper, lower)
1
2
3
4
5
6
7
2
3
4
5
6
7
参考资料
- https://yimiandai.me/post/voc-pillow/
上次更新: 2023/03/25, 19:58:09
- 02
- README 美化05-20
- 03
- 常见 Tricks 代码片段05-12