1

I'm looking for a way to transform different RGB values of a color gradient into single values ranging from 0 to 1 in python. My current approach is transforming the RGB values into single greyscale values and then scaling those values, but I'm aware that some data information gets lost in this process. So I was wondering if any of you could suggest a way to directly translate RGB values into single values and then scaling those.

I'm doing this because I'm currently training CNNs on colored images generated with matplotlib. pyplot using matshow and a color bar. The images I'm using look somewhat like this:

gramian angular field of sinusoidal data

They are generated by transforming a 1-dimensional array of values ranging from 0 to 1 into a Gramian angular field.

Looking forward to any suggestions! Cheers!

Naumi
  • 51
  • 3
  • 1
    Welcome to SO. Cool question. If I understand correctly, the input to the problem should be three values: the RGB code of '0', the RGB code of '1', and the RGB code of the item you're trying to map. Right? – Roy2012 Jun 22 '20 at 16:59
  • Please, could you provide and example of input and desired output? – Zephyr Jun 22 '20 at 17:45
  • 1
    @Roy2012 Thanks for responding and sorry for my bad explanation in the question! The input is a 3-dimensional numpy array containing rgb values, more specifically the shape is (256 , 256, 3), so for each pixel of a 256x256 picture, i have 1 value for each R, G and B, all between 0 and 255. These RGB values are all part of a gradient generated by the pyplot matshow function. The desired output is a 2-dimensional numpy array where each RGB value is mapped to a single value between 0 and 1. Hope it's more clear now! – Naumi Jun 22 '20 at 19:03
  • Yes, it is. Here's the catch: the same value would mean different things, depending on the type of gradient you're using. For example, see this page https://plotly.com/python/builtin-colorscales/ (it's for plotly, but the idea applies to other packages). In other words, a certain shape of green would mean one thing in viridis and another in BuGn. – Roy2012 Jun 22 '20 at 19:11
  • See all the good advice here: https://stackoverflow.com/questions/687261/converting-rgb-to-grayscale-intensity – Rusty Widebottom Jun 22 '20 at 19:33
  • 1
    @AndreaBlengino Originally it's a 256x256 image, but applied to a smaller image (2x2), an example would be [[[68 1 84] [28 103 141]][[53, 183, 120][253 231 36]]] (generated with the viridis colormap and the values [[1 2][3 4]]. The desired output would be something like [[0 0.33][0.66 1]]. I hope that is understandable! – Naumi Jun 22 '20 at 19:41
  • @Roy2012 Yea I assumed there was no "easy way" to do so, because of the different gradients. If I limit the process of generating and processing the images to one specific gradient used (like viridis), there might be a way, I guess. As far as I know, there is no way to do that directly using pyplot, though. – Naumi Jun 22 '20 at 19:46

0 Answers0