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')
小结
除了用作常规的图片存储外,通过给二维数组不同元素赋予颜色的方式可以使我们对数据的空间布局分布有感官的认识,类似于热力图可视化的方式。
Python为8bit深度图像应用color map的更多相关文章
- Matlab下imwrite,Uint16的深度图像
		
Matlab下imwrite,Uint16的深度图像 1. 在Matlab命令窗口输入命令: help imwrite 会有如下解释: If the input array is of class u ...
 - 深度图像配准(Registration)原理
		
机器视觉中,3D相机产生的深度图像(depth image)通常需要配准(registration),以生成配准深度图像(registed depth image).实际上配准的目的就是想让深度图和彩 ...
 - Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤
		
Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤 一.在ubuntu下尝试 1. 在虚拟机VWware Workstation 12. ...
 - RGB-D(深度图像) & 图像深度
		
RGB-D(深度图像) 深度图像 = 普通的RGB三通道彩色图像 + Depth Map 在3D计算机图形中,Depth Map(深度图)是包含与视点的场景对象的表面的距离有关的信息的图像或图 ...
 - Color Map的生成方法
		
/* Return a RGB colour value given a scalar v in the range [vmin,vmax] In this case each colour comp ...
 - Python 2.7 学习笔记 字典(map)的使用
		
python中的字典,就是通常说的map,即 key/value集合的数据结构. 本文来介绍下在python下如何使用字典. 对于map这种数据结构能干什么,我们就不说了,这是一个常见的数据结构,我们 ...
 - python 字节转换成图像
		
python 字节转换成图像 使用base64 1.图片转成字节使用: base64.b64encode() 2.字节转成图片: base64.b64decode() 图片字节串: iVBORw0K ...
 - 关于python最大递归深度 - 998
		
今天LeetCode的时候暴力求解233 问题: 给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,1 ...
 - PCL深度图像(1)
		
目前深度图像的获取方法有激光雷达深度成像法,计算机立体视觉成像,坐标测量机法,莫尔条纹法,结构光法等等,针对深度图像的研究重点主要集中在以下几个方面,深度图像的分割技术 ,深度图像的边缘检测技术 ,基 ...
 
随机推荐
- app字体被放大效果发虚
			
IOS App所有字体被放大,显示效果发虚 小小程序猿 我的博客:http://daycoding.com 分析原因: 由于新版本上线更换了LaunchImage,没有注意美工给的图片尺寸,由于图片尺 ...
 - iOS 10对隐私权限的管理(必须要改否则会crash)
			
比如访问的摄像头.麦克风等硬件,都需要提前请求应用权限.允许后才可以使用,或者现在要提前声明,虽然以往要求不严格.比如在iOS10中访问通讯录时,强制必须在Info.plist中加入NSContact ...
 - iOS 小谈开发者中的个人、组织(公司、企业)账号
			
苹果对开发者主要分为3类:个人.组织(公司.企业).教育机构.即: 1.个人(Individual) 2.组织(Organizations) 组织类又分为2个小类: (1)公司(Company) (2 ...
 - 易企秀微场景2016最新完整版V10.5,小编亲测修复众多错误
			
易企秀V10.5更新说明1.修复拨号英文错误2.修复转送场景问题3.修复设置场景密码乱码问题4.修复前台批量删除客户图片5.修复数据收集分页问题6.修复图片分类错乱问题7.修复音乐和特效冲突问题8.修 ...
 - Appfuse:添加自定义页面组件
			
我之前是做ASP.NET的,碰到被多个页面都使用的类似组件后,就想着采用ascx(用户自定义组件)来解决,那做Java我也想用这种方案. 我要做的效果如下: 实现方案:tag方式(自定义标签) 1. ...
 - Oracle 用户、角色、权限(系统权限、对象权限)的数据字典表
			
1 三者的字典表 1.1 用户 select * from dba_users; select * from all_users; select * from user_users; 1.2 角色 s ...
 - C#求斐波那契数列第30项的值(递归和非递归)
			
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
 - [Android] 怎么在应用中实现密码隐藏?
			
[Android] 怎么在应用中实现密码隐藏? 在安卓应用中,用户注册或者登录时,需要把密码隐藏,实现一定的保密效果.在安卓中,可以通过设置EditText组件的TransformationMetho ...
 - 15天玩转redis —— 第九篇 发布/订阅模式
			
本系列已经过半了,这一篇我们来看看redis好玩的发布订阅模式,其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果 ...
 - openstack七大模块概述
			
前言 OpenStack主要由七部分组成,分别是Identify, Image, Network, Compute, Block Storage, Object Storage, Dashboard, ...