Is there a convenient way to apply a lookup table to a large array in numpy?

后端 未结 3 1637
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 03:56

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:

3条回答
  •  时光说笑
    2020-12-15 04:22

    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

提交回复
热议问题