cv2.cvtColor() from "BGR" to "L*A*B" Without Rounding The Returned Values

67 views Asked by At

I am trying to get the CIELAB (L*A*B) color values of an uint8 image as floats values using the cv2.cvtColor method.

After I had read the OpenCv documantation regarding color conversion, that says: "The values are then converted to the destination data type", I tried to cast my image to float32. Suspiciously, the converted image (which definitely wasn't monochromatic originally) L*A*B values were only (100., 0., 0.) which corresponds to white (255, 255, 255) in BGR.

Here is a my code:

import cv2

random_bgr_image = np.random.randint(0, 256, 2 * 2 * 3).astype(np.uint8).reshape(2, 2, 3)

print(random_bgr_image)
print(cv2.cvtColor(random_bgr_image, cv2.COLOR_BGR2Lab))
print(cv2.cvtColor(random_bgr_image.astype(np.float32), cv2.COLOR_BGR2Lab))

And it printed:

random_bgr_image: [[[ 18  56 222]
                    [ 13  69 119]]

                   [[ 60 112 107]
                    [128 191 253]]]
lab_values_as_uint8: [[[128 190 185]
                       [ 88 145 168]]

                      [[117 117 156]
                       [208 143 168]]]

And somehow:

lab_values_as_float32: [[[100.   0.   0.]
                         [100.   0.   0.]]

                        [[100.   0.   0.]
                         [100.   0.   0.]]] 

Am I missing something? Is there a better way to get the float Lab values of an uint8 image?
Any help would be much appreciated.

0

There are 0 answers