CM310 6Chapter 10: Discrete Cosine Transform Prof David

Download Report

Transcript CM310 6Chapter 10: Discrete Cosine Transform Prof David

CM310 6Chapter 10: Discrete
Cosine Transform
Prof David Marshall
[email protected]
and
Dr Kirill Sidorov
[email protected]
www.facebook.com/kirill.sidorov
Scho olof Computer Science &Informatics
Cardiff University, UK
Moving into the Frequency Domain
Frequency domains can be obtained through the
transformationfromone (timeor spatial)domain tothe
other ( frequency ) via
Fou rier Transform (FT) (see Chapter 2 and recall
from CM2202)— MPEG Audio.
Discrete Cosine Transform (DCT) (new ) — Heart of
JPEG and
MPEG Video, MPEG Audio.
Note: Wemention someimage(and video)examples inthis
section with DCT (in particular) butalsothe FT is commonly
applied to filter multimedia data.
ExternalLink: MIT OCW 8.03 Lecture 11 Fourier Analy sis Video
CM3106 Chapter 10: DCT
Frequency Domain
1
Recap:Fourier Transform
The to olwhich converts a spatial
(real space) description of
audio/image da ta into one in terms of its frequency
comp onents is called the Fourier transform.
The new version is usually referred to as the Fou rie r space
description ofthe data.
We then essentially pro cess the data:
E.g . for filtering basically this means attenuating or
setting certain frequenciestozero
We then need toconvert databackto realaudio/imagery to
usein our applications.
The corresp onding inverse transformation which turnsa
Fourier space descriptionbackinto a real
space one is called
the inverse Fourier transform.
CM3106 Chapter 10: DCT
Frequency Domain
2
Recap:Whatdo Frequencies Meanin anImage?
Large values at high frequency comp onents mean the
data is changing rapidly on a short distance scale.
E.g .:a page of smallfont text, brick wall , vegetation.
Large low frequency comp onents then the large scale
features of the picture are more imp ortant.
E.g . a single fairly simple object which occupies most of
the image.
CM3106 Chapter 10: DCT
Frequency Domain
5
The Road toCompression
How do we achievecompression?
Low pass filter — ignore highfrequencynoise
comp onents
Only store lower frequency comp onents
High pass filter — sp ot gradual
changes
If changes are to o low/slow — eye do es not respond so
ignore?
CM3106 Chapter 10: DCT
Frequency Domain
6
Low Pass ImageCompression Example
MATLAB demo, dctdemo.m , (uses DCT) to
Load an image
Low pass filterinfrequency (DCT)space
Tune compression via a single sli der value to select
n
co efficients
Inverse DCT, subtract input and filtered imageto
see compression artefacts.
CM3106 Chapter 10: DCT
Frequency Domain
7
The Discrete CosineTransform (DCT)
Relationship between DCT and FFT
DCT (DiscreteCosineTransform)issimilar tothe DFT since it
decomp oses a signal
into a seriesofharmoniccosine functions.
DCT is actually a cut-down versionof the FourierTransform
orthe Fast Fourier Transform (FFT):
Onlythe real partof FFT(lessdata overheads).
Computationally simpler than FFT.
DCT— effectivefor multimedia compression (energy
compaction).
DCT MUCH more commonly us ed (than FFT) in
multimedia ima ge/vi deo compression — more later.
Cheap MPEG Audio variant — more later.
FT captures more frequency “fidelity” (e .g . phase).
CM3106 Chapter 10: DCT
Frequency Domain
8
DCT vs FT
(a) Fourier tran sform, (b) Sine transform, (c) Cosine
transform.
CM3106 Chapter 10: DCT
Frequency Domain
9
1D DCT
For N dataitems 1DDCT isdefined by:
2
N
F( u )=
1
2
N− 1
Λ( u) cos
i= 0
πu
(2i+ 1) f( i)
2N
and the corres p onding inverse 1D DCT transform is simply
F− 1(u) , i.e.:
f( i) =F
−1
=
(u)
2
N
1
2
N− 1
Λ( u) cos
u= 0
where
Λ(ξ)=
CM3106 Chapter 10: DCT
1D DCT
√1
2
1
πu
(2i+ 1) F( u),
2N
for ξ= 0,
otherwise.
10
DCT Example
Let’s consider a DC signal
that is aconstant 100,
i.e f( i)= 100 fori= 0 . . . 7 (see DCT1Deg.m ):
So the domain is[0,7] for both i and u
We therefore haveN= 8 samples and willneed to work
8values for u= 0. .. 7.
We can now seehow weworkoutF( u):
As u varies weworkcan work for each
u a comp onent or
a b a s is F
. ( u).
Within eachF( u), we ca m work out the value for each
Fi (u) to define a b as is func tio n
Basis function can b e pre-computed and simply lo oked up
in DCT computation.
CM3106 Chapter 10: DCT
1D DCT
11
Plots off( i) andF( U)
100
300
90
250
80
70
200
60
50
150
40
100
30
20
50
10
0
1
2
3
4
5
6
7
f ( i)= 100 fori= 0. . .7
CM3106 Chapter 10: DCT
1D DCT
8
0
1
2
3
4
5
6
7
8
F( u): F(0) ≈ 283, F(1 . . .7)= 0
12
DCT Example:F(0)
So foru= 0:
Note: Λ( 0)=
√1
2
and cos(0)= 1
So F(0) is computed as:
F(0) =
≈
1
√ (1 · 100+ 1 · 100+ 1 × 100+ 1 · 100+ 1 · 100
2 2
+ 1 · 100+ 1 · 100+ 1 · 100)
283
1
Here the valuesFi (0)= 2√ 2 (i= 0. .. 7).
Theseare bases ofFi (0)
CM3106 Chapter 10: DCT
1D DCT
13
F(0) Basis Function Plot
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
1
2
3
4
5
6
7
8
F(0) basis function
CM3106 Chapter 10: DCT
1D DCT
14
DCT Example:F(1 . . .7)
So foru= 1:
Note: Λ( 1)= 1 and we have cos to work out:
so F(1) is
computed as:
F(1)
=
=
1
3π
5π
7π
π
· 100
(cos · 100+ cos · 100+ cos · 100+ cos
2
16
16
16
16
9π
11π
13π
15π
· 100+ cos
· 100+ cos
· 100)
+ cos · 100+ cos
16
16
16
16
0
π
15π
(since cos16 =− cos 16 ,cos
Here the values
3π
16
13π
=− cos 16 e tc .)
1
π 1
3π 1
5π
1
11π 1
13π 1
15π }
Fi ( 1)= { cos , cos
, cos
,. . ., cos
, cos
, cos
2
16 2
16 2
16
2
16 2
16 2
16
form the ba sis function
F(2 . . .7) similarly= 0
CM3106 Chapter 10: DCT
1D DCT
15
F(1) Basis Function Plot
0.5
0.4
0.3
0.2
0.1
0
−0.1
−0.2
−0.3
−0.4
−0.5
1
2
3
4
5
6
7
8
F(1) basis function
Note:
Bases are re fle c te d ab o ut c e ntre and ne g ate d since
π
15π
3π
13π
cos16 =− cos 16 ,cos 16 =− cos 16 e tc .
ONLY as our example function is a constant is F(1) zero.
CM3106 Chapter 10: DCT
1D DCT
16
DCT Matlab Example
DCT1Deg.m explained:
i = 1:8% dim e ns io n o f v e c to r
f(i) = 100 % s e t fuc ntio n
figure(1)
% p lo tf
stem(f);
% c o mp ute DC T
D =dct(f);
figure(2)
% p lo tD
stem(D);
Create our function,f and plot it.
Use MATLAB 1Ddct functionto compute DCTof f and
plot it.
CM3106 Chapter 10: DCT
1D DCT
17
DCT Matlab Example
% Illus trate DC T b as e s c o m p ute DC T b as e s
% w ith dc tm tx
bases =dctmtx(8);
% Plo t b as e s :e ac h ro w (j) o f b as e s is the jth
% DC T Bas is Func tio n
for j= 1:8
figure % inc re me nt fig ure
stem(bases(j,:));
% p lo t ro w s
end
MATLAB dctmtx function computes DCT basis
functions.
Each rowj of bases isthe basis functionF( j).
Plot each row.
CM3106 Chapter 10: DCT
1D DCT
18
DCT Matlab Example
% c o ns truc t DC T fro m Bas is Func tio ns Sim p ly
% m ultip ly f’ (c o lum n v e c to r) b y b as e s
D1 =bases*f’;
figure
% p lo t D1
stem(D1);
Here we show howtocompute the DCTfrom thebasis
functions.
bases is an8 × 8 matrix, f an1 × 8 vector. Need column
8 × 1 form to do matrix multiplication so transp ose
f.
CM3106 Chapter 10: DCT
1D DCT
19
2D DCT
For a2D N by M image 2D DCT is defined:
1
2
2
N
F(u, v) =
1
2
2
M
N− 1 M− 1
Λ( u) Λ( v)×
i= 0 j= 0
πu
πv
cos
(2i+ 1) cos
(2j+ 1) · f(i , j)
2N
2M
and the corres p onding inverse 2D DCT transform is simply
F− 1(u , v), i.e.:
f (i, j)
=F
=
−1
2
N
cos
CM3106 Chapter 10: DCT
(u , v)
1
2
2
M
1
2
N− 1 M− 1
u= 0 v= 0
Λ( u) Λ( v)×
πu
πv
(2i+ 1) cos
(2j+ 1) · F(u, v) .
2N
2M
2D DCT
20
Applying The DCT
Similarto thediscrete Fourier transform:
It transforms a signalorimage from thespatial domain
tothe frequency domain.
DCTcan approximate lines wellwith fewer co efficients.
Helps separate the image into parts (or sp ectral
sub-bands) of differing imp ortance (with resp ect to the
image’s vis ual
quality).
CM3106 Chapter 10: DCT
2D DCT
21
Performing DCT Computations
The basic op eration of the DCT is as follows:
Theinput image is NbyM;
f (i, j) isthe intensityof the pixel in row i and columnj .
F(u, v) is the DCT co efficient in row
u i and col umnvj of
the DCT matrix.
For JPEGimage (andMPEG video), theDCT input is
usually an 8 by 8(or16by 16)array ofintegers.
This array contains each image window’s resp ective
colour band pixellevels.
CM3106 Chapter 10: DCT
2D DCT
22
Compression with DCT
For mostimages, muchof the signalenergy lies at low
frequencies;
These appear in the upp er left corner of the DCT.
Compressionis achievedsince the lower right values
representhigher frequencies, and are often small
Small enough to be neglected with little visible
distortion.
CM3106 Chapter 10: DCT
2D DCT
23
Separability
One of the prop erties of the 2-D DCT is that it is
separable meaning that it ca n b e separated into a pa ir of
1-D DCTs.
To obtain the 2-D DCT of a blo ck a 1-D DCT is first
performed on the rows of the blo ck then a 1-D DCT is
performed on the columns of the resulting blo ck.
The sa me applies to the IDCT.
CM3106 Chapter 10: DCT
2D DCT
24
Separability
Factoringreducesproblem to aseries of1DDCTs
(Noneed toapply 2Dform directly):
Aswith 2D Fourier Transform.
Apply1D DCT(vertically) to columns.
Apply1D DCT(horizontally) to resultantvertical DCT.
Or alternatively horizontalto vertical.
CM3106 Chapter 10: DCT
2D DCT
25
ComputationalIssues
The equations aregivenby:
G(i , v) =
1
2
F(u, v) =
1
2
Λ( v) cos
πv
(2j+ 1) f(i , j)
16
Λ( u) cos
πu
(2i+ 1) G(i , v)
16
i
i
Most software implementations use fixed p oint arithmetic.
Some fast implementations approximate co efficients so all
multiplies are shifts and adds.
CM3106 Chapter 10: DCT
2D DCT
26
2D DCT on an Image Blo ck
Imageis partitioned into8 x8 regions (SeeJPEG)—
The DCT input is an 8 x 8 array of integers.
So in N =M= 8, substitute these in DCT formula.
An 8 point DCT is then:
7
F(u, v) =
1
Λ( u) Λ( v)
4
i=
7
cos
0 j= 0
πu
(2i+ 1) ×
16
πv
cos
(2j+ 1) f(i , j) ,
16
where
√1
2
forξ= 0,
1 otherwise.
The output array of DCT co efficients contains integers;
these can rangefrom− 1024 to 1023.
Λ(ξ)=
CM3106 Chapter 10: DCT
2D DCT
27
2D DCT BasisFunctions
From the ab ove formula, extending what we have seen with
the 1D DCT we can derive basis fu nctions for the 2D DCT:
We havea basis for a 1D DCT (see
bases = dctmtx(8)
example ab ove).
We discussed ab ove that we can compute a DCT by first
doing a 1D DCT inonedirection (e.g.horizontally)
followedby a 1DCT on theintermediateDCT result.
This is equivalent to performing matrix pre-multiplication
by bases and matrix post-multiplication the transp ose of
bases.
take each rowi in bases and you get 8 basis matrices
for eachj .
there are 8 rows so we get 64 bas is matrices.
CM3106 Chapter 10: DCT
2D DCT
28
Visualisationof DCT 2D BasisFunctions
Computationally eas ier to implement and more efficient
to regard the DCTas a setof basis function s.
Givena known inputarraysize (8 x 8) they canbe
precomputed and stored.
The values assimply calculated fromDCT formula.
See MATLAB demo,
dctbasis.m, to see how
to pro duce these bases.
CM3106 Chapter 10: DCT
2D DCT
29
DCT Basis Functions
A =dctmtx(8);
A =A’;
offset =5;
basisim =ones(N*(N+offset))*0.5;
Basically justset up afew things:A = 1D DCT basis
functions
basisim will be used to create the plot of all64 basis
functions.
CM3106 Chapter 10: DCT
2D DCT
30
DCT Basis Functions
B=zeros(N,N,N,N);
for i=1:N
for j=1:N
B(:,:,i,j)=A(:,i)*A(:,j)’;
% max (m ax (B(:,:,i,j)))- min(min(B(:,:,i,j)))
end;
end;
B = computation of 64 2D bases.
Createa 4D array:firsttwodimensions store a 2D image
for eachi , j .
3rd and 4thdimensioni and j storethe 64 basis
functions.
CM3106 Chapter 10: DCT
2D DCT
31
DCT Basis Functions
for i=1:N
for j=1:N
minb = min(min(B(:,:,i,j)));
maxb = max(max(B(:,:,i,j)));
rangeb =maxb -minb;
if rangeb ==0
minb =0;
rangeb =maxb;
end;
imb = B(:,:,i,j) - minb;
imb =imb/rangeb;
CM3106 Chapter 10: DCT
2D DCT
32
DCT Basis Functions
iindex1 =(i-1)*N + i*offset-1;
iindex2 =iindex1 + N -1;
jindex1 =(j-1)*N + j*offset -1;
jindex2 =jindex1 + N -1;
basisim(iindex1: iindex2, jindex1:jindex2) = imb;
end;
end;
Basically just put up 64 2D ba sesbasisim
in
as image
data.
CM3106 Chapter 10: DCT
2D DCT
33
DCT Basis Functions
figure(1)
imshow(basisim)
figure(2)
dispbasisim = imresize(basisim,4,’bilinear’);
imshow(dispbasisim);
Plot normalsizeimage and one4times upsampled.
CM3106 Chapter 10: DCT
2D DCT
34