Search code examples
image-processingluatorch

Torch: max value of each channel in an rgb image


I am trying to get the max value of each channel in an rgb image. Currently, I am iterating over each channel separately like this:

r_max = rgb[1]:max()
g_max = rgb[2]:max()
b_max = rgb[3]:max()

Is there any way to comprehend this into a single statement in Torch? Could someone please help me with possible solutions?


Solution

  • Max takes an optional 'dimension' argument. Using this on the dimensions other than channel will give you a 3x1x1 tensor, which you can flatten if needed, e.g.

     rgb_max = rgb:max(2):max(3):reshape(3)