本系列文章由 @yhl_leo 出品,转载请注明出处。

文章链接: http://blog.csdn.net/yhl_leo/article/details/52185581


PASCAL VOC图像分割数据集中,图像中各个类别以不同的颜色进行区分,即Ground Truth中,每种颜色都对应着相应的类别:

可以看出这个颜色表既好看又容易区分开。下面来看,这个color map是如何产生的。

先附上Matlab版本的代码:

% VOCLABELCOLORMAP Creates a label color map such that adjacent indices have different
% colors. Useful for reading and writing index images which contain large indices,
% by encoding them as RGB images.
%
% CMAP = VOCLABELCOLORMAP(N) creates a label color map with N entries.
function cmap = labelcolormap(N) if nargin==0
N=256
end
cmap = zeros(N,3);
for i=1:N
id = i-1; r=0;g=0;b=0;
for j=0:7
r = bitor(r, bitshift(bitget(id,1),7 - j));
g = bitor(g, bitshift(bitget(id,2),7 - j));
b = bitor(b, bitshift(bitget(id,3),7 - j));
id = bitshift(id,-3);
end
cmap(i,1)=r; cmap(i,2)=g; cmap(i,3)=b;
end
cmap = cmap / 255;

其中,N就是类别数,而bitor()bitshift()bitget()这些位运算的函数,可以在Matlab中查询其功能,很容易理解(源码就不做分析了)。

对于VOC 20类的分割问题,我们调用:labelcolormap(21),可以得到如下输出(这里把结果乘以255,使用整数表示):

     0     0     0
128 0 0
0 128 0
128 128 0
0 0 128
128 0 128
0 128 128
128 128 128
64 0 0
192 0 0
64 128 0
192 128 0
64 0 128
192 0 128
64 128 128
192 128 128
0 64 0
128 64 0
0 192 0
128 192 0
0 64 128
索引值 颜色值 颜色
0 (0,0,0)
1 (128,0,0)
2 (0,128,0)
3 (128,128,0)
4 (0,0,128)
5 (128,0,128)
6 (0,128,128)
7 (128,128,128)
8 (64,0,0)
9 (192,0,0)
10 (64,128,0)
11 (192,128,0)
12 (64,0,128)
13 (192,0,128)
14 (64,128,128)
15 (192,128,128)
16 (0,64,0)
17 (128,64,0)
18 (0,192,0)
19 (128,192,0)
20 (0,64,128)

我将Matlab代码转为python版本:

def uint82bin(n, count=8):
"""returns the binary of integer n, count refers to amount of bits"""
return ''.join([str((n >> y) & 1) for y in range(count-1, -1, -1)]) def labelcolormap(N):
cmap = np.zeros((N, 3), dtype = np.uint8)
for i in range(N):
r = 0
g = 0
b = 0
id = i
for j in range(7):
str_id = uint82bin(id)
r = r ^ ( np.uint8(str_id[-1]) << (7-j))
g = g ^ ( np.uint8(str_id[-2]) << (7-j))
b = b ^ ( np.uint8(str_id[-3]) << (7-j))
id = id >> 3
cmap[i, 0] = r
cmap[i, 1] = g
cmap[i, 2] = b
return cmap

这里,我并没有将cmap保存成float型,而是无符号整型,不过这是小事。执行labelcolormap(21)后,得到输出如下:

[[  0   0   0]
[128 0 0]
[ 0 128 0]
[128 128 0]
[ 0 0 128]
[128 0 128]
[ 0 128 128]
[128 128 128]
[ 64 0 0]
[192 0 0]
[ 64 128 0]
[192 128 0]
[ 64 0 128]
[192 0 128]
[ 64 128 128]
[192 128 128]
[ 0 64 0]
[128 64 0]
[ 0 192 0]
[128 192 0]
[ 0 64 128]]

与Matlab版本的结果一致。

最新文章

  1. jstl-vaStatus 属性count与index
  2. CentOS Linux系统下安装Redis过程和配置参数说明
  3. RabbitMQ 通过记日志来看routingkey
  4. DP入门---饭卡
  5. jQuery 插件 获取URL参数
  6. jsPlumb 学习笔记
  7. winrar激活
  8. HDU 4035Maze(树状+概率dp,绝对经典)
  9. jquery.dragsort实现列表拖曳、排序
  10. 【转】Xcode7.1环境下上架iOS App到AppStore 流程 (Part 一)
  11. NSURLConnection请求时间
  12. OpenRisc-43-or1200的IF模块分析
  13. 第12届北师大校赛热身赛第二场 B起床的烦恼
  14. Android高仿雅虎天气(两)---代码结构分析
  15. tensorflow 1.0 学习:十图详解tensorflow数据读取机制
  16. JavaScript(三)数据类型转换
  17. centos 7安装phantomjs
  18. python 装饰器的应用
  19. [development][dpdk][hugepage] 大页内存的挂载
  20. javascript:查看一个图片是否加载完成

热门文章

  1. GraphicsMagick安装&amp;make命令使用
  2. android textview添加滚动条
  3. 使用 Azure 创建网络文件系统
  4. POJ 3162 Walking Race (树的直径,单调队列)
  5. 用navcat编写定时任务调用存储过程
  6. 快学UIautomator之uiautomatorhelp使用
  7. Web性能优化系列:10个JavaScript性能提升的技巧
  8. Python基础篇 -- if while 语句
  9. insert size|single-read|Paired-end|Mate-pair
  10. lucene4.10.2实例(增删改查)