pythontensorflowtensorflow2.0rgbcielab

Tensorflow CIELAB color space bounds


I have the following script that takes an image in RGB and converts it to Lab color space:

import tensorflow as tf
import tensorflow_io as tfio

img = tf.io.read_file(tf.keras.utils.get_file("tf", "https://upload.wikimedia.org/wikipedia/commons/e/e5/TensorFlow_Logo_with_text.png"))
img = tf.image.decode_png(img, channels=3)
img = tf.image.resize(img, [512, 512])
lab = tfio.experimental.color.rgb_to_lab(img)

lab = lab.numpy()
lab.shape  # (512, 512, 3)

lab[:, :, 0].min()  # 3660.3594
lab[:, :, 0].max()  # 9341.573

lab[:, :, 1].min()  # -49.76082
lab[:, :, 1].max()  # 4273.1514

lab[:, :, 2].min()  # -1256.8489
lab[:, :, 2].max()  # 6293.9043

Wiki CIELAB color space:

LAB space is three-dimensional, and covers the entire range of human color perception, or gamut. It is based on the opponent color model of human vision, where red/green forms an opponent pair, and blue/yellow forms an opponent pair. The lightness value, L*, also referred to as "Lstar," defines black at 0 and white at 100. The a* axis is relative to the green–red opponent colors, with negative values toward green and positive values toward red. The b* axis represents the blue–yellow opponents, with negative numbers toward blue and positive toward yellow.

The a* and b* axes are unbounded, and depending on the reference white they can easily exceed ±150 to cover the human gamut. Nevertheless, software implementations often clamp these values for practical reasons. For instance, if integer math is being used it is common to clamp a* and b* in the range of -128 to 127.

Why isn't 0 <= lab[:, :, 0].min() <= lab[:, :, 0].max() <= 100 true?


Solution

  • The function tfio.experimental.color.rgb_to_lab expects its input to be a float normalized between 0 and 1.

    You can call tf.image.convert_image_dtype to normalize you image (if your input is a integer and your target output is a float, the function will normalize it between 0 and 1 automatically).

    import tensorflow as tf
    import tensorflow_io as tfio
    
    img = tf.io.read_file(tf.keras.utils.get_file("tf", "https://upload.wikimedia.org/wikipedia/commons/e/e5/TensorFlow_Logo_with_text.png"))
    img = tf.image.decode_png(img, channels=3)
    img = tf.image.convert_image_dtype(img, dtype=tf.float32)
    img = tf.image.resize(img, [512, 512])
    lab = tfio.experimental.color.rgb_to_lab(img)
    
    lab = lab.numpy()
    

    And checking the L dimension :

    >>> lab[:,:,0].min()
    33.678085
    >>> lab[:,:,0].max()
    100.0