chapter 7 lossless compression algorithmscourse.sdu.edu.cn/download/f359a9d9-e8ed-4ed5-a5b5... ·...
TRANSCRIPT
Chapter 7 Lossless Compression Algorithms
7.1 Introduction 7.2 Basics of Information Theory 7.3 Run-Length Coding 7.4 Variable-Length Coding (VLC) 7.5 Dictionary-based Coding 7.6 Arithmetic Coding 7.7 Lossless Image Compression 7.8 Further Exploration
1
7.1 Introduction • Compression: the process of coding that will
effectively reduce the total number of bits needed to represent certain information.
A General Data Compression Scheme.
2
Overview of Data Compression In the past storage space was limited in computers, and bandwidth for data transfer was also not sufficient. People needed to find some approaches to represent data in fewer bits or bytes, and so compression methods were born.
Introduction (cont’d) • If the compression and decompression processes
induce no information loss, then the compression scheme is lossless; otherwise, it is lossy.
• Compression ratio:
B0 – number of bits before compression B1 – number of bits after compression
3
0
1
B
compressionratioB
Lossless And Lossy Compression
• Lossless compression is essential in applications such as text file compression.
• Lossy compression is acceptable in many imaging applications. In video transmission, a slight loss in the transmitted video is not noticed by the human eye.
• Lossy compression proves effective when applied to graphics images and digitized voice.
4
7.2 Basics of Information Theory •Information Theory uses the term entropy as a measure of
how much information is encoded in a message. • The higher the entropy of a message, the more information
it contains. • To determine the information content of a message in bits,
we express the entropy using the base 2 logarithm:
Number of bits = - Log2 (probability) • The entropy of an entire message is simply the sum of the
entropy of all individual symbols.
5
Basics of Information Theory • The entropy η of an information source with alphabet S = {s1,
s2, . . . , sn} is:
pi – probability that symbol si will occur in S. – indicates the amount of information (self-
information as defined by Shannon) contained in si, which corresponds to the number of bits needed to encode si.
6
2
1
1( ) log
n
i
i i
H S pp
2
1
logn
i i
i
p p
1log2 pi
Example
7
Note the sum of the probabilities adds up to 1.00
entropy
8
If we have a symbol set {A,B,C,D,E}
where the symbol occurance frequencies are:
A = 0.5 B = 0.2 C = 0.1 D = 0.1 E = 0.1
The average minimum number of bits needed to represent a symbol is
H(X) = -[(0.5log20.5 + 0.2log20.2 + (0.1log20.1)*3)]
= -[-0.5 + (-0.46438) + (-0.9965)]
= -[-1.9] H(X) = 1.9 Rounding up, we get 2 bits/per symbol.
To represent a ten character string AAAAABBCDE would require 20 bits if the
string were encoded optimally.
Such an optimal encoding would allocate fewer bits for the frequency occuring
symbols (e.g., A and B) and long bit sequences for the more infrequent symbols
(C,D,E).
Distribution of Gray-Level Intensities
Histograms for Two Gray-level Images. • Fig. (a) shows the histogram of an image with uniform distribution of gray-
level intensities, i.e., ∀i pi = 1/256. Hence, the entropy of this image is: log2256 = 8
• Fig. (b) shows the histogram of an image with two possible values. Its entropy is 0.92.
9
Entropy and Code Length • Tentropy η is a weighted-sum of terms ; hence it
represents the average amount of information contained per symbol in the source S.
• The entropy η specifies the lower bound for the average
number of bits to code each symbol in S, i.e., - the average length (measured in bits) of the codewords
produced by the encoder.
10
1log2 pi
l
l
7.3 Run-Length Coding • Memoryless Source: an information source that is
independently distributed. Namely, the value of the current symbol does not depend on the values of the previously appeared symbols.
• Instead of assuming memoryless source, Run-Length Coding
(RLC) exploits memory present in the information source. • Rationale for RLC: if the information source has the
property that symbols tend to form continuous groups, then such symbol and the length of the group can be coded.
11
Run-Length Coding • Run length encoding (RLE) is a simple technique to compress
digital data by representing successive runs of the same value in the data as the count followed by the value, rather than the original run of values.
• This encoding method is frequently applied to images.
Consider the following bitmap "picture":
RLE: 5*0 , 5*2 , 1*4 , 3*6 , 3*0 , 3*6
12
Example
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
12W1B12W3B24W1B14W
13
RLC
14
JPEG中的Zigzag(RLC)
15
1 0 1 0 0 1
1 1 0 0 0 0
1 0 0 0 0 0
0 0 1 0 0 0
1 0 1 0 0 0
0 1 0 0 0 0
I
(1,1)(1,0)(4,1)(4,0)(1,1)(4,0)(1,1)(2,0)(1,1)(2,0)(2,1)(13,0)
16
17
18
7.4 Variable-Length Coding (VLC) Shannon-Fano Algorithm — a top-down approach 1. Sort the symbols according to the frequency count of their
occurrences.
2. Recursively divide the symbols into two parts, each with approximately the same number of counts, until all parts contain only one symbol.
An Example: coding of “HELLO”
Frequency count of the symbols in ”HELLO”.
19
Symbol H E L O
Count 1 1 2 1
Coding Tree for HELLO by Shannon-Fano.
20
Result of Performing Shannon-Fano on HELLO
21
Symbol Count Log2 Code # of bits used
L 2 1.32 0 1
H 1 2.32 10 2
E 1 2.32 110 3
O 1 2.32 111 3
TOTAL # of bits: 10
1pi
Another coding tree for HELLO by Shannon-Fano.
22
Another Result of Performing Shannon-Fano on HELLO
Symbol Count Log2 Code # of bits used
L 2 1.32 00 4
H 1 2.32 01 2
E 1 2.32 10 2
O 1 2.32 11 2
TOTAL # of bits: 10
1pi
Huffman Coding Huffman Coding Algorithm— a bottom-up approach 1. Initialization: Put all symbols on a list sorted according to their frequency
counts.
2. Repeat until the list has only one symbol left: (1) From the list pick two symbols with the lowest frequency counts. Form a Huffman
subtree that has these two symbols as child nodes and create a parent node.
(2) Assign the sum of the children’s frequency counts to the parent and insert it into the list such that the order is maintained.
(3) Delete the children from the list.
3. Assign a codeword for each leaf based on the path from the root.
23
Coding Tree for “HELLO” using the Huffman Algorithm.
24
Huffman Coding (cont’d)
new symbols P1, P2, P3 are created to refer to the parent nodes in the Huffman coding tree. The contents in the list are illustrated below:
After initialization: L H E O
After iteration (a): L P1 H
After iteration (b): L P2
After iteration (c): P3
25
Another Example
26
Li & Drew 27
Properties of Huffman Coding 1. Unique Prefix Property: No Huffman code is a prefix of any other Huffman
code - precludes any ambiguity in decoding.
2. Optimality: minimum redundancy code - proved optimal for a given data model (i.e., a given, accurate, probability distribution):
• The two least frequent symbols will have the same length for their
Huffman codes, differing only at the last bit. • Symbols that occur more frequently will have shorter Huffman codes
than symbols that occur less frequently. • The average code length for an information source S is strictly less than
η + 1.
28
1l
Extended Huffman Coding • Motivation: All codewords in Huffman coding have integer bit
lengths. It is wasteful when pi is very large and hence is close to 0.
Why not group several symbols together and assign a single
codeword to the group as a whole? • Extended Alphabet: For alphabet S = {s1, s2, . . . , sn}, if k symbols are
grouped together, then the extended alphabet is: — the size of the new alphabet S(k) is nk.
29
1log2 pi
( )
1 1 1 1 1 2 1 1 1 1 2 1{ , , , , , , }.
k symbols
k
n n n nS s s s s s s s s s s s s s s s s
Extended Huffman Coding (cont’d) • It can be proven that the average # of bits for each
symbol is:
An improvement over the original Huffman coding, but not much.
• Problem: If k is relatively large (e.g., k ≥ 3), then for
most practical applications where n ≫ 1, nk implies a huge symbol table — impractical.
30
1lk
Adaptive Huffman Coding • Adaptive Huffman Coding: statistics are gathered and updated
dynamically as the data stream arrives.
31
ENCODER
-------
Initial_code();
while not EOF
{
get(c);
encode(c);
update_tree(c);
}
DECODER
-------
Initial_code();
while not EOF
{
decode(c);
output(c);
update_tree(c);
}
Adaptive Huffman Coding (Cont’d) • Initial_code assigns symbols with some initially agreed upon codes,
without any prior knowledge of the frequency counts. • update_tree constructs an Adaptive Huffman tree. It basically does two things: (a) increments the frequency counts for the symbols (including any
new ones). (b) updates the configuration of the tree. • The encoder and decoder must use exactly the same initial_code and
update_tree routines.
32
Notes on Adaptive Huffman Tree Updating
• Nodes are numbered in order from left to right, bottom to top. The numbers in parentheses indicates the count.
• The tree must always maintain its sibling property, i.e., all nodes
(internal and leaf) are arranged in the order of increasing counts. If the sibling property is about to be violated, a swap procedure is
invoked to update the tree by rearranging the nodes. • When a swap is necessary, the farthest node with count N is
swapped with the node whose count has just been increased to N +1. • The update of the tree sometimes requires more than one swap.
33
Node Swapping for Updating an Adaptive Huffman Tree
34
Another Example: Adaptive Huffman Coding
• This is to clearly illustrate more implementation details. We show exactly what bits are sent, as opposed to simply stating how the tree is updated.
• An additional rule: if any character/symbol is to be sent the first time, it must be preceded by a special symbol, NEW. The initial code for NEW is 0. The count for NEW is always kept as 0 (the count is
never increased); hence it is always denoted as NEW:(0).
35
Table: Initial code assignment for AADCCDD using adaptive Huffman coding.
36
Initial Code --------------------- NEW: 0 A: 00001 B: 00010 C: 00011 D: 00100
. .
. .
. .
Fig. Adaptive Huffman tree for AADCCDD.
37
Fig. (cont’d) Adaptive Huffman tree for AADCCDD.
38
Table: Sequence of symbols and codes sent to the decoder
39
• It is important to emphasize that the code for a particular symbol changes during the adaptive Huffman coding process.
• For example, after AADCCDD, when the
character D overtakes A as the most frequent symbol, its code changes from 101 to 0.
• The “Squeeze Page” on this book’s web site provides
a Java applet for adaptive Huffman coding.
Symbol NEW A A NEW D NEW C C D D
Code 0 00001 1 0 00100 00 00011 001 101 101
Ex 1 Suppose eight characters have a distribution
A:(1), B:(1), C:(1), D:(2), E:(3), F:(5), G:(5), H:(10).
Draw a Huffman tree for this distribution.
Ex 2 (a) What is the entropy η of the image below, where
numbers (0, 20, 50, 99) denote the gray-level intensities?
(b) Show how to construct the Huffman tree to encode the above four intensity values in this image. Show the resulting code for each intensity value.
(c) What is the average number of bits needed for each pixel using your Huffman code? How does it compare to η?
η = 1.75
Average number of bits = 1.75
This happens to be identical to η —it only happens when all probabilities are 2−k where k is an integer. Otherwise, this number will be larger than η .
Ex 6
What are the advantages of Adaptive Huffman Coding compared to the original Huffman Coding algorithm?
Like any other adaptive compression algorithms,
1. It is more dynamic, therefore offers better compression
2. It works even when prior statistics of the data distribution is unavailable as it is in most multimedia applications.
3. It also saves the overhead since no symbol table needs to be transmitted.