看下面示例,其实就是很巧妙的运用了numpy切片操作
代码:
import numpy as np
COLORMAP = [
[128, 64, 128],
[244, 35, 232...70],
[0, 60, 100],
[0, 80, 100],
[0, 0, 230],
[119, 11, 32],
]
a = np.array([[1, 2, 3,...4], [4, 5, 6, 7], [0, 1, 2, 3]])
print(a.shape) # (3,4)
print(a)
colormap = np.array(COLORMAP, dtype...='uint8') # (19,3)
print(colormap.shape)
print(colormap)
c = colormap[a, :]
print(c.shape) # (3,4,3...)
print(c)
输出结果:
(3, 4)
[[1 2 3 4]
[4 5 6 7]
[0 1 2 3]]
(19, 3)
[[128 64 128]
[244 35 232]
[ 70