I'm looking for the most efficient way to perform the following task.
I have a numpy array with integer values and I have a color map which is a dictionary mapping integers to rgb colors.
What I need is to create for each width by heigth numpy array a width by height by 3 numpy array that can be interpreted as a color image.
For example
import numpy as np
colorMap = {1: [22,244,23], 2: [220,244,23], 3: [22,244,230]}
x = np.array([[1,2,2],[2,2,3],[3,1,2] ])
#I need a very efficient function to create a color image from these two components
image = f(x, colorMap)
My current approach is as follows
import numpy as np
colorMap = {1: [22,244,23], 2: [220,244,23], 3: [22,244,230]}
x = np.array([[1,2,2],[2,2,3],[3,1,2] ])
def f(x):
return colorMap[x]
x = x.flatten()
image = np.reshape(np.array(list(map(f, x))) , (3,3,3))
But when I time this it is rather slow when compared to numpy inbuilt functions. I'm wondering if anyone knows a way to do this using numpy built in functions that would speed up the procedure.
The above is a dummy example, in reality I need to map large rasters to a visualization in real time. The problem is that the colorMap can be rather long (length between 1 and 100) so that looping over the color map is not a really good option. (If I could loop over the colorMap I would see how to do this with numpy built in functions)
