Lecture 5 Image Compression Dr. Mohsen NASRI College of Computer and Information Sciences, Majmaah University, Al Majmaah m.nasri@mu.edu.sa Introduction Definition Reduce size of Image (number of bits needed to represent an image) Benefits Reduce storage needed Reduce transmission cost / latency / bandwidth Applications • Wireless image transmission • image storage in multimedia/image databases difficult to send real-time uncompressed image over current network 2 Need for image compression Huge sizes of the data files, in uncompressed form The storage devices have relatively slow access =>impossible to render/index image info in real time limited bandwidth of the communication channels => Multimedia data (esp. image and video) have large data volume The main advantage/need of/for image compression is that it reduces the data storage requirement. It also offers an alternative approach to reduce the communication cost 3 Need for image compression (cont…) • Reduce the size of data. – Reduces storage space and hence storage cost. • Compression ratio = original data size/compressed data size – Reduces time to retrieve and transmit data. 4 Basic concept of a compresssion 5 Compression categories • Compression = Image coding Image compression Lossless Lossy Lossless compression The information content is not modified Lossy compression The information content is reduced and it is not recoverable 6 Image compression model Original Image o Statistical Redundancy o Psychovisual Redundancy Reduction of data redundancy Reduction of Entropy Entropy encoding Decompressed Image 7 Statistical Redundancy Statistical redundancy occurs due to the fact that pixels within an image tend to have very similar intensities as those of its neighborhood, except at the object boundaries or illumination changes. For still images, statistical redundancies are essentially spatial in nature. For natural two-dimensional images, redundancies are present along both the x- and y-dimensions. Video signals exhibit yet another form of statistical redundancy and that is temporal. For video, intensities of same pixel positions across successive frames tend to be very similar, unless there is large amount of motion present. In this lesson however, we focus our attention to only one form of statistical redundancy and that is spatial redundancies of images. Presence of statistical redundancies in natural images allows efficient representation in the transformed output. 8 Psychovisual Redundancy Psychovisual redundancy arises due to the problem of perception. Our eyes are more responsive to slow and gradual changes of illumination than perceiving finer details and rapid changes of intensities. Hence, to what extent we should preserve the details for our perception and to what extent we can compromise on the quality of reconstructed image that we perceive is essentially carried out by exploiting the psychovisual redundancy. As we shall see later, psychovisual redundancy has been well studied and its exploitation has been included within the multimedia standards. 9 Measuring the quality of reconstructed images To evaluate the image quality, we should use the peak signal-to-noise ratio metric, which is defined (in decibels) by: where q is the number of bits per pixel (bpp) of the original image, and MSE is the mean-square-error which is defined by: 10 Elements of Image Compression System A typical image compression system/image encoder consists of the following elements Original Image Decoded Image Bitstream Encoder 0101100111... Decoder Compression system Compressed Decompression system Image The image file is converted into a series of binary data, which is called the bit-stream The decoder receives the encoded bit-stream and decodes it to reconstruct the image The total data quantity of the bit-stream is less than the total data quantity of the original image 11 Source coding algorithms To achieve less average length of bits per pixel of the image. Assigns short descriptions to the more frequent outcomes and long descriptions to the less frequent outcomes Entropy Coding Methods Huffman Coding Arithmetic Coding Lossless Compression Sequence of source symbols ui Source Encoder Source alphabet U {u1 u 2 P { p1 p2 Sequence of code symbols ai Code alphabet uM} pM} A {a1 a2 an} 12 Huffman code Approach Variable length encoding of symbols Exploit statistical frequency of symbols Efficient when symbol probabilities vary widely Principle Use fewer bits to represent frequent symbols Use more bits to represent infrequent symbols A A A A B B A A 13 Huffman Code Data Structures Binary (Huffman) tree 4 Represents Huffman code Edge code (0 or 1) Leaf symbol Path to leaf encoding Example A = “000”, B = “001”, C = “01” 1 0 3 To efficiently build binary tree 1 0 2 d = “1” Priority queue 1 0 1 A D 1 1 C 1 B 14 Huffman Code Algorithm Overview Encoding Calculate frequency of symbols in file Create binary tree representing “best” encoding Use binary tree to encode compressed file For each symbol, output path from root to leaf Size of encoding = length of path Save binary tree 15 Huffman Code – Algorithm Huffman Coding Algorithm (1) Order the symbols according to the probabilities Alphabet set: S1, S2,…, SN Probabilities: P1, P2,…, PN The symbols are arranged so that P1≧ P2 ≧ … ≧ PN (2) Apply a contraction process to the two symbols with the smallest probabilities. Replace the last two symbols SN and SN-1 to form a new symbol HN-1 that has the probabilities P1 +P2. The new set of symbols has N-1 members: S1, S2,…, SN-2 , HN-1 (3) Repeat the step 2 until the final set has only one member. (4) The codeword for each symbol Si is obtained by traversing the binary tree from its root to the leaf node corresponding to Si 16 Huffman Tree Construction The message to be encoded is "ABBBBAAC" Symbols A B C Frequency of occurrence 3 4 1 Huffman code 0 1 00 8 High frequency of occurrence : shorter bit strings 0 1 B 4 4 0 1 C Low frequency of occurrence :Longer bit strings 1 3 A char encoding A 10 B 0 C 11 B The principle is to use a lower number of bits to encode the data that occurs more frequently 17 Huffman Code Properties Prefix code No code is a prefix of another code Example Huffman(“I”) 00 Huffman(“X”) 001 // not legal prefix code Can stop as soon as complete code found No need for end-of-code marker Nondeterministic Multiple Huffman coding possible for same input If more than two trees with same minimal weight 18 Huffman Code Properties Greedy algorithm Chooses best local solution at each step Combines 2 trees with lowest frequency Still yields overall best solution Optimal prefix code Based on statistical frequency Better compression possible (depends on data) Using other approaches (e.g., pattern dictionary) 19 Arithmetic Coding Arithmetic Coding: a direct extension of Shannon-Fano-Elias coding calculate the probability mass function p(xn) and the cumulative distribution function F(xn) for the source sequence xn Lossless compression technique Treate multiple symbols as a single data unit Arithmetic Coding Algorithm Input symbol is l Previouslow is the lower bound for the old interval Previoushigh is the upper bound for the old interval Range is Previoushigh - Previouslow Let Previouslow= 0, Previoushigh = 1, Range = Previoushigh – Previouslow =1 WHILE (input symbol != EOF) EOF = End Of File get input symbol l Range = Previoushigh - Previouslow New Previouslow = Previouslow + Range* intervallow of l New Previoushigh = Previouslow + Range* intervalhigh of l END Arithmetic Coding Symbol Probability Sub-interval k 0.05 [0.00,0.05) l 0.2 [0.05,0.25) u 0.1 [0.20,0.35) w 0.05 [0.35,0.40) e 0.3 [0.40,0.70) r 0.2 [0.70,0.90) ? 0.2 [0.90,1.00) 1 Input String : k l u w r e ? k l u wre ? 0.0713348389 0.25 0.10 0.074 0.0714 0.07136 0.071336 0.0713360 ? ? ? ? ? ? ? ? r r r r r r r r e e e e e e e e w u w u w u w u w u w u w u w u l l l l l l l l k k k k k k k k 0 0.05 0.06 0.070 0.0710 0.07128 0.07132 0.0713336 1.00 0.90 0.70 0.40 0.35 0.25 0.05 0 Arithmetic Coding cont… Input String : ESIPE Symbol Symbol Probability Sub-interval E 0.4 [0.00,0.4[ S 0.2 [0.4,0.6[ I 0.2 [0.6,0.8[ P 0.2 [0.8,1.0[ lower bound upper bound 0.0 1.0 E 0.0 0.4 S 0.16 0.24 I 0.208 0.224 P 0.2208 0.224 E 0.2208 0.22208 The lower bound code the Input string Overview of Image Compression Algorithms Embedded zero-trees Wavelet (EZW) The EZW encoder is based on two important observations: 1. Natural images in general have a low pass spectrum. When an image is wavelet transformed the energy in the sub-bands decreases as the scale decreases (low scale means high resolution), so the wavelet coefficients will, on average, be smaller in the higher sub-bands than in the lower sub-bands. This shows that progressive encoding is a very natural choice for compressing wavelet transformed images, since the higher sub-bands only add detail; 2. Large wavelet coefficients are more important than smaller wavelet coefficients. 23 Overview of Image Compression Algorithms cont… Embedded zero-trees Wavelet (EZW) The EZW encoder is based on DWT – Energy is compacted into a small number of coeff. – Significant coeff. tend to cluster at the same spatial location in each frequency subband LL3 Two set of info. to code: – Where are the significant coefficients? – What values are the significant coefficients? HL3 HL2 LH3 HH3 HL1 LH2 HH2 LH1 HH1 •Three resoluti on level • Two decomp osition levels 24 Statistics Filters EZW based on three basic elements Original Image Reconstructed Image DWT EZW Encoder Arithmetic Encoder Arithmetic decoder EZW decoder 2-D IDWT Encoded Image 25 Overview of Image Compression Algorithms cont… Embedded zero-trees Wavelet (EZW) coefficients that are in the same spatial location consist of a quad-tree. Parent-children relation among coeff. – Each parent coeff at level k spatially correlates with 4 coeff at level (k-1) of same orientation – A coeff at lowest band correlates with 3 coeff. 26 Overview of Image Compression Algorithms cont… Structure of EZW Root: a Descendants: a1, a2, a3 27 Overview of Image Compression Algorithms cont… Structure of EZW EZW scanning order of sub-bands scan order of the transmission band 28 Overview of Image Compression Algorithms cont… EZW algorithm With Ti=Ti/2 and T0 = ׀Cmax׀/2 29 Overview of Image Compression Algorithms cont… EZW algorithm 30 Overview of Image Compression Algorithms cont… EZW algorithm P N IZ ZT 31 EZW - example 32 EZW – Example cont... 33 EZW – Example Cont... 34 EZW – Example Cont... 35 Contd.. 36 JPEG JPEG encoder The encoding process consists of several steps: • Color space transformation. • Block splitting. • Discrete Cosine Transform. • Quantization. • Entropy coding. AC Color components (Y, Cb, or Cr) 88 FDCT Zig-zag reordering Huffman Table Huffman coding JPEG bit-stream Quantizer DC Quantization Table Difference Encoding Huffman coding Huffman Table • JPEG : Joint Photographic Experts Group 37 JPEG JPEG decoder Bit-stream De-zigzag Huffman Decoding De-DC coding Huffman Decoding Dequantizer Quantization Table Chrominance Upsampling (4:2:2 or 4:2:0) G 8X8 IDCT YVU color coordinate B R Decoded Image 38 Color Space Conversion Y 0.299000 0.587000 0.114000 R 0 C 0.168736 0.331264 0.500002 G 128 b Cr 0.500000 0.418688 0.081312 B 128 (a) translate from RGB to YCbCr 0.0 1.40210 Y R 1.0 G 1.0 0.34414 0.71414 C 128 b B 1.0 1.77180 0.0 Cr 128 (b) translate from YCbCr to RGB 39 DC and AC coefficients • DC Coefficient : • First coefficient in every 8 x 8 block • Represents the average value of pixels in block • AC Coefficients : Remaining 63 coefficients in every 8 x 8 block • DC Coefficients: treated separately from the AC Coefficients • Differential values of DC coeffs. of all blocks are derived and encoded 40 Zig-zag ordering in an 8x8 matrix Zig-Zag ordering: converting a 2D matrix into a 1D array, so that the frequency (horizontal+vertical) increases in this order, and the coefficient variance decreases in this order. 41 Quantization in JPEG Quantization step is the main source of Lossy Compression • DCT itself is not Lossy Quantisation Tables • In JPEG, each F[u,v] is divided by a constant q(u,v). • Table of q(u,v) is called quantisation table. F (u, v) Quantization F (u, v)Quantization round Q ( u , v ) Dequantization F (u, v) deQ F (u, v)Quantization Q(u, v) 16 12 14 14 QY 18 24 49 72 11 10 16 24 40 51 12 14 19 13 16 24 26 40 58 57 60 69 17 22 29 22 37 56 51 87 80 68 109 103 35 55 64 81 104 113 64 78 87 103 121 120 92 95 98 112 100 103 61 55 56 62 77 92 101 99 42 JPEG 2000 JPEG 2000 Encoder B G R Image JPEG 2000 Bit-stream Forward Component Transform 2D DWT Context Modeling Quantization EBCOT RateDistortion Control Arithmetic Coding Tier-1 Tier-2 JPEG 2000 Decoder JPEG 2000 Bit-stream EBCOT Decoder Dequantization 2D IDWT Inverse Component Transform R G B Decoded Image 43 Quantization in JPEG 2000 | ab(u, v) | q b ( u , v ) sign a u ( u , v ) floor Quantization coefficients b ab(u,v) : the wavelet coefficients of subband b Quantization step size b 2 Rb b b 1 11 2 Rb: the nominal dynamic range of subband b εb: number of bits alloted to the exponent of the subband’s coefficients μb: number of bits allotted to the mantissa of the subband’s coefficients 44 Bitplane Scanning • The decimal DWT coefficients can be converted into signed binary format, so the DWT coefficients are decomposed into many 1-bit planes. • In one 1-bit-plane – Significant • A bit is called significant after the first bit ‘1’ is met from MSB to LSB – Insignificant • The bits ‘0’ before the first bit ‘1’ are insignificant n n Sign MSB 0 MSB insignificant 0 coding order significant First 1 appear 1 0 0 LSB 1 1 45 LSB Scanning Sequence • The scanning order of the bit-plane • Sample – Each element of the bit-plane is called a sample • Column stripe – Four vertical samples can form one column stripe • Full stripe – The 16 column stripes in the horizontal direction can form a full stripe Stripe height of 4 Code block 64-bits wide 1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 2 6 10 14 18 22 26 30 34 38 42 46 50 54 58 62 3 7 11 15 19 23 27 31 35 39 43 47 51 55 59 63 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 65 ... 66 ... 46 Comparison between JPEG and JPEG 2000 Image 1 Image 2 47 Thank You Have a Nice Day