zoukankan      html  css  js  c++  java
  • Python为8bit深度图像应用color map

    图片中存在着色版的概念,二维矩阵的每个元素的值指定了一种颜色,因此可以显示出彩色。

    迁移调色板

    下述python代码将VOC数据集中的某个语义分割的图片的调色板直接应用在一个二维矩阵代表的图像上

    #label_im is a numpy array of 1 x height x width
    #return an Image object,call its' save('out.png') functioin to save as image file
    def palette( label_im):
            import copy
            from PIL import Image
            palette_im = Image.open('2008_000144.png')
            palette = palette_im.palette
            '''
            Transfer the VOC color palette to an output mask for visualization.
            '''
            if label_im.ndim == 3:
                    label_im = label_im[0]
                    label = Image.fromarray(label_im, mode='P')
                    label.palette = copy.copy(palette)
                    return label
    

    应用color map

    #直接转成含RGB信息的三维矩阵
    #示例代码中应用了gist_earth的color map
    from matplotlib import cm
    im = Image.fromarray(np.uint8(cm.gist_earth(label)*255))
    

    自定义color map

    下面的代码用于生成一个color map(由VOC数据集中的代码VOCdevkit/VOCcode/VOClabelcolormap.m转换而来)

    import numpy as np
    # bitget bitshift bitor zeros is all in matlab internal function
    def bitget(num,i):
            ar=np.array([[num]], dtype=np.uint8)
            bits=np.unpackbits(ar, axis=1)[0]
            idx=bits.size - 1 - i
            return bits[idx]
    
    def bitshift(num,i): #left shift,if i <0 ,then same as left_shift(num,-i)
            return np.right_shift(num,i)
    
    def bitor(x,y):
            return np.bitwise_or(x,y)
    #N.B. np.zeros default data type is float and usally color map element is float number that less than 1 [(0~255)/255]
    def getColorMap(N):
            #default N is 256
            if N==None:
                    N=256
    
            cmap=np.zeros(N*3, dtype=np.uint8).reshape(N,3)
            for i in range(N):
                    idx=i
                    r=0;g=0;b=0
                    for j in range(8):
                            r = bitor(r, bitshift(bitget(idx,0),7 - j));
                            g = bitor(g, bitshift(bitget(idx,1),7 - j));
                            b = bitor(b, bitshift(bitget(idx,2),7 - j));
                            idx = bitshift(idx,-3);
                    cmap[i,0]=r; cmap[i,1]=g; cmap[i,2]=b;
            #cmap = cmap / 255
            return cmap
    #ar is 2-dim np.ndarray
    def toRGBarray(ar,classes):
            cmap=getColorMap(classes)
            rows=ar.shape[0]
            cols=ar.shape[1]
            r=np.zeros(ar.size*3, dtype=np.uint8).reshape(rows,cols,3)
            for i in range(rows):
                    for j in range(cols):
                            r[i,j]=cmap[ar[i,j]]
    
            return r
    if __name__ == '__main__':
            cmap=getColorMap(21)
            print cmap
    

    调用方式:

    pic_arr=voccm.toRGBarray(label,21)
    im = Image.fromarray(pic_arr,mode='RGB')
    im.save('out.png')
    

    小结

    除了用作常规的图片存储外,通过给二维数组不同元素赋予颜色的方式可以使我们对数据的空间布局分布有感官的认识,类似于热力图可视化的方式。

  • 相关阅读:
    雷林鹏分享:jQuery EasyUI 数据网格
    雷林鹏分享:jQuery EasyUI 数据网格
    雷林鹏分享:jQuery EasyUI 数据网格
    雷林鹏分享:jQuery EasyUI 数据网格
    雷林鹏分享:jQuery EasyUI 数据网格
    雷林鹏分享:jQuery EasyUI 数据网格
    雷林鹏分享:jQuery EasyUI 数据网格
    关于elisp中一些含有'p'的符号
    how elisp works
    elisp 错误提示
  • 原文地址:https://www.cnblogs.com/makefile/p/6034162.html
Copyright © 2011-2022 走看看