0% found this document useful (0 votes)
25 views4 pages

2D Discrete Cosine Transform

Uploaded by

Rrc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

2D Discrete Cosine Transform

Uploaded by

Rrc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

unix4lyfe.

org » Discrete Cosine Transform

The DCT transforms a signal from a spatial representation into a frequency


representation. In an image, most of the energy will be concentrated in the
lower frequencies, so if we transform an image into its frequency
components and throw away the higher frequency coefficients, we can
reduce the amount of data needed to describe the image without sacrificing
too much image quality.

A simplified JPEG compressor:

• Cut an image up into blocks of 8x8 pixels


• Run each block through an 8x8 2D DCT
• Quantize the resulting coefficients (i.e. throw away unimportant
information to reduce the filesize - JPEG does this by dividing the
coefficients by a quantization matrix in order to get long runs of zeros)
• Compress the quantized coefficients using a lossless method (RLE,
Huffman, Arithmetic coding, etc)

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.

Given an image, S , in the spatial domain, the pixel at coordinates (x, y) is


denoted Syx . To transform S into an image in the frequency domain, F , we
can use the following:

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

Four example blocks in spatial and frequency domains:


> > >

Spatial Frequency

Inverse Discrete Cosine Transform


To rebuild an image in the spatial domain from the frequencies obtained
above, we use the IDCT:

1 N−1 N−1 2y + 1 2x + 1
Syx = ∑ ∑ Cv Cu Fvu cos (vπ ) cos (uπ )
4 v=0 u=0 2N 2N

Mathematically, the DCT is perfectly reversable and we do not lose any


image definition until we start quantizing coefficients. In my simulation, I
simply threw most of them out. A better quantizer would decrease precision
gradually instead of simply zeroing out components.

Below is the original image and reconstructions of it using only the most
significant n × n coefficients.

Original image 4x4 (25%)


3x3 (14%) 2x2 (6%)

Do those artifacts look familiar?

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.

The 1D DCT-II is:

N−1
1 2x + 1
Fu = Cu ∑ Sx cos (uπ )
2 x=0
2N

listing2.c uses a 1D DCT with complexity O (n2 ) , running it 2n times to build


a 2D DCT with complexity O (n3 ) .

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.

The AAN (Arai/Agui/Nakajima) algorithm is one of the fastest known 1D DCTs.


listing3.c shows a faster 2D DCT built from 1D AAN DCTs.
First written: 1 Sep 2001
Copyright © 2001-2011 Emil Mikulic.
This page uses the excellent MathJax library.

You might also like