Sub topic 17
CIE XYZ and Lab: Perceptual Colour Differences
This chapter assumes you know what a colour space is, how sRGB gamma encoding works, and how to handle NumPy arrays. By the end you should be able to convert sRGB to CIE XYZ and CIE Lab, and use Lab to measure how different two colours look.
Why RGB distances mislead
Computer vision code often measures the difference between two colours as the Euclidean distance between their RGB triples. That distance is a poor guide to what people see.
- sRGB values are gamma encoded, so equal steps in the stored numbers are not equal steps in light.
- The eye is far more sensitive to changes in green than in blue.
- RGB describes how to drive a display. It does not describe how a colour looks.
A perceptual space aims for one property: equal distances should look like equal differences. CIE Lab comes closer to that than RGB does. It is not perfect, and the gotchas below cover where it falls short.
CIE XYZ: a device-independent reference
CIE XYZ was defined in 1931 from colour-matching experiments with human observers. It describes colour without reference to any display or camera.
- Y is luminance, the overall brightness of the light.
- X and Z carry the colour information. They are mixtures of the observer's three cone responses.
Every colour a person can see has non-negative XYZ coordinates. Display colour spaces such as sRGB cover only part of that range, called their gamut.
XYZ is a linear space, like linear-light RGB. You get from sRGB to XYZ in two steps:
- Remove the gamma encoding to get linear RGB, as in the previous chapters.
- Multiply by a 3×3 matrix built from the sRGB primaries and its D65 white point.
D65 is a standard daylight white with a colour temperature of about 6500 K. Any conversion that involves white must name its white point. Using the wrong one shifts every colour.
From XYZ to Lab
XYZ is linear, so equal distances in it still do not match perceived differences. Lab (formally CIE L\a\b\*) applies a compressive cube-root curve, then re-expresses colour as three axes:
- L\* is lightness, from 0 (black) to 100 (the reference white).
- a\* runs from green (negative) to red (positive).
- b\* runs from blue (negative) to yellow (positive).
Each XYZ value is first divided by the white point (Xn, Yn, Zn). Then, with f(t) defined below:
L = 116 f(Y/Yn) - 16a = 500 (f(X/Xn) - f(Y/Yn))b = 200 (f(Y/Yn) - f(Z/Zn))
The function f(t) is the cube root of t when t is above (6/29)^3. Below that it switches to a straight line, because the cube root has an infinite slope at zero.
import numpy as np
M_SRGB_TO_XYZ = np.array([
[0.4124564, 0.3575761, 0.1804375],
[0.2126729, 0.7151522, 0.0721750],
[0.0193339, 0.1191920, 0.9503041],
])
WHITE_D65 = np.array([0.95047, 1.0, 1.08883])
def srgb_to_linear(rgb_8bit):
c = np.asarray(rgb_8bit, dtype=np.float64) / 255.0
return np.where(c <= 0.04045, c / 12.92, ((c + 0.055) / 1.055) ** 2.4)
def srgb_to_xyz(rgb_8bit):
return srgb_to_linear(rgb_8bit) @ M_SRGB_TO_XYZ.T
def xyz_to_lab(xyz):
t = np.asarray(xyz, dtype=np.float64) / WHITE_D65
delta = 6 / 29
f = np.where(t > delta ** 3, np.cbrt(t), t / (3 * delta ** 2) + 4 / 29)
L = 116 * f[..., 1] - 16
a = 500 * (f[..., 0] - f[..., 1])
b = 200 * (f[..., 1] - f[..., 2])
return np.stack([L, a, b], axis=-1)
def srgb_to_lab(rgb_8bit):
return xyz_to_lab(srgb_to_xyz(rgb_8bit))
print(srgb_to_lab((255, 255, 255)).round(2)) # about [100. 0. 0.]
print(srgb_to_lab((128, 128, 128)).round(2)) # L about 53.6, a and b near 0
print(srgb_to_lab((255, 0, 0)).round(2)) # about [53.24 80.09 67.2]The functions accept a single triple or an array of shape (height, width, 3), because the matrix product and the last-axis indexing apply to the final axis.
Notice that the mid-grey with stored value 128 has L\* of about 53.6, close to the 50% you might expect. A stored value of 128 carries only about 21.6% of the maximum light, as the gamma chapter showed. The cube root in Lab roughly undoes that compression of light.
Measuring colour difference
The simplest perceptual difference is ΔE\*ab (the 1976 formula). It is the Euclidean distance between two Lab triples.
def delta_e_76(rgb1, rgb2):
return float(np.linalg.norm(srgb_to_lab(rgb1) - srgb_to_lab(rgb2)))
print(delta_e_76((0, 0, 0), (10, 10, 10))) # about 2.7
print(delta_e_76((245, 245, 245), (255, 255, 255))) # about 3.5Both pairs differ by 10 in every stored channel, yet the perceived differences are not the same. A ΔE of about 2 to 3 is often quoted as roughly the smallest difference a viewer notices side by side. Treat that figure as a rule of thumb, because it depends on viewing conditions.
Gotchas
- Lab is only approximately uniform. ΔE\*ab overstates differences among highly saturated colours. The CIEDE2000 formula corrects for this and is the usual choice when accuracy matters. In scikit-image it is
skimage.color.deltaE_ciede2000. - The white point must match. Lab values computed with D65 and with D50 differ. ICC profiles use D50, so mixing the two without chromatic adaptation gives wrong numbers.
- Library scaling differs.
skimage.color.rgb2labtakes floats in 0 to 1 oruint8and returns L in 0 to 100. OpenCV'scvtColorwithCOLOR_BGR2LABon auint8image rescales L to 0 to 255 and offsets a and b by 128. Onfloat32input it expects values in 0 to 1 and returns true Lab values. OpenCV also expects BGR order, as the channel-order chapter explained. - Do not convert twice. If your image is not sRGB, convert it to sRGB first, or use its ICC profile. The matrix above is correct only for sRGB primaries.
- Out-of-gamut colours. Going from Lab back to sRGB can produce values below 0 or above 1. Clip them deliberately and be aware that doing so changes the colour.
- Use cases. Lab suits colour matching, clustering by colour, and measuring image-quality differences. It is a poor choice for geometric operations such as blending in a display pipeline, where linear RGB is the right space.
Exercise
Two pixel pairs each differ by 20 in every stored sRGB channel, one pair in dark tones and one in light tones. Would you expect their RGB Euclidean distances to match, and would their ΔE values match? Why does the answer matter for colour clustering?
Show answer
The RGB Euclidean distances are identical, because the stored differences are the same (20 in each of three channels). The ΔE values will generally differ, because Lab applies gamma removal and a cube-root curve, so a fixed step in stored values corresponds to different perceived changes at different brightness levels. For clustering, using Lab means pixels grouped together are those that look alike, while RGB distances can group colours that look clearly different or split colours that look the same.