1

I am using OpenCV python to convert a single HSL value to RGB value.

Since there is no HSL2RGB flag, so I used HLS2RGB flag. I assumed HSL and HLS refers to the same color space but just flipping the S and L values. Is that right?

So here is my attempt:

import cv2

hls = (0, 50, 100)  # This is color Red
rgb = cv2.cvtColor( np.uint8([[hls]] ), cv2.COLOR_HLS2RGB)[0][0]

After conversion, rgb value is [70, 30, 30]

However, when I checked this RGB value on online RGB picker, the color is dark brown.

Any idea where could go wrong in the conversion? Thanks

Katherine Chen
  • 502
  • 2
  • 5
  • 14
  • 1
    Shouldn't that be `hls = (0, 128, 255)`? An `uint8` matrix goes up to 256, not 100. – Thomas Apr 02 '21 at 13:51
  • @Thomas Ah thank you so much, so the HSL value is based on percentage, and not the absolute value? It seems that the S:100 is expanded to its max 255 (if started index 0), and the L: ceil(50% * 255) = 128. Is this the right way retrieving the values? – Katherine Chen Apr 02 '21 at 14:07
  • I tried this method on converting Green HLS (120, 33%, 100%) to RGB, and the H value is a degree out of 360, so 120/360 * 255 = 85. L is a percentage, so ceil(0.33 * 255) = 85, and S is also a percentage, so it's simply 255. HLS value in uint8 is thus (85, 85, 255). However, the RGB converted from this HLS is (0,170,142), different from the expected RGB (0, 168, 0). – Katherine Chen Apr 02 '21 at 14:45

1 Answers1

5

The HLS ranges in OpenCV are

H -> 0 - 180
L -> 0 - 255
S -> 0 - 255

So if the HLS range you are using are

H -> 0 - 360
L -> 0 - 100
S -> 0 - 100

you have to convert it to OpenCV's range first

hls = (0, 50, 100)
hls_conv = (hls[0]/2, hls[1]*2.55, hls[2]*2.55)
rgb = cv2.cvtColor(np.uint8([[hls_conv]]), cv2.COLOR_HLS2RGB)[0][0]

which will result in rgb being [254, 0, 0]

Christian Vorhemus
  • 2,396
  • 1
  • 17
  • 29
  • Thank you for clarifying the HLS range of the OpenCV! This makes the solution crystal clear now. I will mark your answer as the solution. Thank you for your great explanation. – Katherine Chen Apr 02 '21 at 15:00