Colorado School of Mines Image and Multidimensional Signal Processing Professor William Hoff Dept of Electrical Engineering &Computer Science Colorado School of Mines Image and Multidimensional Signal Processing http://inside.mines.edu/~whoff/ Image Compression Colorado School of Mines Image and Multidimensional Signal Processing 2 Image Compression • Goal: Reduce amount of data, for transmission or storage • Try to preserve information, so that image can be reconstructed: – Exactly (loss-less compression) – Approximately (lossy compression) Compression ratio C b b' where b = # bits uncompressed b’ = #bits compressed Colorado School of Mines Image and Multidimensional Signal Processing 3 Redundancy • We take advantage of redundancy in the input image: – Coding redundancy – Spatial and temporal redundancy – Irrelevant information Coding redundancy: Only a few gray values are present; we can represent using short code words (with few bits) Colorado School of Mines Spatial redundancy: All gray values are present, but pixels along each row are the same; if you know the value of the first pixel you know the whole row Image and Multidimensional Signal Processing Irrelevant information: Only a few gray values are present, clustered around value 128; could represent using a constant 128 and would look the same 4 Coding Redundancy • Use short code words instead of long ones • Can use variable length codes, so that most common values have shortest codes • Average bit length L 1 Lavg l (rk ) pr (rk ) k 0 Colorado School of Mines Image and Multidimensional Signal Processing 5 Measuring Image Information • We can compute the theoretical minimum number of bits needed to code an image • First, consider an “event” E – If the event is unlikely to happen, then its probability P(E) is small, and 1/P(E) is large – When the event does occur, it is significant and provides us with a lot of information • We define the “self information” of event E 1 log 2 ( P( E )) I ( E ) log 2 P( E ) • This is how much information is attached to E • Example: – If P(E) = 1, I = 0 bit – If P(E) = ½, I = 1 bit – If P(E) = ¼, I = 2 bits Colorado School of Mines Image and Multidimensional Signal Processing 6 Measuring Image Information • Say we have a set of random events, drawn from a possible set of values {a1, a2, …, aJ} • We’ll call these the “source symbols” – They could be randomly generated from a transmitter • The probabilities of generating these symbols are {P(a1), P(a2), …, P(aJ)} • The information carried by a single symbol is -log2(P(aj)) • Thus, the average information per symbol is J H P(a j ) log P(a j ) j 1 Colorado School of Mines Image and Multidimensional Signal Processing Assumes that events are independent 7 Entropy of an image • For images, the “events” {r0, r1, …, rL-1} are possible gray level values • We can use the histogram to estimate the probabilities of the symbols • The average information per pixel is (also called the entropy of the image) L 1 H pr (rk ) log 2 pr (rk ) k 0 This is the best you can do (for uncorrelated values) • Examples: – 8 gray levels, same probability for all values: p0=p1= … = p7=1/8. H=? – 8 gray levels, but only one has nonzero probability, say level 1: p0=p2= … = p7 = 0, p1=1. H=? Colorado School of Mines Image and Multidimensional Signal Processing 8 Entropy - example rk L 1 H pr (rk ) log 2 pr (rk ) k 0 P(ai) 87 128 186 255 0.25 0.47 0.25 0.03 log P(ai) -P log P -2 0.5 -1.08927 0.511956 -2 0.5 -5.05889 0.151767 H (bits) Colorado School of Mines Image and Multidimensional Signal Processing 1.663722 9 Fidelity Criteria • RMS (root mean square) error – an objective measure of error between original and the compressed image erms 1 MN M 1 N 1 x 0 y 0 ˆf ( x, y) f ( x, y) • Can also use SNR M 1 N 1 SNRms x 0 y 0 1/ 2 2 fˆ ( x, y ) 2 M 1 N 1 fˆ ( x, y ) f ( x, y ) x 0 y 0 2 • And subjective measures – E.g., rate quality on a scale of 1 to 10 Colorado School of Mines Image and Multidimensional Signal Processing 10 Image Compression & Decompression Model • Mapper: Transforms data to a form that can be more easily compressed (eg, Fourier or wavelet transform) • Quantizer: Reduces amount of data (eg., throws away smallest transform coefficients) • Symbol coder: Codes the resulting data using the shortest code words (eg., variable length coding) Colorado School of Mines Image and Multidimensional Signal Processing 11 Loss-less Compression Methods • “Loss-less”: the compressed image can be reconstructed exactly • We’ll look at these methods: – – – – Huffman coding Arithmetic coding LZW coding Run-length coding Colorado School of Mines Image and Multidimensional Signal Processing 12 Huffman Coding • Takes advantage of coding redundancy • Generates a variable length code, as close as possible to the theoretical minimum length • Doesn’t take advantage of inter-pixel redundancy • Widely used as a component (the symbol coder) in many compression methods Algorithm: (1) Find the gray level probabilities (2) Order the probabilities, from smallest to largest (3) Combine the smallest two by addition (4) Repeat steps 2-3 until only two probabilities are left (5) By working backward along the tree, generate code by alternating assignment of 0 and 1 Colorado School of Mines Image and Multidimensional Signal Processing 13 Example Start with gray level probabilities Symbol Probability a1 0.1 a2 0.4 a3 0.06 a4 0.1 a5 0.04 a6 0.3 Colorado School of Mines Sort the probabilities, from largest to smallest Image and Multidimensional Signal Processing Combine the smallest two by addition 14 Example Colorado School of Mines Image and Multidimensional Signal Processing 15 Example Work backward along the tree, generate code by alternating assignment of 0 and 1 Final result: Sym Code Colorado School of Mines Image and Multidimensional Signal Processing 16 Example • Example – generate code sequence for symbols a2 a1 a3 a1 • Example – decode the sequence 010100111100 • Average length of code? Colorado School of Mines Image and Multidimensional Signal Processing 17 Example • Compare to theoretical minimum J H (z ) P( a j ) log2 P( a j ) j 1 P(ai) a2 a6 a1 a4 a3 a5 log P(ai) -P log P 0.4 0.3 0.1 0.1 0.06 0.04 H Colorado School of Mines Image and Multidimensional Signal Processing 18 Example • Compare to theoretical minimum J H (z ) P( a j ) log2 P( a j ) j 1 P(ai) a2 a6 a1 a4 a3 a5 Colorado School of Mines 0.4 0.3 0.1 0.1 0.06 0.04 log P(ai) -1.32193 -1.73697 -3.32193 -3.32193 -4.05889 -4.64386 -P log P 0.528771 0.52109 0.332193 0.332193 0.243534 0.185754 H 2.143534 Image and Multidimensional Signal Processing 19 Example • Find Huffman code for Gray Level Probability 00 0.2 01 0.3 10 0.1 11 0.4 Algorithm: (1) Find the gray level probabilities (2) Sort the probabilities (3) Combine the smallest two by addition (4) Repeat steps 2-3 until only two are left (5) Work backward, generate code Lavg = ? H=? Colorado School of Mines Image and Multidimensional Signal Processing 20 Example • Find Huffman code for Algorithm: (1) Find the gray level probabilities (2) Sort the probabilities (3) Combine the smallest two by addition (4) Repeat steps 2-3 until only two are left (5) Work backward, generate code Gray Level Probability 00 0.2 010 0.4 1 0.4 1 0.6 0 01 0.3 00 0.3 00 0.3 00 0.4 1 10 0.1 011 0.2 010 11 0.4 1 0.1 011 Colorado School of Mines 0.3 01 Image and Multidimensional Signal Processing 21 Arithmetic Coding • A sequence of values is assigned a single arithmetic code word • The code word is a fractional number between 0 and 1 (e.g., 0.4731) • Each symbol is assigned an interval based on its probability of occurrence • Code words are fixed length Colorado School of Mines Image and Multidimensional Signal Processing 22 Example Message: a1a2a3a3a4 With each new interval, you find the subintervals by multiplying the total interval by the probability of each symbol, and then add it to the lower bound of the interval to get the upper bound. Colorado School of Mines Image and Multidimensional Signal Processing 23 Example Message: a1a2a3a3a4 (0.08-0.04)(0.8)+0.04 0.8 0.072 (0.072-0.056)(0.8)+0.056 0.0688 Can use 0.068 0.2(0.4)+0 0.4 0.08 0.2 0.04 Colorado School of Mines Image and Multidimensional Signal Processing 24 Example • Final code word: 0.068 • Three decimal digits for five symbols, or 3/5 = 0.6 digits per symbol – Equivalently, 1.99 bits – 100.6 = 2x -> x = 1.99 bits • Theoretical minimum: 1.92 bits or 0.58 digits a1 a2 a3 a4 P(ai) log P(ai) 0.2 -2.3219 0.2 -2.3219 0.4 -1.3219 0.2 -2.3219 H Colorado School of Mines -P log P 0.46439 0.46439 0.52877 0.46439 1.92193 Image and Multidimensional Signal Processing 25 Example • Colorado School of Mines Image and Multidimensional Signal Processing The sequence “a1 a1 a1” can be encoded as what number? 26 LZW Coding • Stands for Lempel-Ziv-Welch • Works by coding short strings of data • Used in GIF, TIFF, and PDF file formats • Creates a “dictionary” of code words – For an 8-bit image, the first 256 words are assigned to the gray values 0,1,2, … , 255 – As sequences are discovered, new code words (i.e., 256 through 511) are assigned to represent them – Eg: The sequence 126-126 may be assigned to code word 256 Colorado School of Mines Image and Multidimensional Signal Processing 27 Example • Original image 39 39 126 126 39 39 126 126 39 39 126 126 39 39 126 126 Colorado School of Mines Initial Dictionary Dictionary Index Entry 0 0 1 1 : : 255 255 256 ? : : 511 ? Image and Multidimensional Signal Processing 28 Colorado School of Mines Image and Multidimensional Signal Processing 29 Final Dictionary • Original image 39 39 126 126 39 39 126 126 39 39 126 126 39 39 126 126 • Coded sequence 39 39 256 126 258 260 259 257 Colorado School of Mines 126 126 Dictionary Index Entry 0 0 1 1 : : 255 255 256 39-39 257 39-126 258 126-126 259 126-39 260 39-39-126 261 126-126-39 262 39-39-126-126 263 126-39-39 264 39-126-126 Image and Multidimensional Signal Processing 30 Run Length Coding • Inter-pixel redundancy • Usually used for binary images • Output the number of consecutive 0’s along a row, then the # of 1’s, etc 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 8 044 125 : Output code • Best case for compression: a row is all zeros (or all ones) • Worst case for compression? Colorado School of Mines Image and Multidimensional Signal Processing 31 RLC Applied to Non-binary Images • For an m-bit image, the gray values are am-12m-1 + am-22m-2 + … + a121 + a020 • Apply RLC separately to each bit plane • This can be a problem in areas where values fluctuate about certain transition points – Example: 12710 = 011111112 – But 12810 = 100000002 – So each bit keeps flipping from 0 to 1 Colorado School of Mines Image and Multidimensional Signal Processing 32 Example Colorado School of Mines Image and Multidimensional Signal Processing 33 Better: Use Gray Code • First code the image using the “Gray Code” rather than the normal binary code • Gray code: – Each adjacent code word differs only by one bit – So if a pixel differs from its neighbor by one gray level, only one bit in the gray code is different Colorado School of Mines Image and Multidimensional Signal Processing 34 Gray Codes 2 bit binary 2 bit Gray 0 00 00 1 01 01 10 11 11 10 2 3 Colorado School of Mines 3 bit Gray Image and Multidimensional Signal Processing 35 Colorado School of Mines Image and Multidimensional Signal Processing 36