-
-
Save ccj5351/ae554ea70cef79ab1efdb3f9f92d2b37 to your computer and use it in GitHub Desktop.
Python implementation of the color map function for the PASCAL VOC data set.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" added by CCJ """ | |
def color_map_info(palette): | |
labels = [ | |
'background', #0 | |
'aeroplane', #1 | |
'bicycle', #2 | |
'bird', #3 | |
'boat', #4 | |
'bottle', #5 | |
'bus', #6 | |
'car', #7 | |
'cat', #8 | |
'chair', #9 | |
'cow', #10 | |
'diningtable', #11 | |
'dog', #12 | |
'horse', #13 | |
'motorbike', #14 | |
'person', #15 | |
'pottedplant', #16 | |
'sheep', #17 | |
'sofa', #18 | |
'train', #19 | |
'tv/monitor', #20 | |
"void/unlabelled", #255 | |
] | |
print 'class colormap and palette = {r,g,b}' | |
for i in range(0,21*3,3): | |
print '# {:>3d}: {:<20} (R,G,B) = {},{},{}'.format(i/3, labels[i/3], palette[i], palette[i+1],palette[i+2]) | |
i = 255*3 | |
print '# {:>3d}: {:<20} (R,G,B) = {},{},{}'.format(i/3, labels[21], palette[i], palette[i+1],palette[i+2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# added by CCJ: | |
""" arrange these 21 classes to 2D matrix with 3 rows and 7 columns""" | |
def color_map_viz(fname = None): | |
labels = ['B-ground', 'Aero plane', 'Bicycle', 'Bird', 'Boat', 'Bottle', | |
'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Dining-Table', 'Dog', 'Horse', | |
'Motorbike', 'Person', 'Potted-Plant', 'Sheep', 'Sofa', 'Train', | |
'TV/Monitor', 'Void/Unlabelled'] | |
nclasses = 21 | |
row_size = 80 | |
col_size = 250 | |
cmap = color_map() | |
""" arrange these 21 classes to 2D matrix with 3 rows and 7 columns""" | |
r = 3 | |
c = 7 | |
delta = 10 | |
array = np.empty((row_size*(r+1), col_size*c, cmap.shape[1]), dtype=cmap.dtype) | |
fig=plt.figure() | |
for r_idx in range(0,r): | |
for c_idx in range(0,c): | |
i = r_idx *c + c_idx | |
array[r_idx*row_size:(r_idx+1)*row_size, c_idx*col_size: (c_idx+1)*col_size, :] = cmap[i] | |
x = c_idx*col_size + delta | |
y = r_idx*row_size + row_size/2 | |
s = labels[i] | |
plt.text(x, y,s, fontsize=9, color='white') | |
print "write {} at pixel (r={},c={})".format(labels[i], y,x) | |
array[r*row_size:(r+1)*row_size, :] = cmap[-1] | |
x = 3*col_size + delta | |
y = r*row_size + row_size/2 | |
s = labels[-1] | |
plt.text(x, y,s, fontsize=9, color='black') | |
print "write {} at pixel (r={},c={})".format(labels[i], y,x) | |
plt.title("PASCAL VOC Label Color Map") | |
imshow(array) | |
axis = plt.subplot(1, 1, 1) | |
plt.axis('off') | |
if fname: | |
plt.savefig(fname, dpi=300,bbox_inches='tight', pad_inches=0.1) | |
else: | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%Official Matlab version can be found in the PASCAL VOC devkit | |
%http://host.robots.ox.ac.uk/pascal/VOC/voc2012/index.html#devkit | |
% 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Python implementation of the color map function for the PASCAL VOC data set. | |
Official Matlab version can be found in the PASCAL VOC devkit | |
http://host.robots.ox.ac.uk/pascal/VOC/voc2012/index.html#devkit | |
""" | |
import numpy as np | |
from skimage.io import imshow | |
import matplotlib.pyplot as plt | |
def color_map(N=256, normalized=False): | |
def bitget(byteval, idx): | |
return ((byteval & (1 << idx)) != 0) | |
dtype = 'float32' if normalized else 'uint8' | |
cmap = np.zeros((N, 3), dtype=dtype) | |
for i in range(N): | |
r = g = b = 0 | |
c = i | |
for j in range(8): | |
r = r | (bitget(c, 0) << 7-j) | |
g = g | (bitget(c, 1) << 7-j) | |
b = b | (bitget(c, 2) << 7-j) | |
c = c >> 3 | |
cmap[i] = np.array([r, g, b]) | |
cmap = cmap/255 if normalized else cmap | |
return cmap | |
def color_map_viz_1_column(): | |
labels = ['background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor', 'void'] | |
nclasses = 21 | |
row_size = 50 | |
col_size = 500 | |
cmap = color_map() | |
array = np.empty((row_size*(nclasses+1), col_size, cmap.shape[1]), dtype=cmap.dtype) | |
for i in range(nclasses): | |
array[i*row_size:i*row_size+row_size, :] = cmap[i] | |
array[nclasses*row_size:nclasses*row_size+row_size, :] = cmap[-1] | |
imshow(array) | |
plt.yticks([row_size*i+row_size/2 for i in range(nclasses+1)], labels) | |
plt.xticks([]) | |
plt.show() |
@ccj5351 how do save the colormap files? This seems to be an issue; the server returns "Results are not properly generated"; any clue?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I write a jupyter note for more details. Please check this jupyter note at https://github.com/ccj5351/my_notes_jupyter/blob/master/PASCAL_VOC_2012_Label_Colormap.ipynb