matplotlib显示mask标记的img / matplotlib自定义渐变透明度的color map

from matplotlib.colors import LinearSegmentedColormap
# get colormap
nalphas = 256    # 透明度alpha的级别
color_array = plt.get_cmap('Reds')(range(nalphas))
# change alpha values
color_array[:, -1] = np.linspace(0, 1, nalphas)
# color_array[-1,:] = [1, 0, 0, 1]    # 针对二值mask修改
# create a colormap object
Reds_alpha_objects = LinearSegmentedColormap.from_list(name='Reds_alpha', colors=color_array)
# register this new colormap with matplotlib
plt.register_cmap(cmap=Reds_alpha_objects)

# 显示原图像
plt.imshow(img[slice_index,:,:], cmap='gray')    
# 由于是二值mask,自定义的colormap只能保证label为0的位置是透明的,因此此处alpha设置为了0.3
plt.imshow(mask[slice_index,:,:], alpha=.3, cmap='Reds_alpha')    

你可能感兴趣的:(matplotlib显示mask标记的img / matplotlib自定义渐变透明度的color map)