4

I am looking for suggestions or best practices to follow in terms of converting a 12-bit (0-4096) grayscale image to a 3-channel 8-bit color image in Python in order to pass it into a convolutional neural network.

With 8-bit RGB, I essentially have 24-bit colour, so I see no reason to lose data in the conversion, although most other posts suggest simply dividing the pixel value by 16 in order to squeeze it into 8-bits and then replicating this over all three channels in a lossy way.

Some ideas I have come up with include creating some kind of gradient function that converts the 12-bit uint to a corresponding colour on the gradient, but my understanding of the RGB colour space is that this would be tricky to implement using Numpy or other such libraries.

Do any of the common libraries such as OpenCV / Scikit offer this kind of functionality? I cannot find anything in the docs. Other ideas include using some kind of intermediary color space such as HSL/L*AB but I don't really know enough about this.

Please note that I am ultimately trying to create an 8-bit RGB image, not a 16-bit RGB image. Simply trying to colorize the grayscale image in a way that retains the original 12-bit data across the colour range.

Hope somebody can help!

Joe
  • 41
  • 1
  • See cv2.LUT() at https://docs.opencv.org/4.1.1/d2/de8/group__core__array.html#gab55b8d062b7f5587720ede032d34156f will map a grayscale image to a color image using a color table image. – fmw42 Apr 21 '21 at 01:56
  • @fmw42 That only takes 8-bit input/256 LUT entries. | However I see Python mentioned, and it's quite simple to do lookups with Numpy: https://stackoverflow.com/questions/14448763/is-there-a-convenient-way-to-apply-a-lookup-table-to-a-large-array-in-numpy – Dan Mašek Apr 21 '21 at 08:00

1 Answers1

0

My first question would be: Why do you need to convert it to color in any specific way?

If you are training the CNN on these images, any arbitrary transformation should work and give you similar performance. You just need to convert the training images and input images in the same manner.

You could probably just split the 16 bits and put the bottom half in R, the top half in G, and leave B with zeroes.

It kinda depends on how black-box this CNN is. But you can always test this by running a few training/testing cycles with the conversion I mentioned above and then do that again with the "compressed" versions.

Smolakian
  • 414
  • 3
  • 15