0% found this document useful (0 votes)
200 views

Lecture 3 Compressiond Algo

This document summarizes key concepts from Chapter 7 of a multimedia fundamentals textbook on lossless compression algorithms. It introduces different types of compression including lossless and lossy, and discusses entropy and information theory concepts important to understanding compression techniques. Specific algorithms covered include run-length coding, variable-length coding using Shannon-Fano and Huffman algorithms. Examples are provided to illustrate how to apply these algorithms to encode text strings and compute compression ratios.

Uploaded by

Kanishka Gopal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
200 views

Lecture 3 Compressiond Algo

This document summarizes key concepts from Chapter 7 of a multimedia fundamentals textbook on lossless compression algorithms. It introduces different types of compression including lossless and lossy, and discusses entropy and information theory concepts important to understanding compression techniques. Specific algorithms covered include run-length coding, variable-length coding using Shannon-Fano and Huffman algorithms. Examples are provided to illustrate how to apply these algorithms to encode text strings and compute compression ratios.

Uploaded by

Kanishka Gopal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

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 Li & Drew

Fundamentals of Multimedia, Chapter 7

Introduction: What is compression?


Data compression is the representation of the source in digital form with as few bits as possible while maintaining an acceptable loss in fidelity. fidelity. Source can be data, still images, speech, audio, video or whatever signal needs to be stored & transmitted. transmitted.

Fundamentals of Multimedia, Chapter 7

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

Fundamentals of Multimedia, Chapter 7

Introduction: Why compression?


Digital representation of analog signals

requires huge storage


High quality audio signal require 1.5 megabits/sec A low resolution movie (30 frames per second, 640 x 580 pixels per frame, 24 bits per pixel) requires
95 gigabytes per hour

It is challenging transferring such files

through the available limited network bandwidth. bandwidth.

Fundamentals of Multimedia, Chapter 7

7.1 Introduction
Compression: the process of coding that will effectively reduce the total number of bits needed to represent certain information.

Fig. 7.1: A General Data Compression Scheme.


5 Li & Drew

Fundamentals of Multimedia, Chapter 7

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)

B0 number of bits before compression B1 number of bits after compression


6 Li & Drew

Fundamentals of Multimedia, Chapter 7

Lossy and Lossless compression


Lossless compression: the decompressed data is exactly the same as the original one Example: text compression, medical images, etc Lossy compression: the decompressed data is not exactly the same as the original data. Some details have been lost although the loss is often acceptable. Example: image compression (JPEG), video compression (MPEG), audio compression (MP3)

Fundamentals of Multimedia, Chapter 7

7.2 Basics of Information Theory


The entropy of an information source with alphabet S = {s1, s2, . . . , sn} is:

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

Fundamentals of Multimedia, Chapter 7

Distribution of Gray-Level Intensities

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

Fundamentals of Multimedia, Chapter 7

Entropy and Code Length


As can be seen in Eq. (7.3): the entropy is a weighted-sum 1 of terms log 2 pi ; 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.,

l
produced by the encoder.

(7.5)

l - the average length (measured in bits) of the codewords

10

Li & Drew

Fundamentals of Multimedia, Chapter 7

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

Li & Drew

Fundamentals of Multimedia, Chapter 7

Run-Length Encoding Algorithm


Run Length Encoding or simply RLE is the simplest of the data compression algorithms. The consecutive sequences of symbols are identified as runs and the others are identified as non runs in this algorithm. It checks whether there are any repeating symbols or not, and is based on those redundancies and their lengths. Consecutive recurrent symbols are identified as runs and all the other sequences are considered as non-runs. For an example, the text ABABBBBC is considered as a source to compress, then the first 3 letters are considered as a non-run with length 3, and the next 4 letters are considered as a run with length 4 since there is a repetition of symbol B.

Fundamentals of Multimedia, Chapter 7

RLE Examples
Compress the following ABBBBBBBBBCDEEEEF Ans: A 8*BCD4*EF Compress the following aaaabbcdeeeeefghhhij Ans:4*a2*bcd5*efg3*hij

13

Li & Drew

Fundamentals of Multimedia, Chapter 7

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
Symbol Count H 1 E 1 L 2 O 1

Frequency count of the symbols in HELLO.


14 Li & Drew

Fundamentals of Multimedia, Chapter 7

Fig. 7.3: Coding Tree for HELLO by Shannon-Fano.


15 Li & Drew

Fundamentals of Multimedia, Chapter 7

Table 7.1: Result of Performing Shannon-Fano on HELLO


Symbol L H E O Count 2 1 1 1 Log2 p i 1.32 2.32 2.32 2.32
1

Code 0 10 110 111 TOTAL # of bits:

# 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

Fundamentals of Multimedia, Chapter 7

Fig. 7.4 Another coding tree for HELLO by Shannon-Fano.

17

Li & Drew

Fundamentals of Multimedia, Chapter 7

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

Code 00 01 10 11 TOTAL # of bits:

# of bits used 4 2 2 2 10

18

Li & Drew

Fundamentals of Multimedia, Chapter 7

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.

Fundamentals of Multimedia, Chapter 7

Example 2: Answer

Fundamentals of Multimedia, Chapter 7

Example 2: Answer
C 000 M 0010 P 0011 R 010 E 0110 I 0111 N 100 O 101 S 11

COMPRESSION RATIO = 37/12*8 = 1:2.59

Fundamentals of Multimedia, Chapter 7

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.

Fundamentals of Multimedia, Chapter 7

Example 3: Answer 1

Fundamentals of Multimedia, Chapter 7

Example 3: Answer 2

Fundamentals of Multimedia, Chapter 7

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

Fundamentals of Multimedia, Chapter 7

Fig. 7.5: Coding Tree for HELLO using the Huffman Algorithm.

26

Li & Drew

Fundamentals of Multimedia, Chapter 7

Huffman Coding (contd)


In Fig. 7.5, 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: After iteration (a): After iteration (b): After iteration (c): L L L P3
27

H P1 P2

E H

Li & Drew

Fundamentals of Multimedia, Chapter 7

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. Combined with Eq. (7.5), we have:

l 1
28

(7.6)
Li & Drew

Fundamentals of Multimedia, Chapter 7

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

Fundamentals of Multimedia, Chapter 7

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.

Fundamentals of Multimedia, Chapter 7

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.

Fundamentals of Multimedia, Chapter 7

Extended Huffman Coding


Motivation: All codewords in Huffman coding have integer bit 1 lengths. It is wasteful when pi is very large and hence log 2 p is close i 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:

S (k )

k symbols {s1s1 s1 , s1s1 s2 , , s1s1 sn , s1s1 s2 s1 , , sn sn sn }.

the size of the new alphabet S(k) is nk.

32

Li & Drew

Fundamentals of Multimedia, Chapter 7

Extended Huffman Coding (contd)


It can be proven that the average # of bits for each symbol is:
l 1 k

(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

Fundamentals of Multimedia, Chapter 7

Adaptive Huffman Coding


Adaptive Huffman Coding: statistics are gathered and updated dynamically as the data stream arrives.
ENCODER ------Initial_code(); DECODER ------Initial_code();

while not EOF while not EOF { { get(c); decode(c); encode(c); output(c); update_tree(c); update_tree(c); } }

34

Li & Drew

Fundamentals of Multimedia, Chapter 7

Adaptive Huffman Coding (Contd)


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.
35 Li & Drew

Fundamentals of Multimedia, Chapter 7

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.

36

Li & Drew

Fundamentals of Multimedia, Chapter 7

Fig. 7.6: Node Swapping for Updating an Adaptive Huffman Tree

37

Li & Drew

Fundamentals of Multimedia, Chapter 7

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) in Fig. 7.7.
38 Li & Drew

Fundamentals of Multimedia, Chapter 7

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

Fundamentals of Multimedia, Chapter 7

Fig. 7.7 Adaptive Huffman tree for AADCCDD.


40 Li & Drew

Fundamentals of Multimedia, Chapter 7

Fig. 7.7 (contd) Adaptive Huffman tree for AADCCDD.


41 Li & Drew

Fundamentals of Multimedia, Chapter 7

Table 7.4 Sequence of symbols and codes sent to the decoder


Symbol Code NEW 0 A 00001 A NEW 1 0 D 00100 NEW 00 C C D D 00011 001 101 101

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

Fundamentals of Multimedia, Chapter 7

7.5 Dictionary-based Coding


LZWuses fixed-length codewords to represent variablelength strings of symbols/characters that commonly occur together, e.g., words in English text. the LZW encoder and decoder build up the same dictionary dynamically while receiving the data. LZW places longer and longer repeated entries into a dictionary, and then emits the code for an element, rather than the string itself, if the element has already been placed in the dictionary.
43 Li & Drew

Fundamentals of Multimedia, Chapter 7

ALGORITHM 7.2 - LZW Compression


BEGIN s = next input character; while not EOF { c = next input character; if s + c exists in the dictionary s = s + c; else { output the code for s; add string s + c to the dictionary with a new code; s = c; } } output the code for s; END

44

Li & Drew

Fundamentals of Multimedia, Chapter 7

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

Fundamentals of Multimedia, Chapter 7

ALGORITHM 7.3 LZW Decompression (simple version)


BEGIN s = NIL; while not EOF { k = next input code; entry = dictionary entry for k; output entry; if (s != NIL) add string s + entry[0] to dictionary with a new code; s = entry; } END

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

Fundamentals of Multimedia, Chapter 7

The LZW decompression algorithm then works as follows:


S K Entry/output Code 1 2 3 NIL A B AB BA B C AB ABB A 1 2 4 5 2 3 4 6 1 EOF A B AB BA B C AB ABB A 4 5 6 7 8 9 10 11 String A B C AB BA ABB BAB BC CA ABA ABBA

Apparently, the output string is ABABBABCABABBA, a truly lossless result!


48 Li & Drew

Fundamentals of Multimedia, Chapter 7

ALGORITHM 7.4 LZW Decompression (modified)


BEGIN
s = NIL; while not EOF { k = next input code; entry = dictionary entry for k; /* exception handler */ if (entry == NULL) entry = s + s[0]; output entry; if (s != NIL) add string s + entry[0] to dictionary with a new code; s = entry; }

END
49 Li & Drew

Fundamentals of Multimedia, Chapter 7

LZW Coding (contd)


In real applications, the code length l is kept in the range of [l0, lmax]. The dictionary initially has a size of 2l0. When it is filled up, the code length will be increased by 1; this is allowed to repeat until l = lmax. When lmax is reached and the dictionary is filled up, it needs to be flushed (as in Unix compress, or to have the LRU (least recently used) entries removed.
50 Li & Drew

Fundamentals of Multimedia, Chapter 7

7.6 Arithmetic Coding


Arithmetic coding is a more modern coding method that usually out-performs Huffman coding. Huffman coding assigns each symbol a codeword which has an integral bit length. Arithmetic coding can treat the whole message as one unit. A message is represented by a half-open interval [a, b) where a and b are real numbers between 0 and 1. Initially, the interval is [0, 1). When the message becomes longer, the length of the interval shortens and the number of bits needed to represent the interval increases.

51

Li & Drew

Fundamentals of Multimedia, Chapter 7

ALGORITHM 7.5 Arithmetic Coding Encoder


BEGIN
low = 0.0; high = 1.0; range = 1.0; while (symbol != terminator) { get (symbol); low = low + range * Range_low(symbol); high = low + range * Range_high(symbol); range = high - low; } output a code so that low <= code < high;

END
52 Li & Drew

Fundamentals of Multimedia, Chapter 7

Example: Encoding in Arithmetic Coding


Symbol A B C D E F G Probability 0.2 0.1 0.2 0.05 0.3 0.05 0.1 Range [0, 0.2) [0.2, 0.3) [0.3, 0.5) [0.5, 0.55) [0.55, 0.85) [0.85, 0.9) [0.9, 1.0)

(a) Probability distribution of symbols. Fig. 7.8: Arithmetic Coding: Encode Symbols CAEE$
53 Li & Drew

Fundamentals of Multimedia, Chapter 7

Fig. 7.8(b) Graphical display of shrinking ranges.


54 Li & Drew

Fundamentals of Multimedia, Chapter 7

Example: Encoding in Arithmetic Coding


Symbol C A E E $ Low 0 0.3 0.30 0.322 0.3286 0.33184 High 1.0 0.5 0.34 0.334 0.3322 0.33220 Range 1.0 0.2 0.04 0.012 0.0036 0.00036

(c) New low, high, and range generated. Fig. 7.8 (contd): Arithmetic Coding: Encode Symbols CAEE$
55 Li & Drew

Fundamentals of Multimedia, Chapter 7

PROCEDURE 7.2 Generating Codeword for Encoder


BEGIN
code = 0; k = 1; while (value(code) < low) { assign 1 to the kth binary fraction bit if (value(code) > high) replace the kth bit by 0 k = k + 1; }

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

Fundamentals of Multimedia, Chapter 7

ALGORITHM 7.6 Arithmetic Coding Decoder


BEGIN
get binary code and convert to decimal value = value(code); Do { find a symbol s so that Range_low(s) <= value < Range_high(s); output s; low = Rang_low(s); high = Range_high(s); range = high - low; value = [value - low] / range; } Until symbol s is a terminator

END
57 Li & Drew

Fundamentals of Multimedia, Chapter 7

Table 7.5 Arithmetic coding: decode symbols CAEE$

Value 0.33203125 0.16015625 0.80078125 0.8359375 0.953125

Output Symbol C A E E $

Low 0.3 0.0 0.55 0.55 0.9

High 0.5 0.2 0.85 0.85 1.0

Range 0.2 0.2 0.3 0.3 0.1

58

Li & Drew

Fundamentals of Multimedia, Chapter 7

7.7 Lossless Image Compression


Approaches of Differential Coding of Images:
Given an original image I(x, y), using a simple difference operator we can define a difference image d(x, y) as follows: d(x, y) = I(x, y) I(x 1, y) (7.9) or use the discrete version of the 2-D Laplacian operator to define a difference image d(x, y) as d(x, y) = 4 I(x, y) I(x, y 1) I(x, y +1) I(x+1, y) I(x 1, y) (7.10)

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

Fundamentals of Multimedia, Chapter 7

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

Fundamentals of Multimedia, Chapter 7

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

Fundamentals of Multimedia, Chapter 7

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

Fundamentals of Multimedia, Chapter 7

Table 7.6: Predictors for Lossless JPEG


Predictor P1 P2 P3 P4 P5 P6 P7 Prediction A B C A+BC A + (B C) / 2 B + (A C) / 2 (A + B) / 2

63

Li & Drew

Fundamentals of Multimedia, Chapter 7

Table 7.7: Comparison with other lossless compression programs


Compression Program Lossless JPEG Optimal Lossless JPEG Compress (LZW) Gzip (LZ77) Gzip -9 (optimal LZ77) Pack(Huffman coding) Compression Ratio Lena 1.45 1.49 0.86 1.08 1.08 1.02 Football 1.54 1.67 1.24 1.36 1.36 1.12 F-18 2.29 2.71 2.21 3.10 3.13 1.19 Flowers 1.26 1.33 0.87 1.05 1.05 1.00

64

Li & Drew

Fundamentals of Multimedia, Chapter 7

7.8 Further Exploration


Text books: The Data Compression Book by M. Nelson Introduction to Data Compression by K. Sayood Web sites: Link to Further Exploration for Chapter 7 including: An excellent resource for data compression compiled by Mark Nelson. The Theory of Data Compression webpage. The FAQ for the comp.compression and comp.compression.research groups. A set of applets for lossless compression. A good introduction to Arithmetic coding Grayscale test images f-18.bmp, flowers.bmp, football.bmp, lena.bmp

65

Li & Drew

You might also like