RGB to YCbCr Conversion Matrix in JPEG
JPEG image compression relies on color space transformation to separate brightness from chromatic information, enabling efficient compression via chroma subsampling. This article covers the standard mathematical matrix defined by the ITU-R BT.601 standard and adopted by the JPEG File Interchange Format (JFIF) specification to convert digital RGB color components into the YCbCr color space.
The Role of YCbCr in JPEG
Digital images are natively captured and displayed using the RGB (Red, Green, Blue) color model. However, the human visual system is significantly more sensitive to variations in brightness (luminance) than to subtle variations in color (chrominance).
To exploit this biological characteristic, the JPEG pipeline converts RGB values into YCbCr before performing Discrete Cosine Transform (DCT) and quantization:
- Y: Luminance (grayscale/brightness component)
- Cb: Blue-difference chrominance component
- Cr: Red-difference chrominance component
Once separated, the Cb and Cr components can be subsampled (e.g., 4:2:0 or 4:2:2) with virtually no perceptible loss in visual quality.
The Standard Forward Conversion Matrix
The JFIF standard utilizes full-range conversion based on the ITU-R BT.601 (formerly CCIR 601) standard coefficients. Assuming standard 8-bit inputs where \(R, G, B \in [0, 255]\), the mathematical transformation is represented as follows:
\[ \begin{bmatrix} Y \\ Cb \\ Cr \end{bmatrix} = \begin{bmatrix} 0.299 & 0.587 & 0.114 \\ -0.168736 & -0.331264 & 0.5 \\ 0.5 & -0.418688 & -0.081312 \end{bmatrix} \begin{bmatrix} R \\ G \\ B \end{bmatrix} + \begin{bmatrix} 0 \\ 128 \\ 128 \end{bmatrix} \]
Component Breakdown
Expressed as linear scalar equations, the conversion corresponds to:
- Luminance (\(Y\)):
\(Y = 0.299R + 0.587G + 0.114B\) - Chroma Blue (\(Cb\)):
\(Cb = -0.168736R - 0.331264G + 0.5B + 128\) - Chroma Red (\(Cr\)):
\(Cr = 0.5R - 0.418688G - 0.081312B + 128\)
In this transformation:
- The weighting factors (\(0.299\), \(0.587\), and \(0.114\)) reflect the human eye’s relative sensitivity to red, green, and blue light respectively.
- The \(+128\) bias added to \(Cb\) and \(Cr\) centers the signed chrominance values into an unsigned 8-bit range (\([0, 255]\)), where a value of \(128\) denotes neutral chrominance.
The Inverse Conversion Matrix (Decoding)
During decompression, the JPEG decoder reconstructs the RGB values by subtracting the 128 offset and multiplying the components by the inverted matrix:
\[ \begin{bmatrix} R \\ G \\ B \end{bmatrix} = \begin{bmatrix} 1 & 0 & 1.402 \\ 1 & -0.344136 & -0.714136 \\ 1 & 1.772 & 0 \end{bmatrix} \begin{bmatrix} Y \\ Cb - 128 \\ Cr - 128 \end{bmatrix} \]
Expressed as scalar equations:
- \(R = Y + 1.402(Cr - 128)\)
- \(G = Y - 0.344136(Cb - 128) - 0.714136(Cr - 128)\)
- \(B = Y + 1.772(Cb - 128)\)
Calculated values are then clamped to the valid integer range of \(0\) to \(255\) to eliminate any rounding artifacts before being displayed on an RGB device.