I’ve got an image read into numpy with quite a few pixels in my resulting array.
I calculated a lookup table with 256 values. Now I want to do the following:
for i in image.rows:
for j in image.cols:
mapped_image[i,j] = lut[image[i,j]]
Yep, that’s basically what a lut does.
Only problem is: I want to do it efficient and calling that loop in python will have me waiting for some seconds for it to finish.
I know of numpy.vectorize()
, it’s simply a convenience function that calls the same python code.
You can just use image
to index into lut
if lut
is 1D.
Here's a starter on indexing in NumPy:
http://www.scipy.org/Tentative_NumPy_Tutorial#head-864862d3f2bb4c32f04260fac61eb4ef34788c4c
In [54]: lut = np.arange(10) * 10
In [55]: img = np.random.randint(0,9,size=(3,3))
In [56]: lut
Out[56]: array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
In [57]: img
Out[57]:
array([[2, 2, 4],
[1, 3, 0],
[4, 3, 1]])
In [58]: lut[img]
Out[58]:
array([[20, 20, 40],
[10, 30, 0],
[40, 30, 10]])
Mind also the indexing starts at 0
TheodrosZelleke's answer in correct, but I just wanted to add a little undocumented wisdom to it. Numpy provides a function, np.take
, which according to the documentation "does the same thing as fancy indexing."
Well, almost, but not quite the same:
>>> import numpy as np
>>> lut = np.arange(256)
>>> image = np.random.randint(256, size=(5000, 5000))
>>> np.all(lut[image] == np.take(lut, image))
True
>>> import timeit
>>> timeit.timeit('lut[image]',
... 'from __main__ import lut, image', number=10)
4.369504285407089
>>> timeit.timeit('np.take(lut, image)',
... 'from __main__ import np, lut, image', number=10)
1.3678052776554637
np.take
is about 3x faster! In my experience, when using 3D luts to convert images from RGB to other color spaces, adding logic to convert the 3D look-up to a 1D flattened look-up allows a x10 speed up.