Skip to content
Advertisement

Mapping RGB values in an image to a corresponding ID using a dictionary

I am working on a segmentation problem where given an image, each RGB value corresponds to a class label. The problem I have is to efficiently map RGB values from an image (numpy array) to a corresponding class label image.

Let’s provide the following simplified example:

JavaScript

(in a real example the colorIdMap will have about 20 entries and labelOld will be an array of shape: (1024,512,3))

Now I want the result to be the following mapped array. with shape: (1024,512)

JavaScript

I attempted to do this with loops and list comprehensions but both methods are quite slow (about ~10seconds per image, which is a big number for 250K images). And I am wondering if there is a faster way of doing it.


Attempted method 1:

JavaScript

Attempted method 2:

JavaScript

So, my question is if there is any faster and more efficient way of doing this?

Advertisement

Answer

Here’s one approach based on dimensionality-reduction

JavaScript

Alternatively, we could use sidx as sorter input arg for np.searchsorted to get the final output –

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement