7

31
Mulitimedia Computing Online Lecture-6 Learning Objectives: Data Compression Text Compression Huffman Coding LZW Coding Arithmetic Coding Image Compression GIF JPEG Standard JPEG 2000 Solution to Some Problems Some Recommended Problems Instructor-in-Charge Dr. Mukesh Kumar Rohil WILPD, BITS, Pilani Rajasthan Monday, June 6, 2022 1 WILPD, B.I.T.S., PILANI EA ZC473 Multimedia Computing On-Line Lecture-6

Transcript of 7

Page 1: 7

Mulitimedia ComputingOnline Lecture-6

Learning Objectives:

• Data Compression– Text Compression

• Huffman Coding • LZW Coding• Arithmetic Coding

– Image Compression• GIF• JPEG Standard• JPEG 2000

• Solution to Some Problems• Some Recommended Problems

Instructor-in-Charge

Dr. Mukesh Kumar Rohil

WILPD, BITS, PilaniRajasthan

Saturday, April 8, 2023 1WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-6

Page 2: 7

Data Compression

• Why data compression?• Why we are able to

compress?• Lossless compression• Lossy compression• Source Coding• Entropy Coding• Hybrid Coding

• Run-Length Coding• Huffman Coding• LZW Coding• Arithmetic coding• Image Compression

– JPEG (details)– JPEG 2000 (Overview)

* Video Compression

Saturday, April 8, 2023 2WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-6

Page 3: 7

3

Q1. Find Huffman codes and compression ratio (C.R.) for Table 1, assuming that uncompressed representation takes 8-bit per character and assume that size of Huffman table is not part of the compressed size.

Table 1:

Char A B C D E F G H

Freq 90 60 50 20 12 8 7 3

Huffman Codes:

A B C D E F G H

00 01 10 111 1101 11001 110000 110001

11 10 01 000 0010 00110 001111 001110

Saturday, April 8, 2023WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-6

Page 4: 7

Huffman Coding - ExampleHuffman Tree

250 / \ 150 100 / \ / \ A B C 50 / \ 30 D / \ 18 E / \ 10 F / \ G H

Saturday, April 8, 2023WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-64

Char A B C D E F G H

Freq 90 60 50 20 12 8 7 3

Huff-manCode

00 01 10 111 1101 11001 110000 110001

C.R. = (250*8) / (2*90 + 2*60 + 2*50 + 3*20 + 4*12 + 5*8 + 6*7 + 6*3) = 3.29

Page 5: 7

5

Q2. Find Huffman code-words and the achieved compression ratio using Huffman coding, for the data given in Table 2. Assume originally a character is stored using 8-bit.

Table 2:

Code:

00 01 11 1000 1001 10100 10101 10110 10111

Char C E T B V S U N R

Freq 130 120 58 30 22 10 9 8 5

C.R. = (392*8) / (2*130 + 2*120 + 2*58 + 4*30 + 4*22 + 5*10 + 5*9 + 5*8 + 5*5) = 3.187

Saturday, April 8, 2023WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-6

Page 6: 7

Decompression - Huffman Codes

Compress DEAF using above Huffman Codes.111 1101 00 11001

Decompress 110001 1101 00 111Ans.: HEAD

Saturday, April 8, 2023WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-66

A B C D E F G H

00 01 10 111 1101 11001 110000 110001

11 10 01 000 0010 00110 001111 001110

Page 7: 7

More about Huffman Coding

• Prefix property of Huffman Codes• Two passes to scan data to compress:

– Pass1: Generation of Frequency Table– Pass2: Compression

* Problems when an error is erroneous

Saturday, April 8, 2023WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-67

Page 8: 7

8

Reference: A simplified approach to IMAGE Processing by Randy Crane

Pseudo-code for LZW Compression

initialize table with single character stringsSTRING = first input characterWhile not end of input stream CHARACTER = next input character If STRING + CHARACTER is in the string table STRING = STRING + CHARACTER Else output the code for STRING add STRING + CHARACTER to the string table STRING = CHARACTER End IfEnd Whileoutput code for STRING

Saturday, April 8, 2023WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-6

Page 9: 7

9

Example of LZW Compression• Compress: babaabaaa• Originally 7-bit representation• Find StringTable, Output and

Compression Ratio• Solution:• As originally 7-bit representation,

codeword starts from 8-bit (128 onwards)

• Apply algorithm given in the last slide to generate the following:

• StringTable = [ba,ab,baa,aba,aa] ,• Codes = [128 for ba, 129 for ab,

130 for baa, 131 for aba, 132 for aa]

• OutPutTable = [98 for b, 97 for a, 128 for ba, 129 for ab, 97 for a, 130 for aa]

• Compression Ratio:• Original size = 9 x 7 bits = 63 bits• Compressed size = entries in the

output table x 8 bits = 5 x 8 = 40 bits

• Compression Ratio = (Original Size / Compressed

Size) = 63/40 = 1.575

Saturday, April 8, 2023WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-6

Page 10: 7

10

Pseudo-code for LZW Decompression

initialize table with single character stringsOLD_CODE = first input characteroutput translation of OLD_CODEWhile not end of input stream NEW_CODE = next input character If NEW_CODE is not in the string table STRING = translation of OLD_CODE STRING = STRING + CHARACTER Else STRING = translation of NEW_CODE End If output STRING CHARACTER = first character of STRING add OLD_CODE + CHARACTER to the string table OLD_CODE = NEW_CODEEnd While

Saturday, April 8, 2023WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-6

Page 11: 7

11

Reference: A simplified approach to IMAGE Processing by Randy Crane

Pseudo-code for Arithmetic Compression

LOW = 0.0HIGH = 1.0While not end of input stream get next CHARACTER RANGE = HIGH – LOW HIGH = LOW + RANGE * high range of CHARACTER LOW = LOW + RANGE * low range of CHARACTEREnd Whileoutput LOW

Saturday, April 8, 2023WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-6

Page 12: 7

12

Example of Arithmetic Coding• Compress: babaabaaa• Originally 8-bit representation• Floating Point is represented by say 64

bits• Find Compressed Code and Compression

Ratio

• Solution:• Find frequency, Cummulative Frequency

and RangesChar Freq Cumm LowRange HighRangeA 6 6 0 6/9B 3 9 6/9 9/9

• Apply algorithm given in the last slide to generate the following output. (Simulate the algorithms for the given number of iterations or until it terminates when no more characters)

OldLow-OldHigh/ Character/Range/NewLow-NewHigh

0 - 1 / b / 1 / 0.667 - 10.667 - 1 / a / 0.333 / 0.667 - 0.8890.667 - 0.889 / b / 0.222 / 0.815 - 0.8890.815 - 0.889 / a / 7.41E-002 / 0.815 - 0.8640.815 - 0.864 / a / 4.94E-002 / 0.815 - 0.8480.85 - 0.848 / b / 3.29E-002 / 0.837 - 0.8480.837 - 0.848 / a / 1.10E-002 / 0.837 - 0.8440.837 - 0.844 / a / 7.32E-003 / 0.837 - 0.8420.837 - 0.842 / a / 4.88E-003 / 0.837 - 0.840

Coded = 0.837

• Compression Ratio:Original size = 72 bits, Compressed size = 64 bitsCompression Ratio = (72 / 64) = 1.125

Saturday, April 8, 2023WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-6

Page 13: 7

13

Pseudo-code for Arithmetic Decompression

get NUMBERDo find CHARACTER that has HIGH > NUMBER and LOW < NUMBER set HIGH and LOW corresponding to CHARACTER output CHARACTER RANGE = HIGH – LOW NUMBER = NUMBER – LOW NUMBER = NUMBER / RANGEUntil no more CHARACTERs

Page 14: 7

Discrete Cosine Transform (DCT)

Equation for DCT :

2 M-1 N-1 (2x+1)u ╥ (2y+1)v ╥ H( u,v) = ——— C(u) C(v) ∑ ∑ h(x,y) cos ———— cos ———— √( MN) x=0 y=0 2M 2N

Where 1/ √2 for =0 C()= 1 for >0

M x N = Size of the block

Page 15: 7

Reverse Discrete Cosine Transform

Equation for this : 2 M-1 N-1 (2x+1)u ╥ (2y+1)v ╥ h(x,y) = ——— ∑ ∑ C(u) C(v) H(u,v) cos ———— cos ———— √( MN) u=0 v=0 2M 2N

Where

1/ √2 for =0 C( )= 1 for >0

M x N = Size of the block

Page 16: 7

Data Compression

• JPEG– DCT based mode

(Sequential coding)– Expanded Lossy DCT-

based Mode (Progressive encoding)

– Lossless Mode– Hierarchical Mode

• MPEG Coding• Types of frames

– I, P, B and D frames– Motion estimation

Audio Coding (T1.Ch6)– PCM– DPCM– ADPCM– -μ-Law

Page 17: 7

JPEG Compression Standard• JPEG is an image compression standard that was developed by the \Joint

Photographic Experts Group". JPEG was formally accepted as an international standard in 1992.

• JPEG is a lossy image compression method. It employs a transform coding method using the DCT (Discrete Cosine Transform).

• An image is a function of i and j (or conventionally x and y) in the spatial domain.

• The 2D DCT is used as one step in JPEG in order to yield a frequency response which is a function F(u; v) in the spatial frequency domain, indexed by two integers u and v.

Source: Li & Drew c Prentice Hall 2003

Page 18: 7

JPEG Compression Standard … 2

• Observations for JPEG Image Compression• The effectiveness of the DCT transform coding

method in JPEG relies on 3 major observations:• Observation 1: Useful image contents change

relatively slowly across the image, i.e., it is unusual for intensity values to vary widely several times in a small area, for example, within an 88 image block. much of the information in an image is repeated, hence spatial redundancy".

Page 19: 7

JPEG Compression Standard … 3

• Observation 2: Psychophysical experiments suggest that humans are much less likely to notice the loss of very high spatial frequency components than the loss of lower frequency components. The spatial redundancy can be reduced by largely reducing the high spatial frequency contents.

• Observation 3: Visual acuity (accuracy in distinguishing closely spaced lines) is much greater for gray (\black and white") than for color.

• chroma subsampling (4:2:0) is used in JPEG.

Page 20: 7

JPEG Compression Standard … 4

Page 21: 7

JPEG Compression Standard … 5

Main Steps in JPEG Image Compression• Transform RGB to YIQ or YUV and subsample

color.• DCT on image blocks.• Quantization.• Zig-zag ordering and run-length encoding.• Entropy coding.

Page 22: 7

JPEG Compression Standard … 6

• DCT on image blocks• Each image is divided into 8 8 blocks. The 2D DCT is

applied to each block image f(i; j), with output being the DCT cofficients F(u; v) for each block.

• Using blocks, however, has the effect of isolating each block from its neighboring context. This is why JPEG images look choppy (\blocky") when a high compression ratio is specified by the user. (Quality Vs Compression)

Page 23: 7

JPEG Compression Standard … 7• Quantization• ^ F(u; v) = roundF(u; v)/Q(u;

v) (9:1)• F(u; v) represents a DCT

coecient, Q(u; v) is a quantization

• matrix" entry, and ^ F(u; v) represents the quantized DCT cofficients which JPEG will use in the succeeding entropy coding.

• { The quantization step is the main source for loss in JPEG

compression.

• { 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 | (due to Observations 1 and 2).

• { Table 9.1 and 9.2 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.

Page 24: 7

JPEG Compression Standard … 8

Page 25: 7

JPEG Compression Standard … 9

Page 26: 7

JPEG Compression Standard … 10

Page 27: 7

JPEG Compression Standard … 12

Page 28: 7

JPEG Compression Standard … 13

• DPCM on DC coffiecients• The DC cofficients are coded separately from the AC

ones.• Differential Pulse Code Modulation (DPCM) is the

coding• method.• If the DC cofficients for the 5 image blocks are 150,155, 149, 152, 144, then the DPCM would produce 150,

5, -6, 3, -8, assuming di = DCi+1 − DCi, and d0=DC0.

Page 29: 7

JPEG Compression Standard … 14• Entropy Coding• The DC and AC cofficients usually undergo an entropy coding

step to gain a possible further compression.• Use DC as an example: each DPCM coded DC coecient is

represented by (SIZE, AMPLITUDE), where SIZE indicates how many bits are needed for representing the cofficient,

• and AMPLITUDE contains the actual bits.• In the example we're using, codes 150, 5, −6, 3, −8 will be

turned into (8, 10010110), (3, 101), (3, 001), (2, 11), (4, 0111) .• SIZE is Human coded since smaller SIZEs occur much more

often. AMPLITUDE is not Human coded, its value can change widely so Human coding has no appreciable benefit.

Page 30: 7

JPEG Compression Standard … 15

Page 31: 7

Thanks ….

Any questions please …..

Thanking you all

Saturday, April 8, 2023 31WILPD, B.I.T.S., PILANI EA ZC473

Multimedia Computing On-Line Lecture-6