嗯,这是一个非常精巧的函数
Matlab 代码是这样的:
function cmap = labelcolormap(N)
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
转成 Python 就变成了这样的:
def labelcolormap(n):
ans = []
for i in range(n):
x = i
r, g, b = 0, 0, 0
for j in range(7):
r = r ^ ((x & 1) >> 0 << (7 - j))
g = g ^ ((x & 2) >> 1 << (7 - j))
b = b ^ ((x & 4) >> 2 << (7 - j))
x = x >> 3
ans.append([r, g, b])
return ans
比如,给 10 个物体打 mask 标签,标签值分别为
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
发表回复