加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vis.py 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
import numpy as np
def make_palette(num_classes):
"""
Maps classes to colors in the style of PASCAL VOC.
Close values are mapped to far colors for segmentation visualization.
See http://host.robots.ox.ac.uk/pascal/VOC/voc2012/index.html#devkit
Takes:
num_classes: the number of classes
Gives:
palette: the colormap as a k x 3 array of RGB colors
"""
palette = np.zeros((num_classes, 3), dtype=np.uint8)
for k in xrange(0, num_classes):
label = k
i = 0
while label:
palette[k, 0] |= (((label >> 0) & 1) << (7 - i))
palette[k, 1] |= (((label >> 1) & 1) << (7 - i))
palette[k, 2] |= (((label >> 2) & 1) << (7 - i))
label >>= 3
i += 1
return palette
def color_seg(seg, palette):
"""
Replace classes with their colors.
Takes:
seg: H x W segmentation image of class IDs
Gives:
H x W x 3 image of class colors
"""
return palette[seg.flat].reshape(seg.shape + (3,))
def vis_seg(img, seg, palette, alpha=0.5):
"""
Visualize segmentation as an overlay on the image.
Takes:
img: H x W x 3 image in [0, 255]
seg: H x W segmentation image of class IDs
palette: K x 3 colormap for all classes
alpha: opacity of the segmentation in [0, 1]
Gives:
H x W x 3 image with overlaid segmentation
"""
vis = np.array(img, dtype=np.float32)
mask = seg > 0
vis[mask] *= 1. - alpha
vis[mask] += alpha * palette[seg[mask].flat]
vis = vis.astype(np.uint8)
return vis
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化