What is the fastest way to fill a ndarray by another ndarray using a lookup table

Viewed 16

I have 3 ndarrays:

  • lookup_table[4,]
  • color_image[height,width,3]
  • gray_image[height,width]

"lookup_table" contains values for "red, green, blue" and "gray" value for each RGB is there a high performance way to fill "gray_image" with corresponding "gray" value of "color_image" other than using nested for loops?

In other words: I have these two arrays (lookup_table and rgb_image):

lookup_table = 
[ [ 19  92 192  25]
 [ 16  99 186  30]
 [ 14 106 179  35]
 [ 15 113 171  40]
 [ 19 121 164  45]
 [ 23 127 155  50]
 [ 31 134 146  55] ... ]   # [Red  Green  Blue  Gray_Equivalent]

rgb_image = 
[ [ 0  0  19  92 192]
  [ 0  1  19  92 192]
  [ 0  2  19  92 192]
  [ 0  3  23 127 155] ... ]    # [ X  Y  Red  Green  Blue]

I want to join them on [Red Green Blue] values and make a new array like this:

gray_image = 
[ [ 0  0  25]
  [ 0  1  25]
  [ 0  2  25]
  [ 0  3  50] ... ]     # [ X  Y  Gray_Equivalent]

Something like the output of this SQL query BUT in python:

SELECT  
    rgb_image.X, 
    rgb_image.Y, 
    lookup_table.Gray_Equivalent  
FROM rgb_image LEFT JOIN lookup_table ON
     rgb_image.Red = lookup_table.Red 
     rgb_image.Green= lookup_table.Green
     rgb_image.Blue= lookup_table.Blue
0 Answers
Related