Professional Documents
Culture Documents
Discrete Cosine Transform Discrete Cosine Transform: Nuno Vasconcelos Ucsd
Discrete Cosine Transform Discrete Cosine Transform: Nuno Vasconcelos Ucsd
Nuno Vasconcelos
UCSD
energy compaction
is the ability to pack the energy of the spatial sequence into as
few frequency
q
y coefficients as p
possible
this is very important for image compression
we represent the signal in the frequency domain
if compaction is high
high, we only have to transmit a few coefficients
instead of the whole set of pixels
Image compression
an image compression system has three main blocks
Image compression
the transform throws away correlations
if you make a plot of the value of a pixel as a function of one of
its neighbors
you will see that the pixels are highly correlated (i.e. most of the
time they are very similar)
this is jjust a consequence
q
of the fact that surfaces are smooth
5
Image compression
the transform eliminates these correlations
this is best seen by considering the 2-pt transform
note that the first coefficient is always the DC-value
X = Tx,
T TT = I
Image compression
the transform eliminates these correlations
note that if x[0] similar to x[1], the
T TT = I
applies a rotation to the pixel space
this aligns the data with the canonical axes
7
Image compression
a second advantage of working in the
frequency domain
is that our visual system is less sensitive
to distortion around edges
the transition associated with the edge
masks our ability to perceive the noise
e.g. if you blow up a compressed picture,
it is likely to look like this
in general, the
compression
errors are more
annoying in the
smooth image
regions
8
Image compression
three JPEG examples
36KB
5.7KB
1.7KB
Image compression
important point: by itself, the transform
does not save any bits
does not introduce any distortion
10
Quantizer
it is a function of this type
inputs in a given range are mapped
to the same output
o tp t
to implement this, we
1)) define a q
quantizer step
p size Q
2) apply a rounding function
x
xq = round
Q
the larger
g the Q, the less reconstruction levels we have
more compression at the cost of larger distortion
e.g. for x in [0,255], we need 8 bits and have 256 color values
with Q = 64
64, we only have 4 levels and only need 2 bits
11
Quantizer
note that we can quantize some frequency coefficients
more heavily than others by simply increasing Q
this leads to the idea of a quantization matrix
we start with an image block (e.g. 8x8 pixels)
12
Quantizer
next we apply a transform (e.g. 8x8 DCT)
DCT
13
Quantizer
and quantize with a varying Q
DCT
Q mtx
14
Quantizer
note that higher frequencies are quantized more heavily
Q mtx
increasing frequency
in result
result, many high frequency coefficients are simply wiped out
DCT
quantized DCT
15
Quantizer
this saves a lot of bits, but we no longer have an exact
replica of original image block
DCT
quantized DCT
inverse DCT
original pixels
16
Quantizer
note, however, that visually the blocks are not very
different
original
i i l
d
decompressed
d
loss
this is the reason why JPEG and MPEG work
17
Image compression
three JPEG examples
36KB
5.7KB
1.7KB
N 1
k (2n + 1) , 0 k < N
2 x[n]cos
C x [k ] = n =0
2N
0
otherwise
1 N 1
w[k ]C x [k ]cos
k (2n + 1) 0 n < N
x[n] = N k =0
2N
otherwise
0
20
2 x[n]cos
k (2n + 1) , 0 k < N
C x [k ] =
2N
n =0
0
otherwise
=
x[2 N n 1], N n < 2 N
2 N 1
y[n]e
2
kn
2N
0 k < 2N
n =0
step
p 3):
) rewrite as a function of N terms only
y
N 1
Y [k ] = y[n]e
n =0
2
kn
2N
2 N 1
y[n]e
2
kn
2N
n= N
22
Y [k ] = x[n]e
2
kn
2N
n =0
2 N 1
x[2 N n 1]e
2
kn
2N
n= N
= (m = 2 N 1 n, n = 2 N 1 m ) =
N 1
= x[n]e
n =0
2
kn
2N
N 1
+ x[m]e
2
k ( 2 N 1 m )
2N
m =0
2
2
2
j
kn j
k 2N j
k
j 22N kn
2N
2N
2N
= x[n]e
+e
e1
424
3e
n =0
1
N 1
2
2
j
kn j
k
j 22N kn
2N
2N
= x[n]e
+e
e
n =0
N 1
23
n =0
N 1
N 1
= x[n ]e
2N
n =0
N 1
= 2 x[n ]e
n =0
=e
2N
2N
k
j
kn j
j 22N kn j 2N k
2N
2N
e
+e
e
e
cos
k ( 2n + 1)
2N
2
x
[
n
]
cos
k
(
2
n
+
1
)
2N
n =0
k N 1
from which
Y [k ] = e
2N
C x [k ],
0 k < 2N
24
0,
otherwise
x[n] {
y[n]
{
N pt
2 N pt
[k ] C12
Y{
x [k ]
3
DFT
2 N pt
N pt
Energy compaction
x[n] {
y[n]
{
N pt
2 N pt
[k ] C12
Y{
x [k ]
3
DFT
2 N pt
N pt
y[n]
Energy compaction
x[n] {
y[n]
{
N pt
2 N pt
[k ] C12
Y{
x [k ]
3
DFT
2 N pt
N pt
DCT
Fast algorithms
the interpretation of the DCT as
x[n] {
y[n]
{
N pt
2 N pt
[k ] C12
Y{
x [k ]
3
DFT
2 N pt
N pt
C x [k ] = e
Y [k ], 0 k < N
0,
otherwise
2N
28
2D DCT
the extension to 2D is trivial
the procedure is the same
x[n1 , n2 ] y[n1 , n2 ]
1
424
3
1
424
3
N1 N 2 ppt
pt
2 N1 2 N 2 p
2D DFT
Y [k1 , k 2 ] C x [k1 , k 2 ]
1
424
3
1
424
3
pt
2 N1 2 N 2 p
N1 N 2 p
pt
with
+ x[n1 ,2 N 2 1 n2 ] + x[2 N1 1 n1 ,2 N 2 1 n2 ]
and
j 2N k1 j 2N k2
0 k1 < N1
1
2
e
Y [k1 , k 2 ],
C x [k1 , k 2 ] = e
0 k2 < N 2
0,
otherwise
29
2D DCT
the end result is the 2D DCT pair
N 1 1N 2 1
0 k1 < N 1
4x [n1 , n 2 ]cos
k 1 (2n1 + 1) cos
k 2 (2n 2 + 1) ,
C x [k 1 , k 2 ] = n1 =0 n 2 =0
0 k2 < N2
2N 1
2N 2
otherwise
0
1 N 1 1N 2 1
0 n1 < N 1
[
]
w
[
k
]
w
[
k
]
C
k
,
k
cos
k
(
2
n
+
1
)
cos
k
(
2
n
+
1
)
1 1 2 2 x 1 2 2N 1 1
2
2
x [n1 , n 2 ] = N 1N 2 k
1
2N 2
0 n2 < N 2
1 =0 k 2 =0
0
otherwise
with
1 ,
k1 = 0
,
w1 [k1 ] = 2
1, 1 k1 < N1
1 ,
k2 = 0
w2 [k 2 ] = 2
1, 1 k 2 < N 2
2D-DCT
1) create
intermediate
sequence by
computing
1D-DCT of
rows
n2
n2
1D-DCT
k1
n1
f [ k1 , n 2 ]
x [ n1 , n 2 ]
2) compute
1D-DCT of
columns
n2
k2
1D-DCT
1D
DCT
k1
f [k1 , n 2 ]
k1
C x [k1 , k 2 ]
31
32