Discrete cosine transform
The conceptHow a JPEG photo fits in a fraction of the space of its raw pixels
The discrete cosine transform (DCT) rewrites a row or block of pixels as a mix of cosine waves, from completely flat to finely striped. Nothing is lost: the original pixels can be rebuilt exactly from the wave weights.
Take two neighbouring pixels with brightness 10 and 10. The DCT says: lots of the flat pattern, none of the striped one. Smooth areas of a photo turn into a few big numbers and many near-zero ones, and near-zero numbers are cheap to store.
What it’s forTurns N pixel values into N weights, one per cosine pattern.
Xₖ = Σₙ xₙ · cos(π/N · (n + ½) · k)
- xₙ
- brightness of pixel n in the row
- N
- number of pixels in the row; JPEG uses 8
- k
- which pattern: 0 is flat, higher k means finer stripes
- Xₖ
- how much of pattern k the row contains
What it givesA list of pattern weights where smooth images put almost everything in the first few.
- Pixels [10, 10], N = 2: X₀ = 10 + 10 = 20
- X₁ = 10·cos(π/4) + 10·cos(3π/4) = 7.07 − 7.07 = 0
- Pixels [10, 6]: X₀ = 16, X₁ = 4 × 0.707 ≈ 2.83