class SegColorMap(object): def __init__(self, alpha=0.8): my_seg_map = np.array([ [0, 0, 0, 0], [10, 170, 28, 0], # green [232, 167, 4, 0], # orange [43, 84, 206, 0], # blue [230, 230, 0, 0], # yellow [234., 9, 9, 0] ] # red ) self.np_cmap = (my_seg_map - np.min(my_seg_map)) / ( np.max(my_seg_map) - np.min(my_seg_map)) self.np_cmap[..., -1] = alpha # make sure the BACKGROUND class is completely transparant self.np_cmap[0, -1] = 0 self.cmap = ListedColormap(self.np_cmap) self.cmap._init() self.cmap._lut[0, -1] = 0 self.cmap._lut[:, -1] = np.linspace(0, alpha, len(my_seg_map) + 3) def convert_multi_labels(self, label_array): if label_array.dtype != np.int: label_array = label_array.astype(np.uint) return self.np_cmap[label_array]
def _init(self): # convert to lab lab_colors = convert_rgb2lab(self.init_colors) # initialize new list colors = [] # iterate over stops stops = self.get_stops() for j in range(len(self.init_colors) - 1): # interpolate between stops in lab for i in np.linspace(stops[j], stops[j + 1], self.N / (len(stops) - 1)): colors.append(lab_colors[j] * (1 - i) + i * lab_colors[j + 1]) # convert back to rgb self.colors = convert_lab2rgb(colors) # initialize a listed colormap ListedColormap._init(self)
def joincmap(cmap1, cmap2, N=256): """ Join two colormaps and return one dynamic colormap. """ if isinstance(cmap1, str): cmap1 = plt.get_cmap(cmap1) if isinstance(cmap2, str): cmap2 = plt.get_cmap(cmap2) cmap = ListedColormap( np.vstack((cmap1(np.linspace(0, 1, N // 2)), cmap2(np.linspace(0, 1, N // 2)))), '->'.join((cmap1.name, cmap2.name)), N) cmap._init() cmap.values = np.linspace(-1, 1, N) cmap.hinge = 0 return DynamicColormap(cmap)