I'm having a bunch of images (RGB) that I need to express in terms of labels, i. e. each colour tuple (r, g, b) shall be mapped to a scalar label. In other words, I want to have images with respect of labels instead of colour. I'm well aware that this is a rather simply problem that can be solved easily by using 3 for loops, but it has to be done in Python where this approach is insufficiently slow. Example:
img = array(
[[[227, 57, 53],
[ 42, 141, 15]],
[[182, 158, 38],
[ 83, 239, 12]]])
img_lab = array(
[[2, 3],
[1, 0]])
where
2 = [227, 57, 53]
3 = [42, 141, 15]
1 = [182, 158, 38]
0 = [83, 239, 12]
Has anyone an idea how to exploit numpy's functions to speed up this replacement? I thought about using np.where(), however, I don't know how to tell python to perform the comparison on axis 3 for each pixel.