2D Discrete Cosine Transform
2D Discrete Cosine Transform
Wikipedia has an excellent article about the discrete cosine transform. The
rest of this page describes a two-dimensional DCT-II and inverse DCT and
gives implementations in C.
1
Cu = { √2
if u = 0
1 else
Cv = (similar to the above)
N−1 N−1
1 2y + 1 2x + 1
Fvu = Cv Cu ∑ ∑ Syx cos (vπ ) cos (uπ )
4 y=0 x=0
2N 2N
Spatial Frequency
1 N−1 N−1 2y + 1 2x + 1
Syx = ∑ ∑ Cv Cu Fvu cos (vπ ) cos (uπ )
4 v=0 u=0 2N 2N
Below is the original image and reconstructions of it using only the most
significant n × n coefficients.
You can find slow DCT/IDCT code in listing1.c. Note that you might also want
the Targa reader/writer code.
Optimization
The implementation above uses four nested loops and has complexity O (n4 )
for a 2D DCT of size n × n. We can do better by using row-column
decomposition: build a 2D DCT by running a 1D DCT over every row and
then every column.
N−1
1 2x + 1
Fu = Cu ∑ Sx cos (uπ )
2 x=0
2N
We can do better again by replacing the naive O (n2 ) DCT algorithm with
one factored similarly to a Fast Fourier Transform which would have
O (n log n) complexity.