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)
目前深度图像的获取方法有激光雷达深度成像法,计算机立体视觉成像,坐标测量机法,莫尔条纹法,结构光法等等,针对深度图像的研究重点主要集中在以下几个方面,深度图像的分割技术 ,深度图像的边缘检测技术 ,基 ...
随机推荐
- SYN Flood测试
由于工作需要对公司进行SYN Flood测试,在网上查了些资料,Youtube上找到最多的方法就是hping3工具来实现, 该工具已经预装在Kali下,具体操作用一条命令即可实现. hping3 -S ...
- 使用ViewPager切换Fragment时,防止频繁调用OnCreatView
使用ViewPager切换Fragment,我原先使用系统自带的适配器FragmentPagerAdapter. 切换fragment时,频繁调用oncreatview(). 查看FragmentPa ...
- DBCC CHECKDB 遭遇Operating system error 112(failed to retrieve text for this error. Reason: 15105) encountered
我们一个SQL Server服务器在执行YourSQLDBa的作业YourSQLDba_FullBackups_And_Maintenance时遇到了错误: Exec YourSQLDba.Maint ...
- 编译protobuf的jar文件
1.准备工作 需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases protobuf有很多不同语言的版本,因为我们需要的是ja ...
- FTP概述
FTP是什么? 早期三大网络应用之一:ftp(40年).http.mail ftp file transfer protocol 文件传输协议 FTP服务概述 C/S模型 客户端-服务器 FTP采用双 ...
- Android 强制设置横屏或竖屏 设置全屏
(转自:http://blog.csdn.net/yuejingjiahong/article/details/6636981) 强制横屏: @Override protected void onRe ...
- 安卓android sharepreference数据存储,保存输入框里面的数据
Fragment 里面 使用轻量级的数据存储sharepreference ,代码思路清晰.保存输入框里面的数据,实现按钮保存. 个人项目中简单清晰代码: 赵存档 编写 ,可以参考: 类继承Fragm ...
- 今天搞了一天的CentOS,唉,实在感觉自己渺小啊
从别人处转载一篇文章:http://my.oschina.net/idiotsky/blog/303545 这个文章讲的很好,因为告诉了我怎么去查看ftp有关的SElinux的bool变量值以及设定. ...
- 使用selenium编写脚本常见问题(一)
前提:我用selenium IDE录制脚本,我用java写的脚本,如果大家想看的清楚明白推荐java/Junit4/Webdriver 我用的是java/TestNG/remote control 1 ...
- django 一些相关问题
这两天在处理django项目时碰到一些问题 1.ur路径设置要忽略大小写,查找了很多资料,都没有发现相关的介绍,最后在谷歌上找到一个解决方案,https://groups.google.com/for ...