Beruflich Dokumente
Kultur Dokumente
nk
lk nk = n lk = n lk p (ak ) = nLavg
k =0 k =0 n k =0
• Naturally, we need an encoding scheme with Lavg as small as
possible. From Shannon’s theorem, we know that Lavg ≥ H (z ) .
H (z ) ≤ Lavg < H (z ) + 1
Huffman Code
• The algorithm is best illustrated by means of an example.
• Given a source which generates one of six possible symbols
A = {a1 , a2 , , a6 } with corresponding probabilities
{0.1,0.4,0.06,0.1,0.04,0.3}.
• Arrange the symbols in descending order of their probability of
occurrence.
• Successively reduce the number of source symbols by replacing the
two symbols having least probability, with a “compound symbol.”
This way, the number of source symbols is reduced by one at each
stage.
• The compound symbol is placed at an appropriate location in the next
stage, so that the probabilities are again in descending order. Break
ties using any arbitrary but consistent rule.
• Code each reduced source starting with the smallest source and
working backwards.
Source Reduction:
Symbol Prob. 1 2 3 4
a2 0.4 0.4 0.4 0.4 0.6
a6 0.3 0.3 0.3 0.3 0.4
a1 0.1 0.1 0.2 0.3
a4 0.1 0.1 0.1
a3 0.06 0.1
a5 0.04
Code Assignment:
Symb. Prob. Code 1 2 3 4
a2 0.4 1 0.4 1 0.4 1 0.4 1 0.6 0
a6 0.3 00 0.3 00 0.3 00 0.3 00 0.4 1
a1 0.1 011 0.1 011 0.2 010 0.3 01
a4 0.1 0100 0.1 0100 0.1 011
a3 0.06 01010 0.1 0101
a5 0.04 01011
= 2.2 bits/symbol
H ( z ) = −∑ pi log( pi ) = −
0.4(log(0.4)) + 0.3(log(0.3)) + 0.1(log(0.1))
= 2.14 bits/symbol
• The resulting code is called a Huffman code. It has some interesting
properties:
The source symbols can be encoded (and decoded) one at time.
It is called a block code because each source symbol is mapped
into a fixed sequence of code symbols.
It is instantaneous because each codeword in a string of code
symbols can be decoded without referencing succeeding symbols.
It is uniquely decodable because any string of code symbols can
be decoded in only one way.
• Example: Given the encoded string, 010100111100, it can be
decoded as follows:
010100111100 → 010100111100 → a3 0111100 → a3a11100
→ a3a1a2 100 → a3a1a2 a2 00 → a3a1a2 a2 a6
Disadvantage:
• For a source with J symbols, we need J − 2 source reductions. This
can be computationally intensive for large J (ex. J = 256 for an
image with 256 graylevels).
Suboptimal approaches
Truncated Huffman coding:
• This is generated by encoding only the ψ most probable symbols, for
some integer ψ < J .
Bit-plane decomposition:
• Given a grayscale image with 2 m graylevels, each grayvalue can be
represented by m-bits, say (am −1 , am−2 , , a1 , a0 ) .
r = am −1 2 m−1 + am−2 2 m− 2 +
+ a1 21 + a0 20
, g1 , g 0 )
to represent a given grayvalue.
• The Gray code ( g m−1 , g m−2 , , g1 , g 0 ) can be obtained from
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
• Two approaches:
Start position and lengths of runs of 1s for each row is used:
(1,3)(7,2)(12,4)(17,2)(20,3)
(5,13)(19,4)
(1,3)(17,6)
H 0 = −∑ i =1 ai log(ai )
M
• Let be the entropy associated with
, aM ) and L0 = ∑ i =1 iai be the average length of runs of 0s.
M
(a1 , a2 ,
H RL =
(H 0 + H1 ) bits/run
(L0 + L1 ) symbols/run
• H RL provides an estimate of the average number of bits per pixel
required to code the run lengths in a binary image, using a variable-
length code.
• The concept of run-length can be extended to a variety of 2-D coding
procedures.
Runlength Example
0.9
0.8
Plot of a typical
0.7 row: (row # 152)
0.6
0.5
0.4
0.3
0.2
0.1
0
0 50 100 150 200 250 300
0.45
0.2
0.15
0.1
0.05
0
0 50 100 150 200 250 300
0.2
0.18
0.16
0.1
L1 = 20.3 pixels/run
H1 = 4.4 bits/run
0.08
0.06
0.04
0.02
0
0 50 100 150 200 250 300
H 0 + H1
H RL = = 0.28 bits/pixel
L0 + L1
Lossless Predictive Coding
• Does not require decomposition of grayscale image into bitplanes.
• Eliminate interpixel redundancy by extracting and coding only the
new information in each pixel.
• New information in a pixel is the difference between its actual and
predicted (based on “previous pixel values”) values.
Compressed
fn + en Image
Symbol
Input Encoder
Image −
fˆn
Nearest
Predictor integer
Prediction
error
en = f n − fˆn or f n = en + fˆn
∑α
m
fˆn = round f
i n −i
i =1
Prediction based on
previous pixel values
Symbol en + fn
Decoder Decompressed
Compressed + Image
Image
fˆn
Predictor
Example: 1-D first order linear predictor
• In each row, a pixel value is predicted based on the value of the pixel
to its left.
• The first element of each row (i.e., first column of image) is also
encoded (using, for example, a different Huffman code).
0.025
0.02
0.015
0.01
0.005
Entropy = 6.9
0
0 50 100 150 200 250 300
Distribution of prediction error values
0.2
0.18
0.16
0.14
0.12
0.1
0.08
0.06
Entropy = 4.4
0.04
0.02
0
-300 -200 -100 0 100 200 300