pythonopencvrgbhsl

OpenCV converts HSL to RGB


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


Solution

  • 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]