1

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.

LosConikos
  • 93
  • 6
  • 2
    Do you already have the list of labels or should a new label be created for every combination of RGB? – nicoco Oct 08 '19 at 13:30
  • Same question but without upvoted answers: [Convert RGB image to index image](https://stackoverflow.com/q/42750910/7851470) – Georgy Oct 08 '19 at 13:35
  • 1
    @Georgy - The answers to [How to convert 3D RGB label image (in semantic segmentation) to 2D gray image, and class indices start from 0?](https://stackoverflow.com/questions/53059201/how-to-convert-3d-rgb-label-imagein-semantic-segmentation-to-2d-gray-image-an) don't apply to arbitrary RGB values. – Armali Oct 09 '19 at 07:51
  • @Goergy - Thanks, your 2nd answer is exactly what I was looking for. Damn google, why didn't you provide me with this? :( – LosConikos Oct 09 '19 at 10:25

0 Answers0