830 likes | 1.05k Views
Lossy Compression. 15-211 Fundamental Data Structures and Algorithms. Peter Lee February 18, 2003. Announcements. Homework #4 is available Due on Monday, March 17, 11:59pm Get started now! Quiz #2 Available on Tuesday, Feb.25
E N D
Lossy Compression 15-211 Fundamental Data Structures and Algorithms Peter Lee February 18, 2003
Announcements • Homework #4 is available • Due on Monday, March 17, 11:59pm • Get started now! • Quiz #2 • Available on Tuesday, Feb.25 • Some questions will be easier if you have some parts of HW4 working • Read • Chapter 8
Eliza • Eliza was one of the first AI programs • J. Weizenbaum, 1966 • At the time, it impressed people who used it • Eliza has been implemented many, many times • gnu emacs has one • try “M-x doctor”
Eliza’s impact • Many stories of Eliza’s impact • some people became so dependent that Weizenbaum eventually had to withdraw its use • some psychiatrists saw Eliza as a way for the profession to handle many more patients • Eliza might be used for most patients, and the human doctor reserved for only the most serious cases
Eliza’s rules • Eliza is a remarkably simple program • Some sample rules: • X me YX you Y • I remember X Why do you remember X just now? • My <family-member> is X Who else in your family is X? • X <family-member> Y Tell me more about your family • X That is very interesting
Why “Eliza”? • The name was chosen for its ability to converse increasingly well • The Greek legend of Pygmalion • the mysogynist King of Cyprus • fell in love with an ivory statue, Galatea • taking pity, Aphrodite made Galatea come alive • Pygmalion then married Galatea
Why “Eliza”? – cont’d • George Bernard Shaw wrote a play, Pygmalion, based on the legend • Professor Higgins creates a “lady” from a low-class cockney flower vendor, Eliza Doolittle • first filmed in 1938 • Later, adapted into the “politically correct” My Fair Lady
Wrap-Up on LZW Compression
Byte method LZW • We start with a trie that contains a root and n children • one child for each possible character • each child labeled 0…n • When we compress as before, by walking down the trie • but, after emitting a code and growing the trie, we must start from the root’s child labeled c, where c is the character that caused us to grow the trie
LZW: Byte method example • Suppose our entire character set consists only of the four letters: • {a, b, c, d} • Let’s consider the compression of the string • baddad
Byte LZW: Compress example baddad Input: ^ Dictionary: b a c d 0 1 2 3 Output:
Byte LZW: Compress example baddad Input: ^ Dictionary: b a c d 0 1 2 3 a 4 Output: 1
Byte LZW: Compress example baddad Input: ^ Dictionary: b a c d 0 1 2 3 d a 5 4 Output: 10
Byte LZW: Compress example baddad Input: ^ Dictionary: b a c d 0 1 2 3 d a d 5 4 6 Output: 103
Byte LZW: Compress example baddad Input: ^ Dictionary: b a c d 0 1 2 3 a d a d 5 4 6 7 Output: 1033
Byte LZW: Compress example baddad Input: ^ Dictionary: b a c d 0 1 2 3 a d a d 5 4 6 7 Output: 10335
Byte LZW output • So, the input • baddad • compresses to • 10335 • which again can be given in bit form, just like in the binary method… • …or compressed again using Huffman
Byte LZW: Uncompress example • The uncompress step for byte LZW is the most complicated part of the entire process, but is largely similar to the binary method
Byte LZW: Uncompress example 10335 Input: ^ Dictionary: b a c d 0 1 2 3 Output:
Byte LZW: Uncompress example 10335 Input: ^ Dictionary: b a c d 0 1 2 3 b Output:
Byte LZW: Uncompress example 10335 Input: ^ Dictionary: b a c d 0 1 2 3 a 4 ba Output:
Byte LZW: Uncompress example 10335 Input: ^ Dictionary: b a c d 0 1 2 3 d a 5 4 bad Output:
Byte LZW: Uncompress example 10335 Input: ^ Dictionary: b a c d 0 1 2 3 d a d 5 4 6 badd Output:
Byte LZW: Uncompress example 10335 Input: ^ Dictionary: b a c d 0 1 2 3 a d a d 5 4 6 7 baddad Output:
LZW applications • LZW is an extremely useful lossless method for compressing data • LZW is used in the GIF and compressed TIFF standards for image data • Unisys holds the patent on LZW, but allows free noncommercial use
LZW performance • Suppose we have a file of N a’s: • aaaa…a • What would the output look like after LZW compression? • What, roughly, is the size of the output (in big-Oh terms)? • How big would the output be if we used Huffman instead?
Lossy compression • Often, we can tolerate some loss of data through the compress/ decompress cycle. • Images, and especially video/audio, can be huge • HDTV bit rate is >1Gbps! • Big problem for storage and network
Techniques • Lossy compression is based on mathematical transformations • Discrete Cosine Transform (DCT) • Used in JPEG algorithm • Wavelet based image compression • Used in MPEG-4 • Many, many, others…
Image files One-dimensional array of 3 width height bytes Three two-dimensional arrays, one for each color component
Consider this color image This is part of a famous image (Do you know who? Hint: Splay) The image is a 16x16 bitmap image enlarged
The red image, again 173 165 165 165 148 132 123 132 140 156 173 181 181 181 189 173 198 189 189 189 181 165 148 165 165 173 181 198 206 198 181 165 206 206 206 206 198 189 181 181 198 206 206 222 231 214 181 165 231 222 206 198 189 181 181 181 206 222 222 222 231 222 198 181 231 214 189 173 165 165 173 181 181 189 198 222 239 231 206 214 206 189 173 148 148 148 148 165 156 148 165 198 222 231 214 239 181 165 140 123 123 115 115 123 140 148 140 148 165 206 239 247 165 82 66 82 90 82 90 107 123 123 115 132 140 165 198 231 123 198 74 49 57 82 82 99 107 115 115 123 132 132 148 214 239 239 107 82 82 74 90 107 123 115 115 123 115 115 123 198 255 90 74 74 99 74 115 123 132 123 123 115 115 140 165 189 247 99 99 82 90 107 123 123 123 123 123 132 140 156 181 198 247 239 165 132 107 148 140 132 132 123 132 148 140 140 156 214 198 231 165 156 132 156 156 140 140 140 148 148 132 140 156 222 247 239 222 181 181 140 156 140 148 148 148 140 132 156 206 222 214 198 181 181 181 181 173 148 156 148 140 140 165 198 222 239 Byte values (0…255) indicate intensity of the color at each pixel
173 165 165 165 148 132 123 132 140 156 173 181 181 181 189 173 198 189 189 189 181 165 148 165 165 173 181 198 206 198 181 165 206 206 206 206 198 189 181 181 198 206 206 222 231 214 181 165 231 222 206 198 189 181 181 181 206 222 222 222 231 222 198 181 231 214 189 173 165 165 173 181 181 189 198 222 239 231 206 214 206 189 173 148 148 148 148 165 156 148 165 198 222 231 214 239 181 165 140 123 123 115 115 123 140 148 140 148 165 206 239 247 165 82 66 82 90 82 90 107 123 123 115 132 140 165 198 231 123 198 74 49 57 82 82 99 107 115 115 123 132 132 148 214 239 239 107 82 82 74 90 107 123 115 115 123 115 115 123 198 255 90 74 74 99 74 115 123 132 123 123 115 115 140 165 189 247 99 99 82 90 107 123 123 123 123 123 132 140 156 181 198 247 239 165 132 107 148 140 132 132 123 132 148 140 140 156 214 198 231 165 156 132 156 156 140 140 140 148 148 132 140 156 222 247 239 222 181 181 140 156 140 148 148 148 140 132 156 206 222 214 198 181 181 181 181 173 148 156 148 140 140 165 198 222 239 The red image, again Byte values (0…255) indicate intensity of the color at each pixel
JPEG • Joint Photographic Expert Group • Voted as international standard in 1992 • Works well for both color and grayscale images • Many steps in the algorithm • Some requiring sophistication in mathematics • We’ll skip many parts and focus on just the main elements of JPEG
B G R Y I Q JPEG in a nutshell for each plane (scan) RGB to YIQ (optional) DPCM Zig-zag Quant DCT for each 8x8 block RLE 11010001… Huffman
B G R Y I Q JPEG in a nutshell for each plane (scan) RGB to YIQ (optional) DPCM Zig-zag Quant DCT for each 8x8 block RLE 11010001… Huffman
Linear transform coding • For video, audio, or images, one key first step of the compression will be to encode values over regions of time or space • The basic strategy is to select a set of linear basis functions i that span the space • sin, cos, wavelets, … • defined at discrete points
Linear transform coding • Coefficients: • In matrix notation: • Where A is an nxn matrix, and each row defines a basis function
Discrete Cosine Transform • DCT separates the image into spectral sub-bands of differing importance • With input image A, the output coefficients B are given by the following equation: N1 and N2 give the image’s height and width
B G R Y I Q JPEG in a nutshell for each plane (scan) RGB to YIQ (optional) DPCM Zig-zag Quant DCT for each 8x8 block RLE 11010001… Huffman
Quantization • The purpose of quantization is to encode an entire region of values into a single value • For example, can simply delete low-order bits: • 101101 could be encoded as 1011 or 101 • When dividing by power-of-two, this amounts to deleting whole bits • Other division constants give finer control over bit loss • JPEG uses a standard quantization table