Lecture 3 Compressiond Algo
Lecture 3 Compressiond Algo
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 Li & Drew
Data cmpresion
Think of everyday situations where you perform data compression?
SMS (love = luv, there = zer, etc) Narrating a film to a friend Summarising a lecture for a friend who was absent Sending emails Compressing files using winrar/winzip
7.1 Introduction
Compression: the process of coding that will effectively reduce the total number of bits needed to represent certain information.
Introduction (contd)
If the compression and decompression processes induce no information loss, then the compression scheme is lossless; otherwise, it is lossy. Compression ratio:
B0 compression ratio B1
(7.1)
1 H ( S ) pi log 2 pi i 1
n
(7.2) (7.3)
pi log 2 pi
i 1
pi probability that symbol si will occur in S. indicates the amount of information ( selfinformation as defined by Shannon) contained in si, which corresponds to the number of bits needed to encode si.
log
8 Li & Drew
1 2 pi
Fig. 7.2 Histograms for Two Gray-level Images. Fig. 7.2(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 (7.4)
Fig. 7.2(b) shows the histogram of an image with two possible values. Its entropy is 0.92.
9 Li & Drew
l
produced by the encoder.
(7.5)
10
Li & Drew
11
Li & Drew
RLE Examples
Compress the following ABBBBBBBBBCDEEEEF Ans: A 8*BCD4*EF Compress the following aaaabbcdeeeeefghhhij Ans:4*a2*bcd5*efg3*hij
13
Li & Drew
# of bits used 1 2 3 3 10
Calculate the entropy for the Above ? Result 0.4 x 1.32 +0.2 x 2.32 +0.2 x 2.32 + 0.2 x 2.32 = 1.92
16 Li & Drew
17
Li & Drew
Table 7.2: Another Result of Performing Shannon-Fano on HELLO (see Fig. 7.4)
Symbol L H E O Count 2 1 1 1 Log2 1 1.32 2.32 2.32 2.32
pi
# of bits used 4 2 2 2 10
18
Li & Drew
Example 2: Shannon-Fano
Encode the word COMPRESSIONS using the Shannon-Fano algorithm. You should start by creating the binary tree and then assign codewords to each unique symbols. You should also compute the compression ratio.
Example 2: Answer
Example 2: Answer
C 000 M 0010 P 0011 R 010 E 0110 I 0111 N 100 O 101 S 11
Example 3: Shannon-Fano
Encode the word ABRACADABRA using the Shannon-Fano algorithm. You should start by creating the binary tree and then assign codewords to each unique symbols. You should also compute the compression ratio.
Example 3: Answer 1
Example 3: Answer 2
Huffman Coding
ALGORITHM 7.1 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 childrens 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.
25
Li & Drew
Fig. 7.5: Coding Tree for HELLO using the Huffman Algorithm.
26
Li & Drew
H P1 P2
E H
Li & Drew
l 1
28
(7.6)
Li & Drew
Examples
Consider a Text String containing a set of characters and their frequency count as follows: A:(15) B: (7) C: (6) D: (6)E: (5) Compress it using Shannon Fano and Huffman coding. Is there any difference? Ans: Shannon Fano 89bits Huffman 87bits
29
Li & Drew
Example 2: Huffman
Encode the word COMPRESSIONS using the Shannon-Fano algorithm. You should start by creating the binary tree and then assign codewords to each unique symbols. You should also compute the compression ratio.
Example 3: Huffman
Encode the word ABRACADABRA using the Shannon-Fano algorithm. You should start by creating the binary tree and then assign codewords to each unique symbols. You should also compute the compression ratio.
S (k )
32
Li & Drew
(7.7)
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.
33 Li & Drew
while not EOF while not EOF { { get(c); decode(c); encode(c); output(c); update_tree(c); update_tree(c); } }
34
Li & Drew
36
Li & Drew
37
Li & Drew
Table 7.3: Initial code assignment for AADCCDD using adaptive Huffman coding.
Initial Code --------------------NEW: 0 A: 00001 B: 00010 C: 00011 D: 00100 .. .. ..
39
Li & Drew
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 books web site provides a Java applet for adaptive Huffman coding.
42 Li & Drew
44
Li & Drew
Example 7.2 LZW compression for string ABABBABCABABBA Lets start with a very simple dictionary (also referred to as a string table), initially containing only 3 characters, with codes as follows:
Code 1 2 3 String A B C
Now if the input string is ABABBABCABABBA, the LZW compression algorithm works as follows:
45 Li & Drew
Fundamentals of Multimedia, Chapter 7 S C Output Code 1 2 3 A B A AB B BA B C A AB A AB ABB A B A B B A B C A B A B B A EOF 1 2 4 5 2 3 4 4 5 6 7 8 9 10 String A B C AB BA ABB BAB BC CA ABA
6 1
11
ABBA
The output codes are: 1 2 4 5 2 3 4 6 1. Instead of sending 14 characters, only 9 codes need to be sent (compression ratio = 14/9 = 1.56).
46 Li & Drew
Example 7.3: LZW decompression for string ABABBABCABABBA. Input codes to the decoder are 1 2 4 5 2 3 4 6 1. The initial string table is identical to what is used by the encoder.
47 Li & Drew
END
49 Li & Drew
51
Li & Drew
END
52 Li & Drew
(a) Probability distribution of symbols. Fig. 7.8: Arithmetic Coding: Encode Symbols CAEE$
53 Li & Drew
(c) New low, high, and range generated. Fig. 7.8 (contd): Arithmetic Coding: Encode Symbols CAEE$
55 Li & Drew
END The final step in Arithmetic encoding calls for the generation of a number that falls within the range [low, high). The above algorithm will ensure that the shortest binary codeword is found.
56 Li & Drew
END
57 Li & Drew
Output Symbol C A E E $
58
Li & Drew
Due to spatial redundancy existed in normal images I, the difference image d will have a narrower histogram and hence a smaller entropy, as shown in Fig. 7.9.
59 Li & Drew
Fig. 7.9: Distributions for Original versus Derivative Images. (a,b): Original gray-level image and its partial derivative image; (c,d): Histograms for original and derivative images. (This figure uses a commonly employed image called Barb.)
60 Li & Drew
Lossless JPEG
Lossless JPEG: A special case of the JPEG image compression. The Predictive method 1. Forming a differential prediction: A predictor combines the values of up to three neighboring pixels as the predicted value for the current pixel, indicated by X in Fig. 7.10. The predictor can use any one of the seven schemes listed in Table 7.6. 2. Encoding: The encoder compares the prediction with the actual pixel value at the position X and encodes the difference using one of the lossless compression techniques we have discussed, e.g., the Huffman coding scheme.
61
Li & Drew
Fig. 7.10: Neighboring Pixels for Predictors in Lossless JPEG. Note: Any of A, B, or C has already been decoded before it is used in the predictor, on the decoder side of an encode-decode cycle.
62 Li & Drew
63
Li & Drew
64
Li & Drew
65
Li & Drew