Slide 1

Slide 1 text

Digital Image Processing Compression (Week 6) NHSM - 4th year - Fall 2024 - Prof. Mohammed Hachama hachamam@gmail.com http://hachama.github.io/home/

Slide 2

Slide 2 text

Outline Introduction Huffman Coding Transform-Domain Compression JPEG algorithm NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama (hachamam@gmail.com) 2/14

Slide 3

Slide 3 text

Introduction NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama (hachamam@gmail.com) 3/14

Slide 4

Slide 4 text

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 (hachamam@gmail.com) 4/14

Slide 5

Slide 5 text

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 (hachamam@gmail.com) 4/14

Slide 6

Slide 6 text

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 (hachamam@gmail.com) 4/14

Slide 7

Slide 7 text

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 (hachamam@gmail.com) 4/14

Slide 8

Slide 8 text

Huffman Coding NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama (hachamam@gmail.com) 5/14

Slide 9

Slide 9 text

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 (hachamam@gmail.com) 6/14

Slide 10

Slide 10 text

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 (hachamam@gmail.com) 6/14

Slide 11

Slide 11 text

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 (hachamam@gmail.com) 7/14

Slide 12

Slide 12 text

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 (hachamam@gmail.com) 7/14

Slide 13

Slide 13 text

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 (hachamam@gmail.com) 7/14

Slide 14

Slide 14 text

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 (hachamam@gmail.com) 7/14

Slide 15

Slide 15 text

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 (hachamam@gmail.com) 7/14

Slide 16

Slide 16 text

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 (hachamam@gmail.com) 7/14

Slide 17

Slide 17 text

Huffman Coding NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama (hachamam@gmail.com) 7/14

Slide 18

Slide 18 text

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 (hachamam@gmail.com) 8/14

Slide 19

Slide 19 text

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 (hachamam@gmail.com) 8/14

Slide 20

Slide 20 text

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 (hachamam@gmail.com) 8/14

Slide 21

Slide 21 text

Transform-Domain Compression NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama (hachamam@gmail.com) 9/14

Slide 22

Slide 22 text

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 (hachamam@gmail.com) 10/14

Slide 23

Slide 23 text

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 (hachamam@gmail.com) 10/14

Slide 24

Slide 24 text

The Discrete Wavelet Transform 2-Level Haar DWT NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama (hachamam@gmail.com) 10/14

Slide 25

Slide 25 text

The Discrete Wavelet Transform 2-Level Haar DWT NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama (hachamam@gmail.com) 10/14

Slide 26

Slide 26 text

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 (hachamam@gmail.com) 10/14

Slide 27

Slide 27 text

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 (hachamam@gmail.com) 10/14

Slide 28

Slide 28 text

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 (hachamam@gmail.com) 11/14

Slide 29

Slide 29 text

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 (hachamam@gmail.com) 11/14

Slide 30

Slide 30 text

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 (hachamam@gmail.com) 11/14

Slide 31

Slide 31 text

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 (hachamam@gmail.com) 11/14

Slide 32

Slide 32 text

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 (hachamam@gmail.com) 11/14

Slide 33

Slide 33 text

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 (hachamam@gmail.com) 11/14

Slide 34

Slide 34 text

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 (hachamam@gmail.com) 11/14

Slide 35

Slide 35 text

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 (hachamam@gmail.com) 11/14

Slide 36

Slide 36 text

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 (hachamam@gmail.com) 11/14

Slide 37

Slide 37 text

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 (hachamam@gmail.com) 11/14

Slide 38

Slide 38 text

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 (hachamam@gmail.com) 11/14

Slide 39

Slide 39 text

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 (hachamam@gmail.com) 11/14

Slide 40

Slide 40 text

JPEG algorithm NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama (hachamam@gmail.com) 12/14

Slide 41

Slide 41 text

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 (hachamam@gmail.com) 13/14

Slide 42

Slide 42 text

JPEG algorithm • JPEG Codec: JPEG Compression Steps NHSM - 4th year: Digital Image Processing - Compression (Week 6) - M. Hachama (hachamam@gmail.com) 13/14

Slide 43

Slide 43 text

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 (hachamam@gmail.com) 13/14

Slide 44

Slide 44 text

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 (hachamam@gmail.com) 13/14

Slide 45

Slide 45 text

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 (hachamam@gmail.com) 13/14

Slide 46

Slide 46 text

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 (hachamam@gmail.com) 13/14

Slide 47

Slide 47 text

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 (hachamam@gmail.com) 13/14

Slide 48

Slide 48 text

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 (hachamam@gmail.com) 13/14

Slide 49

Slide 49 text

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 (hachamam@gmail.com) 13/14

Slide 50

Slide 50 text

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 (hachamam@gmail.com) 14/14