Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Image compression

Image compression

Mohammed Hachama

November 17, 2024
Tweet

More Decks by Mohammed Hachama

Other Decks in Education

Transcript

  1. Digital Image Processing Compression (Week 6) NHSM - 4th year

    - Fall 2024 - Prof. Mohammed Hachama [email protected] http://hachama.github.io/home/
  2. Outline Introduction Huffman Coding Transform-Domain Compression JPEG algorithm NHSM -

    4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 2/14
  3. Introduction Compression • 2 hours color video: • 1000x1000 pixels

    • 8 bits per channel • 30 frames/sec • 120 minutes NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 4/14
  4. Introduction Compression • 2 hours color video: • 1000x1000 pixels

    • 8 bits per channel • 30 frames/sec • 120 minutes Size = NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 4/14
  5. Introduction Compression • 2 hours color video: • 1000x1000 pixels

    • 8 bits per channel • 30 frames/sec • 120 minutes Size = 1000x1000x24x30x60x120 = 5184 × 109 bits = 648Go !! → We definitely need compression! NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 4/14
  6. Introduction Compression • Why compression is possible? • Redundancy in

    data: • Coding redundancy (just 4 colors), spatial redundancy, and irrelevant information. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 4/14
  7. Huffman Coding Coding • Set A = {s1, · ·

    · , sN} is called an alphabet. • Each si ∈ A is called a symbol. • A string over the alphabet is a sequence of symbols, e.g. s11 s4 s105 s105 s47 s86 s86 s3 · · · s30. • A codeword is a sequence of bits that represents a symbol or string. e.g. 1011000010110111101. • pi = p(si ) is the probability of occurrence of symbol si in a string. • Li is the length of the codeword for si in bits. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 6/14
  8. Huffman Coding Coding • Fixed length: all codewords have the

    same length (number of bits), e.g., letters A through H encoded as 3-bit binary numbers. A ↔ 000, B ↔ 001, C ↔ 010, · · · , H ↔ 111 • Variable length: different codewords may have different lengths depending on, for example, their probability of occurrence. A ↔ 0, B ↔ 10, 110, · · · , H ↔ 11111110 • This variable length code is a prefix code: no code is the prefix of another code. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 6/14
  9. Huffman Coding Huffman Coding • Represent highly probable values (intensities)

    by a small number of bits and lowly probable values by relatively more number of bits • Coding 1. Find the probabilities of the occurrence of each value of the image. 2. Starting from the lowest probability, pair the two of the lowest probabilities to get a binary tree. 3. Assign 0 to leaf to the left side of a branch and 1 to that in the right side or vice versa. 4. Repeat steps 2 and 3, until all the values are placed. 5. Read the codes from the root of the tree to the leaf node where the value is placed. • Decoding: looking at the first few characters until a code corresponding to a value is found. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 7/14
  10. Huffman Coding Example 13 14 2 14 8 2 2

    8 15 15 2 15 15 8 13 2 NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 7/14
  11. Huffman Coding Example 13 14 2 14 8 2 2

    8 15 15 2 15 15 8 13 2 NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 7/14
  12. Huffman Coding Example 13 14 2 14 8 2 2

    8 15 15 2 15 15 8 13 2 Code = ? NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 7/14
  13. Huffman Coding Example 13 14 2 14 8 2 2

    8 15 15 2 15 15 8 13 2 {00010000100000011100101011010100100011} NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 7/14
  14. Huffman Coding Example 13 14 2 14 8 2 2

    8 15 15 2 15 15 8 13 2 {00010000100000011100101011010100100011} {001000110000110100110101110100100111} NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 7/14
  15. Huffman Coding Entropy • A measure of the uncertainty or

    unpredictability of a random variable or data source. • Introduced by Claude Shannon in 1948, it quantifies the theoretical minimum number of bits per pixel (bpp) for compressing by a lossless compression algorithm. • The entropy H(X) of a discrete random variable X is H(X) = − n i=1 p(xi ) log 2 p(xi ) • log: Additivity of independent events H(X, Y ) = H(X) + H(Y ) • Base 2 of the logarithm: entropy measured in bits NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 8/14
  16. Huffman Coding Entropy: example 13 14 2 14 8 2

    2 8 15 15 2 15 15 8 13 2 • Theoretical minimum number of bits required for lossless compression • Entropy = • The actual value of the first method = • The actual value of the second method = NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 8/14
  17. Huffman Coding Entropy: example 13 14 2 14 8 2

    2 8 15 15 2 15 15 8 13 2 • Theoretical minimum number of bits required for lossless compression • Entropy = 2.2272 • The actual value of the first method = 2.375 • The actual value of the second method = 2.25 NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 8/14
  18. The Discrete Wavelet Transform The 1-level (scale 1) 4-point Haar

    DWT • Matrix formulation of the DWT ∼ DFT      XΦ (1, 0) XΦ (1, 1) XΨ (1, 0) XΨ (1, 1)      = 1 √ 2      1 1 0 0 0 0 1 1 1 −1 0 0 0 0 1 −1           x(0) x(1) x(2) x(3)      or X = W4,1 x. → Analysis • Synthesis: iDWT x = W−1 4,1 X. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 10/14
  19. The Discrete Wavelet Transform The 1-level (scale 1) 4-point Haar

    DWT • Convolution NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 10/14
  20. The Discrete Wavelet Transform 2-Level Haar DWT NHSM - 4th

    year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 10/14
  21. The Discrete Wavelet Transform 2-Level Haar DWT NHSM - 4th

    year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 10/14
  22. The Discrete Wavelet Transform Haar 2-D DWT • Computation of

    a 1-level 2-D Haar DWT using a 2-stage filter bank NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 10/14
  23. The Discrete Wavelet Transform Haar 2-D DWT • Computation of

    a 1-level 2-D Haar IDWT using a 2-stage filter bank NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 10/14
  24. Image Compression with Haar Filters Encoding • Consider the compression

    of the 4 × 4 8-bit image 172 188 189 186 178 187 189 192 188 190 196 197 191 193 197 199 NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  25. Image Compression with Haar Filters Encoding • 1. The image

    is level shifted by subtracting 128 (0.5 times the maximum gray-level value) 44 60 61 58 50 59 61 64 60 62 68 69 63 65 69 71 • This ensures that the DWT coefficients are more evenly distributed around zero and quantization will be more effective. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  26. Image Compression with Haar Filters Encoding • 2. 2-D Haar

    DWT 106.5 122.0 -12.5 0 125.0 138.5 -2.0 -1.5 -2.5 -3.0 -3.5 3.0 -3.0 -1.5 0 0.5 NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  27. Image Compression with Haar Filters Encoding • 3. Quantization. The

    quantized levels are assumed to be −64 to 63. The maximum value of the coefficients is X(1, 1) = 138.5. Therefore, the quantization step is 138.5/63 = 2.1984. 48 55 -5 0 56 63 0 0 -1 -1 -1 1 -1 0 0 0 • A high quantization step will result in more compression, but the quality of the reconstructed image will be low. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  28. Image Compression with Haar Filters Encoding • 4. The quantized

    4 × 4 matrix is converted to a 1 × 16 vector by arranging the values in the zigzag order (to help getting a high run-length of zeros). • The approximation coefficients are followed by the H, V, and D detail coefficients. {48, 55, 56, 63, −5, 0, 0, 0, −1, −1, −1, 0, −1, 1, 0, 0} NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  29. Image Compression with Haar Filters Encoding • 5. A Huffman

    code • Dictionary 1 : 0 0 1 1 48 : 0 0 1 0 55 : 0 0 0 0 1 56 : 0 0 0 0 0 63 : 0 0 0 1 1 −5 : 0 0 0 1 0 −1 : 0 1 0 : 1 • The Huffman code {0010 00001 00000 00011 00010 1 1 1 01 01 01 1 01 0011 1 1} NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  30. Image Compression with Haar Filters Encoding • 5. A Huffman

    code • The compressed image requires 42 bits. • The compression ratio is C = 128/42 = 3.0476. • The bits per pixel is bpp = 8/3.0476 = 2.6250. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  31. Image Compression with Haar Filters Decoding • . The code

    is decoded using the dictionary and the resulting 1-D vector is converted to the 4 × 4 matrix. The values are multiplied by the quantization step to get 105.5238 120.9127 -10.9921 0 123.1111 138.5 0 0 -2.1984 -2.1984 -2.1984 -2.1984 -2.1984 0 0 0 NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  32. Image Compression with Haar Filters Decoding • IDWT 45.0675 58.2579

    60.4563 58.2579 49.4643 58.2579 60.4563 62.6548 60.4563 60.4563 69.25 69.25 62.6548 62.6548 69.25 69.25 NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  33. Image Compression with Haar Filters Decoding • IDWT 45.0675 58.2579

    60.4563 58.2579 49.4643 58.2579 60.4563 62.6548 60.4563 60.4563 69.25 69.25 62.6548 62.6548 69.25 69.25 • Adding 128 to get the reconstructed image. 173.0675 186.2579 188.4563 186.2579 177.4643 186.2579 188.4563 190.6548 188.4563 188.4563 197.25 197.25 190.6548 190.6548 197.25 197.25 NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  34. Image Compression with Haar Filters Decoding • IDWT 45.0675 58.2579

    60.4563 58.2579 49.4643 58.2579 60.4563 62.6548 60.4563 60.4563 69.25 69.25 62.6548 62.6548 69.25 69.25 • Adding 128 to get the reconstructed image. 173.0675 186.2579 188.4563 186.2579 177.4643 186.2579 188.4563 190.6548 188.4563 188.4563 197.25 197.25 190.6548 190.6548 197.25 197.25 • SNR is NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  35. Image Compression with Haar Filters Decoding • IDWT 45.0675 58.2579

    60.4563 58.2579 49.4643 58.2579 60.4563 62.6548 60.4563 60.4563 69.25 69.25 62.6548 62.6548 69.25 69.25 • Adding 128 to get the reconstructed image. 173.0675 186.2579 188.4563 186.2579 177.4643 186.2579 188.4563 190.6548 188.4563 188.4563 197.25 197.25 190.6548 190.6548 197.25 197.25 • SNR is 45.2924 dB. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 11/14
  36. JPEG algorithm • JPEG (Joint Photographic Experts Group) is a

    commonly used method of lossy compression for digital images. • Tasked in the late 1980’s to generate a general-purpose standard for compression of almost all continuous tone and still-image applications. • The main idea is to transform the image into a frequency domain, quantize it, and then encode it. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 13/14
  37. JPEG algorithm • JPEG Codec: JPEG Compression Steps NHSM -

    4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 13/14
  38. JPEG algorithm • 1. Color Space Conversion • Convert RGB

    to YCbCr color space. • Y: Luminance (brightness); Cb and Cr: Chrominance (color information). NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 13/14
  39. JPEG algorithm • 2. Downsampling • Human vision is less

    sensitive to color details. • Reduce the resolution of Cb and Cr components. • Typically 2:1 horizontal and vertical downsampling. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 13/14
  40. JPEG algorithm • 3. Discrete Cosine Transform (DCT) • Apply

    DCT to 8x8 blocks of the image. • Transforms spatial domain to frequency domain. Most of the energy is concentrated in a few low-frequency components. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 13/14
  41. JPEG algorithm • 3. Discrete Cosine Transform (DCT) C(u, v)

    = 1 4 7 x=0 7 y=0 Y (x, y)·cos (2x + 1)uπ 16 ·cos (2y + 1)vπ 16 • The top-left corner represents the low frequencies (most important information), while the bottom-right corner represents high frequencies (less important, more compressible). NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 13/14
  42. JPEG algorithm • 4. Quantization • Divide each DCT coefficient

    by a quantization value. • Round to the nearest integer. • Reduces the number of bits needed to store the coefficients. • The quantization matrix is chosen based on human visual perception. The human eye is more sensitive to changes in low frequencies (smooth areas) and less sensitive to high frequencies (sharp details). A quality factor (Q-factor) can be applied to scale the entire matrix. Larger quantization steps, more zero coefficients, and smaller file sizes. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 13/14
  43. JPEG algorithm • 5. Coding • Apply Huffman coding or

    Arithmetic coding. • Further reduces the size of the data. • Takes advantage of the statistical properties of the quantized coefficients. • The values are arranged in a zig-zag order to group similar coefficients together, which improves compression. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 13/14
  44. JPEG algorithm • 6. Decoding Process • Reverse the compression

    steps to reconstruct the image. • Entropy Decoding. • Dequantization. • Inverse DCT. • Upsampling. • Color Space Conversion (YCbCr to RGB). NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 13/14
  45. Project 1 • Perform a full implementation of the JPEG

    compression algorithm and the first one based on the DWT2. Analyze each step and evaluate the results of the compression. NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama ([email protected]) 14/14