Tensorflow CIELAB 色彩空间边界

问题描述

我有以下脚本,它以 RGB 格式获取图像并将其转换为 Lab 色彩空间:

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 空间是三维的,涵盖了人类色彩感知或色域的整个范围。它基于人类视觉的对手颜色模型,其中红色/绿色形成对手对,蓝色/黄色形成对手对。亮度值 L*,也称为“Lstar”,将黑色定义为 0,白色定义为 100。a* 轴相对于绿色-红色对立颜色,负值指向绿色,正值指向红色。 b*轴代表蓝黄色对手,负数代表蓝色,正数代表黄色。

a* 和 b* 轴是无界的,根据参考白色,它们很容易超过 ±150 以覆盖人类色域。然而,出于实际原因,软件实现通常会限制这些值。例如,如果使用整数数学,通常会将 a* 和 b* 限制在 -128 到 127 的范围内。

为什么 0 <= lab[:,0].min() <= lab[:,0].max() <= 100 不是真的?

解决方法

函数 tfio.experimental.color.rgb_to_lab 期望它的输入是一个在 0 和 1 之间标准化的浮点数。

您可以调用 tf.image.convert_image_dtype 来规范化您的图像(如果您的输入是整数而目标输出是浮点数,则该函数会自动将其规范化为 0 和 1 之间)。

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()

并检查 L 维度:

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