Sub topic 15
Separating brightness from colour with YCbCr
This chapter assumes you know the RGB model, 8-bit channels, and the idea of gamma encoding from earlier chapters. It also assumes you can read short NumPy snippets.
Why split brightness from colour
An RGB pixel mixes two kinds of information. One is how bright the pixel is. The other is which colour it has. In RGB, every channel carries some of both. If you change only the brightness, all three numbers change.
YCbCr is a colour space that separates the two. It has three channels:
- Y is luma, the brightness signal.
- Cb is blue-difference chroma. It measures how far the blue content is above or below the brightness.
- Cr is red-difference chroma. It measures how far the red content is above or below the brightness.
Human vision has much finer spatial detail for brightness than for colour. YCbCr lets an encoder keep Y at full resolution and store Cb and Cr at lower resolution. JPEG, most video codecs, and many camera pipelines rely on this.
The conversion
The JPEG (JFIF) version of YCbCr uses the Rec. 601 luma weights you met in the grayscale chapter. For 8-bit full-range values:
- Y = 0.299 R + 0.587 G + 0.114 B
- Cb = 128 − 0.168736 R − 0.331264 G + 0.5 B
- Cr = 128 + 0.5 R − 0.418688 G − 0.081312 B
Y is the grayscale image. Cb and Cr are centred on 128, so a neutral grey or white pixel has Cb = Cr = 128. Values above 128 lean towards blue (Cb) or red (Cr). Values below 128 lean away from them.
import numpy as np
# Rows of (R, G, B): red, green, blue, white
rgb = np.array([[255, 0, 0],
[0, 255, 0],
[0, 0, 255],
[255, 255, 255]], dtype=np.float64)
matrix = np.array([
[ 0.299, 0.587, 0.114 ], # Y
[-0.168736, -0.331264, 0.5 ], # Cb
[ 0.5, -0.418688, -0.081312], # Cr
])
offset = np.array([0, 128, 128])
ycbcr = rgb @ matrix.T + offset
ycbcr = np.clip(np.round(ycbcr), 0, 255).astype(np.uint8)
print(ycbcr)
# [[ 76 85 255]
# [150 44 21]
# [ 29 255 107]
# [255 128 128]]The white pixel has Y = 255 and both chroma values at 128, which means "no colour". Pure red and pure blue both hit 255 in one chroma channel, so the clip matters. The raw result for red's Cr is 255.5, which falls outside the 8-bit range.
Because the transform is linear, you can invert it with a matrix inverse. The round trip is not perfectly exact. Rounding to integers and clipping lose a little information, so a pixel can come back off by one level.
What each channel looks like
If you display Y alone, you see a normal grayscale photograph with all the detail. If you display Cb or Cr alone, you see a faint, low-contrast image. Edges are soft and most areas sit near mid-grey.
This is the property compression exploits. Most of the visible structure is in Y. The chroma channels are smooth, so they can lose resolution with little visible effect.
Luma is not luminance
The weights above are applied to gamma-encoded R, G, and B values. Strictly, the result is called luma, often written Y′ with a prime. Luminance, written Y, is a weighted sum of linear light, which you get after undoing the gamma curve.
The two differ. Saturated colours show the difference most. A strongly coloured pixel can have a luma noticeably different from its true luminance. Video and JPEG pipelines use luma because it is cheap and consistent. If you need physically correct brightness, for example for blurring or resizing in linear light, convert to linear first.
Chroma subsampling in practice
Once Y, Cb, and Cr are separate, you can store Cb and Cr on a coarser grid. The common schemes, previewed here and covered in the next chapter, are:
| Scheme | Chroma resolution relative to luma | Samples per pixel |
|---|---|---|
| 4:4:4 | Full in both directions | 3.0 |
| 4:2:2 | Half horizontally | 2.0 |
| 4:2:0 | Half horizontally and vertically | 1.5 |
The next snippet counts the samples for a 1920×1080 frame and shows the saving at 8 bits per sample.
width, height = 1920, 1080
y_samples = width * height
chroma_420 = (width // 2) * (height // 2) # per chroma channel
total_444 = 3 * y_samples
total_420 = y_samples + 2 * chroma_420
print(total_444) # 6220800
print(total_420) # 3110400
print(total_420 / total_444) # 0.5With 4:2:0, the frame needs half the bytes of 4:4:4 before any entropy coding. That saving comes only from throwing away colour detail the eye barely notices.
The pipeline
A decoder runs the same steps backwards. It decodes the three planes, upsamples Cb and Cr to match Y, and converts back to RGB for display.
Common errors
- Wrong range. JPEG and computer graphics use full range, where all channels span 0 to 255. Broadcast video usually uses limited range, where Y spans 16 to 235 and Cb and Cr span 16 to 240. Treating limited-range data as full range gives washed-out blacks and dull whites. Treating full-range data as limited gives crushed shadows and clipped highlights.
- Wrong matrix. HD video usually follows Rec. 709, which uses luma weights of 0.2126, 0.7152, and 0.0722. SD video and JPEG follow Rec. 601. Decoding with the wrong one shifts colours, most visibly in saturated greens and reds.
- Channel order. OpenCV names its mode YCrCb and stores the channels in the order Y, Cr, Cb. Many other libraries use Y, Cb, Cr. Mixing them swaps the blue and red tint.
- Dtype overflow. Do the arithmetic in float or a wide integer type, then round and clip. Doing it directly in
uint8wraps values around instead of clipping. - Editing chroma and expecting brightness to hold. Changing Cb or Cr leaves Y alone. After conversion back to RGB, however, a pixel can land outside the 0 to 255 range and be clipped. That changes its apparent brightness.
Exercise
A 1920×1080 frame uses 8-bit YCbCr with 4:2:2 subsampling. How many bytes does it need uncompressed, and what fraction is that of the 8-bit RGB frame size of 6,220,800 bytes?
Show answer
Y has 1920 × 1080 = 2,073,600 samples. Each chroma channel is half as wide, so 960 × 1080 = 1,036,800 samples, and there are two of them, giving 2,073,600. The total is 4,147,200 bytes, which is two thirds (about 66.7%) of 6,220,800.
Exercise
A pixel has Cb = 128 and Cr = 128 and Y = 90. What colour does it have, and what is its RGB value in the full-range JFIF conversion?
Show answer
Both chroma values sit at their neutral centre, so the pixel has no colour. It is a mid-dark grey, with R = G = B = 90, since the luma weights sum to 1.