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

迁移调色板

下述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')

小结

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

Python为8bit深度图像应用color map的更多相关文章

  1. Matlab下imwrite,Uint16的深度图像

    Matlab下imwrite,Uint16的深度图像 1. 在Matlab命令窗口输入命令: help imwrite 会有如下解释: If the input array is of class u ...

  2. 深度图像配准(Registration)原理

    机器视觉中,3D相机产生的深度图像(depth image)通常需要配准(registration),以生成配准深度图像(registed depth image).实际上配准的目的就是想让深度图和彩 ...

  3. Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤

    Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤 一.在ubuntu下尝试 1. 在虚拟机VWware Workstation 12. ...

  4. RGB-D(深度图像) & 图像深度

    RGB-D(深度图像)   深度图像 = 普通的RGB三通道彩色图像 + Depth Map   在3D计算机图形中,Depth Map(深度图)是包含与视点的场景对象的表面的距离有关的信息的图像或图 ...

  5. Color Map的生成方法

    /* Return a RGB colour value given a scalar v in the range [vmin,vmax] In this case each colour comp ...

  6. Python 2.7 学习笔记 字典(map)的使用

    python中的字典,就是通常说的map,即 key/value集合的数据结构. 本文来介绍下在python下如何使用字典. 对于map这种数据结构能干什么,我们就不说了,这是一个常见的数据结构,我们 ...

  7. python 字节转换成图像

    python 字节转换成图像 使用base64 1.图片转成字节使用:  base64.b64encode() 2.字节转成图片: base64.b64decode() 图片字节串: iVBORw0K ...

  8. 关于python最大递归深度 - 998

    今天LeetCode的时候暴力求解233 问题: 给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,1 ...

  9. PCL深度图像(1)

    目前深度图像的获取方法有激光雷达深度成像法,计算机立体视觉成像,坐标测量机法,莫尔条纹法,结构光法等等,针对深度图像的研究重点主要集中在以下几个方面,深度图像的分割技术 ,深度图像的边缘检测技术 ,基 ...

随机推荐

  1. SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  2. 初识Spring框架实现IOC和DI(依赖注入)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是 ...

  3. Placeholder如何换行

    使用js动态添加标签充,处理换行问题 var placeholder = 'This is a line \nthis should be a new line'; $('textarea').att ...

  4. win10家庭版查看已连接wifi密码

    点击屏幕右下角无线网路图标. 点击网络设置. 完成.

  5. SharePoint 2013 新手注意事项总结[不断更新ing]

    前言 最近自己的QQ群里,经常有新加入的人,带着一些很入门的问题进行提问,这里,自己也总结总结,入门会经常碰到那些问题,希望能够带给入门的人以帮助. 1. SharePoint搭建环境 大家可以参考下 ...

  6. (十二)Maven生命周期和插件

    除了坐标.依赖以及仓库之外,Maven的另外两个核心概念是生命周期和插件.在有关Maven的日常使用中,命令行的输入往往就对应了生命周期,如mvn package就表示执行默认生命周期阶段packag ...

  7. 学习 git基础命令

    缘起 年后到了新公司,由于个人意愿到了一个海外的项目组,除了自己从Java技术栈转了C#技术栈外,很多技术都是第一次使用,学习压力不小啊. 自己也就先从常用的技术开始学起,比如C#,AngularJS ...

  8. Eclipse中JAR System library 没有怎么添加?

    1.打开  >>  Eclipse 2.右击项目   >>  Build path  >>  Configure Build path  如图1: 图1 3.进入 ...

  9. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  10. [原创]下拉框控件react-native-modal-dropdown更新历程

    前言 不知不觉从今年9月发布第一版控件到现在已经快3个月了 过去不断从开源社区索取,一直一直想着有机会一定要回报