Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
cp467_12_lecture14_image compression1.pdf
1. Lecture 14 Image Compression
1. What and why image compression
2 B i t
2. Basic concepts
3. Encoding/decoding, entropy
2. What is Data and Image Compression?
ƒ Data compression is the art and science of representing
information in a compact form
information in a compact form.
ƒ Data is a sequence of symbols taken from a discrete
l h b t
alphabet.
3. Why do we need Image Compression?
Still Image
g
• One page of A4 format at 600 dpi is > 100 MB.
• One color image in digital camera generates 10-30 MB.
• Scanned 3”×7” photograph at 300 dpi is 30 MB.
Digital Cinema
Digital Cinema
•4K×2K×3 ×12 bits/pel = 48 MB/frame or 1 GB/sec
or 70 GB/min.
4. Why do we need Image Compression?
1) Storage
2) Transmission
)
3) Data access
1990-2000
Disc capacities : 100MB -> 20 GB (200 times!)
Disc capacities : 100MB -> 20 GB (200 times!)
but seek time : 15 milliseconds Æ 10 milliseconds
and transfer rate : 1MB/sec ->2 MB/sec.
Compression improves overall response time in some
applications.
pp
5. Source of images
•Image scanner
•Digital camera
•Video camera,
•Ultra-sound (US), Computer Tomography (CT),
Magnetic resonance image (MRI), digital X-ray (XR),
Infrared.
•etc.
7. Why we can compress image?
• Statistical redundancy:
1) Spatial correlation
1) Spatial correlation
a) Local - Pixels at neighboring locations have
similar intensities.
b) Global - Reoccurring patterns.
2) Spectral correlation – between color planes.
3) Temporal correlation – between consecutive frames.
• Tolerance to fidelity:
1) P t l d d
1) Perceptual redundancy.
2) Limitation of rendering hardware.
8. Lossy vs. Lossless compression
Lossless compression: reversible, information preserving
text compression algorithms,
binary images, palette images
Lossy compression: irreversible
grayscale, color, video
Near-lossless compression:
Near lossless compression:
medical imaging, remote sensing.
10. Distortion measures
Mean average error (MAE): ∑
=
−
=
N
i
i
i x
y
N 1
1
MAE
i 1
Mean square error (MSE): ( )
∑ −
=
N
i
i x
y
N
2
1
MSE ( )
∑
=
i
N 1
Signal-to-noise ratio (SNR): [ ]
MSE
log
10
SNR 2
10 σ
⋅
=
[ ]
2
Signal to noise ratio (SNR): [ ]
g10
(decibels)
[ ]
MSE
log
10
PSNR 2
10 A
⋅
=
Pulse-signal-to-noise ratio (PSNR):
(decibels)
A is amplit de of the signal A 28 1 255 for 8 bits signal
A is amplitude of the signal: A = 28-1=255 for 8-bits signal.
11. Other issues
• Coder and decoder computation complexity
• Memory requirements
• Fixed rate or variable rate
• Error resilience
• Symmetric or asymmetric
• Decompress at multiple resolutions
• Decompress at various bit rates
• Decompress at various bit rates
• Standard or proprietary
12. Entropy
Set of symbols (alphabet) S={s1, s2, …, sN},
N is number of symbols in the alphabet.
y p
Probability distribution of the symbols: P={p1, p2, …, pN}
According to Shannon, the entropy H of an information
According to Shannon, the entropy H of an information
source S is defined as follows:
∑ ⋅
−
=
N
i
i p
p
H 2 )
(
log
∑
=
i
i
i p
p
1
2 )
(
g
13. Entropy
The amount of information in symbol si,
i h d h b f bi d d l h
in other words, the number of bits to code or code length
for the symbol si:
)
(
log
)
( 2 i
i p
s
H −
= 2 i
i
The average number of bits for the source S:
∑ ⋅
−
=
N
i
i
i p
p
H
1
2 )
(
log
=
i 1
14. Entropy for binary source: N=2
S={0 1}
1-p
S {0,1}
p0=p
p1=1-p
p
p1 1 p
0 1
))
1
(
log
)
1
(
log
( 2
2 p
p
p
p
H −
⋅
−
+
⋅
−
= ))
(
g
)
(
g
( 2
2 p
p
p
p
H=1 bit for p0=p1=0.5
H 1 bit for p0 p1 0.5
15. Entropy for uniform distribution: pi=1/N
Uniform distribution of probabilities: pi=1/N:
)
(
log
)
/
1
(
log
)
/
1
( 2
1
2 N
N
N
H
N
i
∑
=
=
⋅
−
=
Pi=1/N
s1 s2 sN
Examples:
N= 2: pi=0.5; H=log2(2) = 1 bit
pi ; g2( )
N=256: pi=1/256; H=log2(256)= 8 bits
16. How to get the probability distribution?
1) Static modeling:
a) The same code table is applied to all input data.
b) One-pass method (encoding)
c) No side information
2) Semi-adaptive modeling:
2) Semi adaptive modeling:
a) Two-pass method: (1) analysis and (2) encoding.
b) Side information needed (model, code table)
3) Adaptive (dynamic) modeling:
a) One-pass method: analysis and encoding
b) Updating the model during encoding/decoding
c) No side information
17. Static vs. Dynamic: Example
S = {a,b,c}; Data: a,a,b,a,a,c,a,a,b,a.
1) Static model: pi=1/10
H = -log2(1/10)=1.58 bits
2) Semi-adaptive method: p1=7/10; p2=2/10; p3=1/10;
H = -(0.7*log20.7 + 0.2*log20.2 + 0.1*log20.1)=1.16 bits
H (0.7 log20.7 0.2 log20.2 0.1 log20.1) 1.16 bits
18. 3) Adaptive method: Example
S = {a,b,c}; Data: a,a,b,a,a,c,a,a,b,a.
S b l 1 2 3 4 5 6 7 8 9 10
Symbol 1 2 3 4 5 6 7 8 9 10
a 1 2 3 3 4 5 5 6 7 7
b 1 1 1 2 2 2 2 2 2 3
c 1 1 1 1 1 1 2 2 2 2
pi 0.33 0.5 0.2 0.5 0.57 0.13 0.56 0.60 0.18 0.58
H 1.58 1.0 2.32 1.0 0.81 3.0 0.85 0.74 2.46 0.78
H=(1/10)(1.58+1.0+2.32+1.0+0.81+3.0+0.85+0.74+2.46+0.78)
H (1/10)(1.58 1.0 2.32 1.0 0.81 3.0 0.85 0.74 2.46 0.78)
=1.45 bits/char
1.16 < 1.45 < 1.58
S.-Ad. Ad. Static
20. Shannon-Fano Code: A top-down approach
1) Sort symbols according their probabilities:
) y g p
p1 ≤ p2 ≤ … ≤ pN
2) R i l di id i t t h ith th
2) Recursively divide into parts, each with approx. the same
number of counts (probability)
21. Shannon-Fano Code: Example (1 step)
si pi
A B C D E
A - 15/39
B - 7/39
C - 6/39
D 6/39
A,B, C,D,E
15,7, 6,6,5
0 1
D - 6/39
E - 5/39
A,B C,D,E
0 1
15+7 =22 6+6+5=17
22. Shannon-Fano Code: Example (2 step)
si pi
A B C D E
A - 15/39
B - 7/39
C - 6/39
D 6/39
A,B, C,D,E
15,7, 6,6,5
0 1
D - 6/39
E - 5/39
A,B C,D,E
0 1
15+7 =22 6+6+5=17
0 0
1 1
A
15
B
7
C
6
D,E
6+5=11
23. Shannon-Fano Code: Example (3 step)
si pi
A B C D E
A - 15/39
B - 7/39
C - 6/39
D 6/39
A,B, C,D,E
15,7, 6,6,5
0 1
D - 6/39
E - 5/39
A,B C,D,E
0 1
15+7 =22 6+6+5=17
0 1 0 1
A
15
B
7
C
6
D,E
6+5=11
0 1
D
6
E
5
24. Shannon-Fano Code: Example (Result)
Symbol pi -log2(pi) Code Subtotal
y pi g2(pi)
A 15/39 1.38 00 2*15
B 7/39 2.48 01 2*7
C 6/39 2 70 10 2*6
C 6/39 2.70 10 2*6
D 6/39 2.70 110 3*6
E 5/39 2.96 111 3*5
T t l 89 bit
Total: 89 bits
0 1
0 1
1 0
0 1
1 0
1
0
A C
B
D
Binary tree
D E
H=89/39=2.28 bits
25. Shannon-Fano Code: Encoding
A - 00 Message: B A B A C A C A D E
B - 01
C - 10
D - 110
Message: B A B A C A C A D E
Codes: 01 00 01 00 10 00 10 00 110 111
Bitstream: 0100010010001000110111
D 110
E - 111
Bitstream: 0100010010001000110111
0 1
0 1
1 0
0 1
1 0
1
0
A C
B
D
Binary tree
D E
26. Shannon-Fano Code: Decoding
A - 00 Bitstream: 0100010010001000110111 (23 bits)
B - 01
C - 10
D - 110
Bitstream: 0100010010001000110111 (23 bits)
Codes: 01 00 01 00 10 00 10 00 110 111
Messaage: B A B A C A C A D E
D 110
E - 111
0 1
0 1
1 0
0 1
1 0
1
0
A C
B
D
Binary tree
D E
27. Huffman Code: A bottom-up approach
INIT:
Put all nodes in an OPEN list keep it sorted all times
Put all nodes in an OPEN list, keep it sorted all times
according their probabilities;.
REPEAT
REPEAT
a) From OPEN pick two nodes having the lowest
probabilities, create a parent node of them.
b) Assign the sum of the children’s probabilities
to the parent node and inset it into OPEN
c) Assign code 0 and 1 to the two branches of the
tree, and delete the children from OPEN.
28. Huffman Code: Example
Symbol pi -log2(pi) Code Subtotal
A 15/39 1 38 0 2*15
A 15/39 1.38 0 2*15
B 7/39 2.48 100 3*7
C 6/39 2.70 101 3*6
D 6/39 2.70 110 3*6
E 5/39 2.96 111 3*5
Total: 87 bits
39/39
0 1
1
0
A
Binary tree
11/39
15/39
13/39
24/39
39/39
1
0
C D E
1
B
Binary tree
6/39 5/39
6/39
7/39
13/39
H=87/39=2.23 bits
6/39
29. Huffman Code: Decoding
A - 0 Bitstream: 1000100010101010110111 (22 bits)
B - 100
C - 101
D - 110
( )
Codes: 100 0 100 0 101 0 101 0 110 111
Message: B A B A C A C A D E
E - 111
0 1
0 1
1
0
1
0
A
1
Binary tree
1
0
C D E
1
B
30. Properties of Huffman code
• Optimum code for a given data set requires two passes.
p g q p
• Code construction complexity O(N logN).
• Fast lookup table based implementation
Fast lookup table based implementation.
• Requires at least one bit per symbol.
A d d l h i i hi bi f d
• Average codeword length is within one bit of zero-order
entropy (Tighter bounds are known): H ≤ R < H+1 bit
Susceptible to bit errors
• Susceptible to bit errors.
31. Unique prefix property
No code is a prefix to any other code all symbols are
No code is a prefix to any other code, all symbols are
the leaf nodes
Shannon Fano and Huffman
0
0
1
1 C
Shannon-Fano and Huffman
codes are prefix codes
(D)
NOT prefix
0 1
A B
Legend: Shannon (1948) and Fano (1949);
Huffman (1952) was student of Fano at MIT.
Fano: ”Construct minimum-redundancy code → final exam is passed!”
32. Predictive coding
1) Calculate prediction value: yi=f(neibourhood of xi)
1) Calculate prediction value: yi f(neibourhood of xi).
2) Calculating the prediction error: ei= yi- xi.
3) Encode the prediction error e
3) Encode the prediction error ei.
33. Predictive model for grayscale images
y=xi-xi-1
Histogram of the original image and Residual image
E t H 7 8 bit / l (?) H 5 1 bit / l (?)
Entropy: Ho= 7.8 bits/pel (?) Hr=5.1 bits/pel (?)
34. Coding without prediction
f 8 8/64 0 125
f0=8; p0=p=8/64 =0.125;
f1=56; p1 =(1-p)=56/64=0.875
Entropy:
H =-((8/64)*log2(8/64)+(56/64)*log2(56/64))=0.544 bits/pel
35. Prediction for binary images by pixel above
f p
f p
16 16/64
48 48/64
Entropy:
H =-((16/64)*log2(16/64)+(48/64)*log2(48/64))=0.811 bits/pel
Wrong predictor!
Wrong predictor!
36. Prediction for binary images pixel to the left
f p
1 16/64
63 63/64
Entropy:
H =-((1/64)*log2(1/64) + (63/64)*log2(63/64)) =0.116 bits/pel
Good predictor!
37. Comparison of predictors:
• Without prediction: H= 0.544 bits/pel
• Prediction by pixel above: H = 0.811 bits /pel (bad!)
y p p ( )
• Prediction by pixel to the left: H=0.116 bits/pel (good!)
38. Shortcoming of Huffman codes
Alphabet: a, b.
pa=p=0.99, pb=q=0.01
1) Entropy
H1=-(p*log2(p)+q*log2(q))=0.081 bits/pel
H1 (p log2(p)+q log2(q)) 0.081 bits/pel
2) Huffman code: pa=’0’, pb=’1’
Bitrate R1 = 1*p+1*q = p+q = 1 bit/pel!
Make a new alphabet blocking symbols!
g y
41. Block coding: n→ ∞
pa=p=0.99, pb=q=0.01
Entropy Hn=0.081 bits/pel
Bitrate for Hufman coder:
1 R 1 0 bit 2 b l i l h b t
n= 1: R1 = 1.0 bit 2 symbols in alphabet
n= 2: R2 = 0.515 bits 4 symbols in alphabet
n= 3: R3 = 0.353 bits 8 symbols in alphabet
If block size n → ∞? Hn ≤ Rn < Hn+1/n
∑
∑ +
≤
≤
N
N
B
p
B
p
R
B
p
B
p 2
*
2
1
)
(
log
)
(
1
)
(
log
)
(
1
Problem - alphabet size and Huffman table size grows
∑
∑ =
=
+
≤
≤
i
n
n
i
n
n
n
n
B
p
B
p
n
R
B
p
B
p
n 1
2
1
2 )
(
log
)
(
)
(
log
)
(
exponentially with number of symbols n blocked.
42. Block coding: Example 2, n=1
p =56/64
pa=56/64
pb=8/64
1) Entropy
H=-((56/64)*log (56/64)+(8/64)*log (8/64))=0 544 bits/pel
H=-((56/64) log2(56/64)+(8/64) log2(8/64))=0.544 bits/pel
2) Huffman code: a= ’0’; b=’1’
Bit t R 1 bit/ l
Bitrate: R= 1 bit/pel
43. Block coding: Example 2, n=4
pA=12/16
pB=4/16
B
1) Entropy
H=-((12/16)*log2(12/16)+(4/16)*log2(4/16))/4=0.203 bits/pel
H ((12/16) log2(12/16) (4/16) log2(4/16))/4 0.203 bits/pel
2) Huffman code: A=’0’, B=’1’
Bitrate R = (1*pA+1*pB)/4=0.250 bits/pel
Bitrate R (1 pA 1 pB)/4 0.250 bits/pel
44. Binary image compression
• Run-length coding
• Predictive coding
• READ code
• Block coding
• G3 and G4
• JBIG: Prepared by Joint Bi-Level Image Expert Group in
1992
1992
45. Compressed file size
Model size
n=1: Model size: p p 21*8 bits
n=1: Model size: pa, pb → 21*8 bits
n=2: Model size: pA, pB , pC, pD → 22*8 bits
n=k: Model size: {pA, pB , …, pD} → 2k*8 bits
Compressed data size for S symbols in input file:
R*S bits, where R is bitrate (bits/pel)
Total size: Model size + R*S bits
Difference between entropy H and bitrate R!
46. Run-length coding idea
• Pre-processing method, good when one symbol
p g , g y
occurs with high probability or when symbols are
dependent
C t h t d b l
• Count how many repeated symbol occur
• Source ’symbol’ = length of run
Example: …, 4b, 9w, 2b, 2w, 6b, 6w, 2b, ...
47. Run-length encoding: CCITT standard
Run length encoding: CCITT standard
R l ti
Resolution:
Image: 1728*1,188
or 2 Mbytes
Transmission time: T=7 min
48. Run-length encoding: Example
Run length encoding: Example
RL Code
RL Code
4 b ’011’
9 w ’10100’
2 b ’11
2 w ’0111’
6 b ’0010’
6 b 0010
6 w ’1110’
2 b ’11’
50. Run-length Huffman encoding: n > 63
Run length Huffman encoding: n > 63
?
Examples:
n=30w: code=’00000011’
?
n=94w=64w+30w: code=’11011 00000011’
n=64w=64w+ 0w: code=’11011 00110101’
51. Predictive coding: Idea
Predictive coding: Idea
• Predict the pixel value on the basis of past pixel(s)
• Send ‘0’ if prediction is correct, ‘1’ if prediction is not
correct.
P di t f
Predictor for xi : yi = xi-1
Prediction error: ei = xi-xi-1
Example: alphabet S = {0 1}
Example: alphabet S = {0,1}
Data: (0) 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 → H=1.0 bit
Errors: 0 0 0 0 1 0 0 0 0 0 0 0 -1 0 0 0
(If e < 0 then e = e+2) Why 2?
Errors: 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 → H=0.5 bit
53. READ Code (1)
READ Code (1)
• Code the location of run boundary relative to the
• Code the location of run boundary relative to the
previous row.
READ = ”Relative Element Address Designate”
g
• The READ code includes three coding modes:
o Pass mode
o Vertical mode
o Horizontal mode
54. READ Code: Principles
READ Code: Principles
• Vertical mode:
The position of each color change is coded with respect
to a nearby change position of the same color on the
reference line if one exists "Nearby" is taken to mean
reference line, if one exists. Nearby is taken to mean
within three pixels.
• Horizontal mode:
There is no nearby change position on the reference line,
one-dimensional run-length coding - called
• Pass code:
Pass code:
The reference line contains a run that has no counterpart
in the current line; next complete run of the opposite
l i th f li h ld b ki d
color in the reference line should be skipped.
55. READ: Codes fo modes
READ: Codes fo modes
wl = length of the white run bl = length of the black run
H = Huffman code of white run Hb = Huffman code of black run
Hw Huffman code of white run Hb Huffman code of black run
(For Hufman codes see previous slides)
56. READ code
READ code
• There is an all-white line above the page, which used as the
reference line for the 1st scan line of the page.
• Each line is assumed start with a white pixel
white pixel, which is ignored by
receiver.
• Pointer a0 is set to an imaginary white pel on the left of the coding
line, and a1 is set to point to the 1st black pel on the coding line.
The first run length is | a a | 1
The first run length is | a0a1 |-1.
• Pointers b1 and b2 are set to point to the start of the 1st and 2nd
runs on the reference line, respectively.
• The encoder assumes an extra pel
extra pel on the right of the line, with a
color opposite that of the last pixel.
57. Pass (a) and Vertical mode (b1 b2)
Pass (a) and Vertical mode (b1,b2)
60. READ Code: Example
READ Code: Example
reference line
current line
vertical mode
010
horizontal mode pass
code
vertical mode horizontal mode
-1 0 3 white 4 black +2 4 white 7 black
-2
1 1000 011 0001 000011 000010 00011
001
001 1011
010
code generated
1 1000 011 0001 000011 000010 00011
001
001 1011
61. Block Coding: Idea
Block Coding: Idea
• Divide the image into blocks of pixels.
g p
• A totally white block (all-white block) is coded by ’0’.
All other blocks (non white blocks) thus contain at least
• All other blocks (non-white blocks) thus contain at least
one black pixel. They are coded with a 1-bit as a prefix
followed by the contents of the block (bit by bit in
followed by the contents of the block (bit by bit in
row-major order) or with Huffman code.
• The Block Coding can be applied to difference (error)
• The Block Coding can be applied to difference (error)
image for predictive coding approach.
(see also Lecture 2)
65. Hierarchical block encoding: Principle
Hierarchical block encoding: Principle
• In the hierarchical variant of the block coding the bit map
g p
is first divided into b*b blocks (typically 16*16).
• These blocks are then divided into quadtree structure of
q
blocks in the following manner:
If a particular b*b block is all-white, it is coded by ’0’.
Oth i th bl k i d d b ’1’ d th di id d i t
Otherwise the block is coded by ’1’ and then divided into
four equal sized subblocks which are recursively coded
in the same manner.
in the same manner.
68. Hierarchical block encoding: Example
Hierarchical block encoding: Example
Image to be compressed: Code bits:
1 0111 0011 0111 1000 0111 1111 1111
1 0111 0011 0111 1000
x x
x x
0111 1111 1111
0101 1010 1100
x x x x
x x x x
x x
x x
x x
x x
x x x x
x x x x
x x
x x
x x
x x
x x
x x
1 x x
x x
x x
x x
1
0 1 1 1
1
0 0 1 1 0 1 1 1 1 0 0 0
1+4+12+24=41
0111 1111 1111 0101 1010 1100
69. CCITT Group 3 (G3) and Group 4 (G4)
CCITT Group 3 (G3) and Group 4 (G4)
• The RLE and READ algorithms are included in image
g g
compression standards, known as CCITT G3 and G4.
(used in FAX-machines).
Buffer
RLE
0101101100...
Run length
Bits
Pixel
READ
Boundary Bits
points
70. CCITT Group 3 (G3)
CCITT Group 3 (G3)
• Every k th line is coded by RLE method and
• Every k-th line is coded by RLE-method and
the READ-code is applied for the rest of the lines.
• The first (virtual) pixel is white
( ) p
• EOL code after every line to synchronize code
• Six EOL codes after every page
• Binary documents only
71. CCITT Group 4 (G4)
CCITT Group 4 (G4)
• All lines are codes by READ
• The first reference line (above image) is white
• EOL code after every line to synchronize code
• Six EOL codes after every page
• Option for grayscale and color images
72. G3 and G4: Results
G3 and G4: Results
Resolution Low (200×100) High (200×200)
Scheme G3 G4 G3 G4
Bits per pel 0.13 0.11 0.09 0.07
Seconds 57 47 74 61
7 min → 1 min
73. Comparison of algorithms
Comparison of algorithms
15 0
20.0
25.0
Compression ratio
5.0
10.0
15.0
7.9 9.8
18.0 18.9 17.9
23.3
10.3
8.9 10.8 11.2
BLOCK RLE 2D-RLE ORLE G3 G4 JBIG
0.0
7.9
PKZIP
GZIP
COMPRESS
COMPRESS = Unix standard 2D-RLE = 2-dimensional RLE [WW92]
COMPRESS Unix standard
compression software
GZIP = Gnu compression software
PKZIP = Pkware compression software
BLOCK Hi hi l bl k di [KJ80]
2D RLE 2 dimensional RLE [WW92]
ORLE = Ordered RLE [NM80]
G3 = CCITT Group 3 [YA85]
G4 = CCITT Group 4 [YA85]
BLOCK = Hierarchical block coding [KJ80]
RLE = Run-length coding [NM80]
JBIG = ISO/IEC Standard draft [PM93]
74. Quantization
Any analog quantity that is to be processed by a digital
Any analog quantity that is to be processed by a digital
computer or digital system must be converted to an
integer number proportional to its amplitude. The
conversion process between analog samples and
discrete-valued samples is called quantization.
Q
Q
Input signal Quantized signal
Quantizer
78. Quantization error
I t i l
Input signal x
Quantized signal q(x)
Quantization error:
e(x) = x−q(x)
79. Distortion measure
Probability density function (pdf) of x is p(x)
Probability density function (pdf) of x is p(x)
Quantization error: e(x) = x − q (x)
Mean (average value) μ of quantization error:
Mean (average value) μ of quantization error:
dx
x
p
y
x
x
q
x
E
M a j
)
(
)
(
)]
(
[ ∑ ∫ −
=
−
=
μ dx
x
p
y
x
x
q
x
E
j a
i
j
)
(
)
(
)]
(
[
1 1
∑ ∫
= −
=
=
μ
Variance σ2 of quantization error as distortion measure:
Variance σ of quantization error as distortion measure:
dx
x
p
y
x
x
q
x
E
M a
j
j
)
(
)
(
]
))
(
[( 2
2
2
∑ ∫ −
=
−
=
σ p
y
q
j a
j
j
j
)
(
)
(
]
))
(
[(
1 1
∑ ∫
= −
80. Optimal quantization problem
Optimal quantization problem
Gi i l ith b bilit d it f ti
Given a signal x, with probability density function
(or histogram) p(x), find a quantizer q(x) of x, which
minimizes the quantization error variance σ2:
dx
x
p
y
x
M a
j
j
)
(
)
(
min
2
2
opt ∑ ∫ −
=
σ p
y
j a
j
y
a
j
j
j
)
(
)
(
min
1
}
{
},
{
opt
1
∑ ∫
= −
82. Part 1: DPCM
y=xi-xi-1
Histogram of the original image and Residual image
E t H 7 8 bit / l (?) H 5 1 bit / l (?)
Entropy: Ho= 7.8 bits/pel (?) Hr=5.1 bits/pel (?)
83. Prediction error quantization with open loop
Prediction error quantization with open loop
ei=xi−xi-1 → q(ei)
DPCM is Differential Pulse Code Modulation
84. Quantization with open loop: Decoding
Quantization with open loop: Decoding
yi=yi-1+q(ei)
Problem: error accumulation!
Problem: error accumulation!
W/o quantization With quantization
xn=xn-1+ei ⇒ yn=yi-1+q(en)
yn−xn= [x1+q(e2)+... +q(en)] − [x1+e2+... +en]=
= (q(e2)−e2)+... +(q(en) −en);
2
2
2
)
1
( q
x
y n σ
σ
σ −
+
=
Variance:
88. Entropy
Entropy
Entropy reduction: xi-1
xi
py
ΔH=H0−H1=log2(σ0/σ1)
σ2
1=2σ2
0(1 − ρ(Δ)),
Δ Δ Δ
where σ2
0 is variance of the data x,
σ2
1 is variance of the predection error e,
Δ Δ Δ
ρ(Δ) is correlation coefficient of the pixels xi and xi-1
or ΔH= 0.5log2[2(1 − ρ(Δ))].
Example: If ρ(Δ) =0.8 → −log2[2*0.2]=1.3 bits
If ρ(Δ) =0.9 → −log2[2*0.1]=2.3 bits
89. Optimum linear prediction
Optimum linear prediction
∑
m
ˆ ∑
=
−
=
j
j
i
j
i x
a
x
1
ˆ
• 1-D Linear predictor:
• 2-D and 3-D linear predictors
Usually m=3
• 2-D and 3-D linear predictors
90. Part 2 Block Truncation Coding
Part 2. Block Truncation Coding
• Divide the image into 4×4 blocks;
• Quantize the block into two representative values a and b;
• Encode (1) the representative values a and b
d (2) h i ifi i h bl k
and (2) the significance map in the block.
Original Bit-plane Reconstructed
2 11 11 9
2 9 12 15
0 1 1 1
0 1 1 1
2 12 12 12
2 12 12 12
3 3 4 14
2 3 12 15
0 0 0 1
0 0 1 1
2 2 2 12
2 2 12 12
x = 7.94 q = 9 a = 2.3
a=[2.3] = 2
= 4.91
q
b = 12.3
σ
a [2.3] 2
b=[12.3]=12
91. 1 How to construct quantizer?
1. How to construct quantizer?
• The first two moments preserving quantization:
p g q
∑
=
>=
<
m
i
i
m x
x
1
1
∑
>=
<
m
i
i
m x
x
1
2
1
2
i 1 =
i 1
2
2
2
>
<
−
>
=< x
x
σ
• Threshold for quantization: T=<x>; n +n =m
• Threshold for quantization: T=<x>; na+nb=m
b
n
a
n
x
m b
a +
>=
<
2
2
2
b
n
a
n
x
m +
>=
<
b
n
x
a ⋅
−
>
=< σ a
n
x
b ⋅
+
= σ
b
n
a
n
x
m b
a +
>=
<
a
n
x
a >
=< σ
b
n
x
b ⋅
+
= σ
92. 2 Optimal scalar quantizer (”AMBTC”)
2. Optimal scalar quantizer ( AMBTC )
• Minimize quantization error:
( ) ( )
⎪
⎭
⎪
⎬
⎫
⎪
⎩
⎪
⎨
⎧
−
+
−
= ∑
∑ i
i
T
b
a
b
x
a
x
D
2
2
,
,
min
⎪
⎭
⎪
⎩ ≥
< T
x
T
x
b
a
i
i
,
,
• Max-Lloyd solution:
∑
⋅
= x
a
1
∑
⋅
= x
b
1
2
b
a
T
+
=
• Max-Lloyd solution:
∑
<
=
T
x
i
a i
x
n
a ∑
≥
⋅
=
T
x
i
b i
x
n
b
2
• How to find the a,b,T? See Max-Lloyd algorithm.
93. Example of BTC
Example of BTC
Original Bit-plane Reconstructed
2 11 11 9
2 9 12 15
0 1 1 1
0 1 1 1
2 12 12 12
2 12 12 12
3 3 4 14
2 3 12 15
0 0 0 1
0 0 1 1
2 2 2 12
2 2 12 12
T 9
x = 7.94
= 4.91
q = 9 a = 2.3
b = 12.3
σ
T= 9
na=7
nb=9
50
43
7
2
2
D
a=[2.3] = 2
b=[12.3]=12
50
43
7
2
2
=
+
=
+
= b
a
D σ
σ
2 3 4 5 6 7 8 9 10 11 12 13 14 15
a b
T
94. Example of optimal quantizer (”AMBTC”)
Example of optimal quantizer ( AMBTC )
Original Bit-plane Reconstructed
2 11 11 9
2 9 12 15
0 1 1 1
0 1 1 1
2 12 12 12
2 12 12 12
3
3
3 3 4 14
2 3 12 15
0 0 0 1
0 0 1 1
2 2 2 12
2 2 12 12
T 9
3 3
3 3 3
x = 7.94
= 4.91
q = 9 a = 2.3
b = 12.3
σ
T= 9
na=7
nb=9
a=[2.7] = 3
b=[12.0]=12
47
43
4
2
2
D 47
43
4
2
2
=
+
=
+
= b
a
D σ
σ
2 3 4 5 6 7 8 9 10 11 12 13 14 15
b
a T
95. Representative levels compression
Representative levels compression
Main idea of BTC:
• Main idea of BTC:
Image → ”smooth part” + ”detailed part”
(a and b) (bit-planes)
(a and b) (bit-planes)
W t t t f ’ d b’ i
• We can treat set of a’s and b’s as an image:
1. Predictive encoding of a and b
2 Lossless image compression algorithm
2. Lossless image compression algorithm
(FELICS, JPEG-LS, CALIC).
3 Lossy compression: DCT (JPEG)
3. Lossy compression: DCT (JPEG)
96. Significance bits compression
Significance bits compression
Binary image:
Binary image:
• Lossless binary image compression
methods (JBIG, context modeling with
arithemtic coding)
• Lossy image compression (vector
ti ti ith b li d
quantization, with sub-sampling and
interpolating missing pixels, filtering)
97. Bitrate and Block size
Bitrate and Block size
The number of pixels in block: k2 pels
p p
• BTC:
1. Values ’a’ and ’b’: (8+8) bits
a ues a a d b (8 8) b s
2. Significance bits: k2 bits
Bitrate: R=(16+k2)/k2 =(1+16/k2) bit/pel
Bitrate: R=(16+k2)/k2 =(1+16/k2) bit/pel
Example: k=4: R=(1+16/42) = 2 bit/pel
• Bigger block → smaller bitrate R bigger distortion D
• Bigger block → smaller bitrate R, bigger distortion D
• Smaller block → bigger bitrate R, smaller distortion D
Trade-off between Rate and Distortion
98. Quadtree segmentation
Quadtree segmentation
1. Divide the image into blocks
g
of m1×m1 size.
2. FOR EACH BLOCK
IF (σ < σ ) THEN apply BTC
IF (σ < σ0) THEN apply BTC
ELSE divide into four
subblocks: m=m/2
3. REPEAT step 2 UNTIL
(σ < σ0) OR m=m2
here m2 is minimal block size
The hierarchy of the blocks is represented by a quadtree
The hierarchy of the blocks is represented by a quadtree
structure.
99. Example of BTC
Example of BTC
AMBTC HBTC-VQ
bpp = 2.00 bpp=8 bpp = 1.62
bpp 2.00 bpp 8 bpp 1.62
mse = 40.51 mse = 15.62
Block size: 2x2 .. 32x32
Block size: 4x4
100. JPEG
JPEG
• JPEG = Joint Photographic Experts Group
• JPEG = Joint Photographic Experts Group
• Lossy coding of continuous tone still images (color and
grayscale)
grayscale)
• Based on Discrete Cosine Transform (DCT):
0) Image is divided into block N×N
0) Image is divided into block N×N
1) The blocks are transformed with 2-D DCT
2) DCT coefficients are quantized
2) DCT coefficients are quantized
3) The quantized coefficients are encoded
101. JPEG: Encoding and Decoding
JPEG: Encoding and Decoding
Source FDCT Quantizer Entropy Compresse
Image Data
8x8 blocks
FDCT Quantizer py
Encoder
p
Image Dat
8x8 blocks
Table
Specifications
Table
Specifications
IDCT
Dequantizer
Entropy
Decoder
Reconstructed
Image Data
Compressed
Image Data
Table
S ifi ti
Table
S ifi ti
Specifications Specifications
102. Divide image into N×N blocks
Divide image into N×N blocks
8x8 block
Input image
103. 2-D DCT basis functions: N=8
2 D DCT basis functions: N 8
Low High
o
Low
High
Low
High High
8x8 block
High High
High
Low
105. 1-D DCT basis functions: N=8
1 D DCT basis functions: N 8
1.0
u=0
1.0
u=1
1.0
u=2
1.0
u=3
0.5
0
-0.5
0.5
0
-0.5
0.5
0
-0.5
0.5
0
-0.5
-1.0 -1.0 -1.0 -1.0
1.0
0 5
u=4
1.0
0 5
u=5
1.0
0 5
u=6
1.0
0 5
u=7
0.5
0
-0.5
-1.0
0.5
0
-0.5
-1.0
0.5
0
-0.5
-1.0
0.5
0
-0.5
-1.0
( ) ( ) ( )
∑
−
⎥
⎦
⎤
⎢
⎣
⎡ +
⋅
=
1
2
1
2
cos
N
j
N
k
j
k
C
k
x
π
α ( )
⎪
⎪
⎨
⎧ =
=
1
2
1
f
2
0
for
1
N
k
k
N
k
α
∑
=
⎥
⎦
⎢
⎣
0 2
k
j
N ⎪
⎩
−
= 1
,...,
2
,
1
for
2 N
k
N
106. Zig-zag ordering of DCT coefficients
Zig zag ordering of DCT coefficients
DC: Direct current
AC: Alternating current
Converting a 2-D matrix into a 1-D array, so that the
g y,
frequency (horizontal and vertical) increases in this order
and the coefficents variance are decreasing in this order.
107. Example of DCT for image block
Example of DCT for image block
Matlab: y=dct(x)
Matlab: y=dct(x)
108. Distribution of DCT coefficients
Distribution of DCT coefficients
DC coefficient AC coefficient
DC: uniformly distributed
AC: distribution resembles Laplacian pdf
109. Bit allocation for DCT coefficients
Bit allocation for DCT coefficients
• Lossy operation to reduce bit-rate
• Lossy operation to reduce bit rate
• Vector or Scalar Quantizer?
• Set of optimal scalar quantizers?
p q
• Set of scalar quantizers with fixed quantization tables
110. Bit allocation for DCT coefficients
Bit allocation for DCT coefficients
Minimize the total distortition D
⎭
⎬
⎫
⎩
⎨
⎧
= ∑ −
N
b
i
i
b
i
i
h
D 2
2
}
{
2
min σ
Minimize the total distortition D
⎭
⎩ =
i
bi
1
}
{
subject to B
b
N
=
∑
See Lecture 10
subject to B
b
i
i =
∑
=1
here bi is number of bits for coefficient yi,
[ ]
{ }3
3
1
)
(
1
∫ d
h
B is a given total number of bits,
[ ]
{ }
3
1
)
(
12
1
∫
= dx
x
p
h i
i
111. Optimal bit allocation for DCT coefficients
Optimal bit allocation for DCT coefficients
Solution of the optimization task with Lagrange multiplier
h
B
b i
i
2
log
1
log
1
+
+
=
σ
Solution of the optimization task with Lagrange multiplier
method:
Bitrate:
H
N
bi 2
2
2 log
2
log
2
+
+
=
θ
N
B
NH
D −
= 2
2
θ
Bitrate:
Distortion:
N
N
k
N
N
k h
H
1
1
1
1
2
2
; ⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
=
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
= ∏
∏
−
−
σ
θ
where
k
k
k
k
0
0
; ⎟
⎠
⎜
⎝
⎟
⎠
⎜
⎝
∏
∏ =
=
112. Minimal distortion
Minimal distortion
N
B
NH
D −
2
2
θ
Distortion:
N
N 1
1
2
2
⎟
⎟
⎞
⎜
⎜
⎛
∏
−
θ
N
B
NH
D = 2
2
θ
Distortion:
h
k
k
0
2
2
⎟
⎟
⎠
⎜
⎜
⎝
= ∏
=
σ
θ
where
2
Distortion D is minimal, if θ2 is minimal.
Product of diagonal elements is greater than or equal
to the determinant of the (positive semidefinite) matrix.
to the determinant of the (positive semidefinite) matrix.
Equality is attained iff the matrix is diagonal.
KLT provides minimum of θ2 (and minimum of distortion D)
among other transforms!
114. Quantization of DCT coefficients: Example
Quantization of DCT coefficients: Example
Ordered DCT coefficients: 15,0,-2,-1,-1,-1,0,0,-1,-1, 54{’0’}.
117. Encoding of quantized DCT coefficients
Encoding of quantized DCT coefficients
• Ordered data: 15,0,-2,-1,-1,-1,0,0,-1,-1, 54{’0’}.
• Encoding:
g
♦ DC: ?
♦ AC: ?
118. Encoding of quantized DCT coefficients
Encoding of quantized DCT coefficients
• DC coefficient for the current block is predicted of
that of the previous block, and error is coded using
Huffman coding
• AC coefficients:
(a) Huffman code, arithmetic code for non-zeroes
( ) ,
(b) run-length encoding: (number of ’0’s, non-’0’-symbol)
119. Performance of JPEG algorithm
Performance of JPEG algorithm
8 bpp 0.6 bpp
0.37 bpp 0.22 bpp
121. RGB vs YCbCr
RGB vs YCbCr
• 24 bits RGB representation: apply DCT for each
p pp y
component separately
- does not make use of the correlation between color
components
components
- does not make use of lowe sensitivity of the human eyes
to chrominance component
to c o a ce co po e t
• Convert RGB into a YCbCr representation: Y is luminance,
and Yb, Yc are chrominance
,
- Downsample the two chrominance components
122. RGB ⇔ YCbCr conversion
RGB ⇔ YCbCr conversion
Luminance Y and two chrominances Cb and Cr
Luminance Y and two chrominances Cb and Cr
124. Quantization of DCT coefficients
Quantization of DCT coefficients
For chrominance
For illuminance For chrominance
For illuminance
125. Performance of JPEG algorithm
Performance of JPEG algorithm
• Grayscale 8 bits images:
• Grayscale 8 bits images:
- 0.5 bpp: excellent quality
• Color 24 bits images:
- 0.25-0.50 bpp: moderate to good
- 0 50-0 75 bpp: good to very good
0.50 0.75 bpp: good to very good
- 0.75-1.00 bpp: excellent, sufficient for most applications
- 1.00-2.00 bpp: indistiniguishable from original
127. JPEG 2000
• JPEG 2000 is a new still image compression standard
g p
• ”One-for-all” image codec:
* Different image types: binary, grey-scale, color,
multi-component
* Different applications: natural images, scientific,
medical remote sensing text, rendered graphics
* Different imaging models: client/server, consumer
electronics, image library archival, limited buffer
and resources.
128. History
History
• Call for Contributions in 1996
Call for Contributions in 1996
• The 1st Committee Draft (CD) Dec. 1999
• Final Committee Draft (FCD) in March 2000
( )
• Accepted as Draft International Standard in Aug. 2000
• Published as ISO Standard in Jan. 2002
129. Key components
Key components
• Transform
– Wavelet
– Wavelet packet
Wavelet in tiles
– Wavelet in tiles
• Quantization
– Scalar
• Entropy coding
– (EBCOT) code once, truncate anywhere
Rate distortion optimization
– Rate-distortion optimization
– Context modeling
– Optimized coding order
p g
133. Quantizer with dead zone
Quantizer with dead zone
2δ
0
δ
⎩
⎨
⎧ +
=
⎥
⎥
⎢
⎢
=
s
n
m
s
n
m
1
0
n]
[m,
,
]
,
[
]
,
[ χ
δ
ν
⎩
⎨
−
⎥
⎦
⎢
⎣ s
1
]
[ ,
,
]
,
[ χ
δ
Quantized Magnitude Sign
135. EBCOT
EBCOT
• Key features of EBCOT: Embedded Block Coding with
Key features of EBCOT: Embedded Block Coding with
Optimized Truncation
– Low memory requirement in coding and decoding
– Easy rate control
– High compression performance
– Region of interest (ROI) access
– Error resilience
Modest complexity
– Modest complexity
136. Block structure in EBCOT
Block structure in EBCOT
Encode each block separately &
record the bitstream of each block.
record the bitstream of each block.
Block size is 64x64.
138. Quantizer with dead zone
Quantizer with dead zone
2δ
0
δ
⎩
⎨
⎧ +
=
⎥
⎥
⎢
⎢
=
s
n
m
s
n
m
1
0
n]
[m,
,
]
,
[
]
,
[ χ
δ
ν
⎩
⎨
−
⎥
⎦
⎢
⎣ s
1
]
[ ,
,
]
,
[ χ
δ
Quantized Magnitude Sign
139. ROI: Region of interest
ROI: Region of interest
Scale-down the coefficients outside the ROI so those are
in lowerer bit-planes.
Decoded or refined ROI bits before the rest of the image.
140. ROI: Region of interest
ROI: Region of interest
• Sequence based code
q
– ROI coefficients are coded as independent sequences
– Allows random access to ROI without fully decoding
– Can specify exact quality/bitrate for ROI and the BG
• Scaling based mode:
S l ROI k ffi i t (d d l d )
– Scale ROI mask coefficients up (decoder scales down)
– During encoding the ROI mask coefficients are found
significant at early stages of the coding
significant at early stages of the coding
– ROI always coded with better quality than BG
– Can't specify rate for BG and ROI