JPEG Image Compression: - K. M. Aishwarya
JPEG Image Compression: - K. M. Aishwarya
JPEG Image Compression: - K. M. Aishwarya
- K. M. Aishwarya
What is Image Compression?
Down-
JPEG Decompression
Decoding Up-sampling
sampling
JPEG Compression
Compressed Colour
Forward DCT
image data Transform
Quantization Encoding
Algorithm
• Splitting: Split the image into 8 x 8 non-overlapping pixel blocks. If the
image cannot be divided into 8-by-8 blocks, then you can add in empty
pixels around the edges, essentially zero-padding the image.
• Colour Space Transform from [R,G,B] to [Y,Cb,Cr] & Subsampling.
• DCT: Take the Discrete Cosine Transform (DCT) of each 8-by-8 block.
• Quantization: quantize the DCT coefficients according to psycho-visually
tuned quantization tables.
• Serialization: by zigzag scanning pattern to exploit redundancy.
• Vectoring: Differential Pulse Code Modulation (DPCM) on DC components
• Run Length Encoding (RLE) on AC components
• Entropy Coding:
– Run Length Coding
– Huffman Coding or Arithmetic Coding
Step I - Splitting
The input image is divided into smaller blocks having 8 x 8
dimensions, summing up to 64 units in total. Each of these units
is called a pixel, which is the smallest unit of any image.
MCU with
sampling factor
8x8 pixel (1, 1, 1)
1 pixel = 3 components
4 blocks
16 x16 pixel
MCU with
sampling
factor
(2, 1, 1)
Why DCT?
• Human vision is insensitive to high frequency components, due to which it
is possible to treat the data corresponding to high frequencies as
redundant. To segregate the raw image data on the basis of frequency, it
needs to be converted into frequency domain, which is the primary
function of DCT.
DCT Formula
• 1-D DCT –
• But the image matrix is a 2-D matrix. So we can either apply 1-D DCT to
the image matrix twice. Once row-wise, then column wise, to get the DCT
coefficients. Or we can apply the standard 2-D DCT formula for JPEG
compression. If the input matrix is P(x,y) and the transformed matrix is
F(u,v) or G(u,v) then the DCT for the 8 x 8 block is computed using the
expression:-
• 2-D DCT –
DC and AC components
(8x8) (8x8)
P(x,y) F(u,v)
• For u = v = 0 the two cosine terms are 0 and hence the value in the location F[0,0]
of the transformed matrix is simply a function of the summation of all the values
in the input matrix.
• This is the mean of all 64 values in the matrix and is known as the DC coefficient.
• Since the values in all the other locations of the transformed matrix have a
frequency coefficient associated with them they are known as AC coefficients.
Step IV - Quantization
• Humans are unable to see aspects of an image that are at really high
frequencies. Since taking the DCT allows us to isolate where these high
frequencies are, we can take advantage of this in choosing which values to
preserve. By multiplying the DCT matrix by some mask, we can zero out
elements of the matrix, thereby freeing the memory that had been
representing those values.
• The resultant quantize matrix will only preserve values at the lowest
frequencies up to a certain point.
• Why Quantization?
– To reduce the number of bits per sample.
• Two types:
– Uniform quantization
• q(u,v) is a constant
– Non-uniform quantization
• Custom quantization tables can be put in image/scan header.
• JPEG Standard defines two default quantization tables, one each for
luminance and chrominance.
Quantization
𝐅(𝐮,𝐯)
Standard Formula: 𝑭 𝐮, 𝒗 = 𝒓𝒐𝒖𝒏𝒅
𝐐(𝐮,𝐯)
12 12 14 19 26 58 60 55 18 21 26 66 99 99 99 99
14 13 16 24 40 57 69 56 24 26 56 99 99 99 99 99
14 17 22 29 51 87 80 62 47 66 99 99 99 99 99 99
18 22 37 56 68 109 103 77 99 99 99 99 99 99 99 99
24 35 55 64 81 104 113 92 99 99 99 99 99 99 99 99
• The entries of Q(u,v) tend to have larger values towards the lower right corner. This
aims to introduce more loss at the higher spatial frequencies.
• The tables above show the default Q(u,v) values obtained from psychophysical
studies with the goal of maximizing the compression ratio while minimizing
perceptual losses in JPEG images.
Quantization - Example
160 80 47 39 5 3 0 0 16 7 4 2 0 0 0 0
65 53 48 8 5 2 0 0 5 4 3 0 0 0 0 0
58 34 6 4 2 0 0 0 4 2 0 0 0 0 0 0
40 7 6 1 1 0 0 0 0 0 0 0 0 0 0 0
8 4 0 0 0 0 0 0 Quantizer 0 0 0 0 0 0 0 0
5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
16 11 10 16 24 40 51 61
12 12 14 19 26 58 60 55
14 13 16 24 40 57 69 56 Each element of F(u,v) is divided
14 17 22 29 51 87 80 62 by the corresponding element of
18 22 37 56 68 109 103 77 Q(u,v) and then rounded off to
24 35 55 64 81 104 113 92
the nearest integer to get the
F(u,v) matrix.
49 64 78 87 103 121 120 101
72 92 95 98 112 100 103 99
……. 0 0 0 6 0 0 0 0 0 1 …
(1x64)
(3,6) (5,1)
RLE Examples
Example 1-
Example 2-
• If the DC component is 48, and the previous DC component is 52. Then the
difference between the 2 components is (48-52) = -4.
• Therefore it is Huffman coded as: 100011
• 011: The codes for representing -4 (Table 1.)
• 100: The size of the value code word is 3. From Table 2, code
corresponding to 3 is 100.
Huffman Coding: AC Components
• AC components are coded in two parts:
– Part1: (RunLength/SIZE)
• RunLength: The length of the consecutive zero values [0..15]
• SIZE: The number of bits needed to code the next nonzero AC component’s value. [0-A]
• (0,0) is the End Of Block (EOB) for the 8x8 block.
• Part1 is Huffman coded (see Table 3)
– Part2: (Value)
• Value: Is the actual value of the AC component.(Table 1)
• Works with colour and grayscale images, but not with binary images.
• Up to 24 bit colour images (Unlike GIF)
• Target photographic quality images (Unlike GIF)
• Suitable for many applications e.g., satellite, medical, general
photography, etc.
Thank you