I am dealing with
myWxImage, an image.myLabelImage, a numpy array that has the same shape as the image and contains one label (an integer) for every pixelmyLookupTable, a 1-dimensional numpy array, i.e. a vector, that has as many entries as there are labels. (I use it to map labels to floating point numbers.)
The goal is to iterate through the pixels and, for every pixel, consider its label, look up the corresponding floating point number and multiply it with the color of this pixel.
The following code does exactly this but is too slow. Do you have a simple suggestion how to do this faster without resorting to C++ or GPU programming which would, of course, make a lot of sense here?
weightedImage = wx.EmptyImage(myWxImage.Width, myWxImage.Height)
rgb = numpy.zeros(3, dtype=int);
for x in range(0, myWxImage.Width):
for y in range(0, myWxImage.Height):
label = myLabelImage[x, y]
weight = myLookUpTable[label]
rgb[0] = myWxImage.GetRed(x, y)
rgb[1] = myWxImage.GetGreen(x, y)
rgb[2] = myWxImage.GetBlue(x, y)
rgb = rgb * weight
weightedImage.SetRGB(x, y, rgb[0], rgb[1], rgb[2])
myBitmap = wx.BitmapFromImage(weightedImage)
# draw myBitmap
If the label image and the lookup table are constant, you could try the follwing:
It might be quicker to directly do the multiply on the image buffer itself (using GetDataBuffer) instead of round-tripping through a numpy array; you’d have to time and see.