0% found this document useful (0 votes)
209 views31 pages

Mulitimedia Computing: Online Lecture-6 Instructor-in-Charge Dr. Mukesh Kumar Rohil

The document discusses various data compression techniques including lossless compression methods like Huffman coding, LZW coding, and arithmetic coding. It provides examples and pseudocode to demonstrate how these compression algorithms work. It also discusses lossy image compression standards like JPEG and JPEG 2000. The document is a lecture on data compression topics for a multimedia computing course.

Uploaded by

a xc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views31 pages

Mulitimedia Computing: Online Lecture-6 Instructor-in-Charge Dr. Mukesh Kumar Rohil

The document discusses various data compression techniques including lossless compression methods like Huffman coding, LZW coding, and arithmetic coding. It provides examples and pseudocode to demonstrate how these compression algorithms work. It also discusses lossy image compression standards like JPEG and JPEG 2000. The document is a lecture on data compression topics for a multimedia computing course.

Uploaded by

a xc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Mulitimedia Computing

Online Lecture-6 Instructor-in-Charge


Learning Objectives:

• Data Compression Dr. Mukesh Kumar Rohil


– Text Compression
• Huffman Coding
• LZW Coding
• Arithmetic Coding
– Image Compression
• GIF
• JPEG Standard
• JPEG 2000
• Solution to Some Problems
• Some Recommended Problems WILPD, BITS, Pilani
Rajasthan

WILPD, B.I.T.S., PILANI EA ZC473


Wednesday, December 8, 2021 1
Multimedia Computing On-Line Lecture-6
Data Compression
• Why data compression? • Run-Length Coding
• Why we are able to • Huffman Coding
compress? • LZW Coding
• Lossless compression • Arithmetic coding
• Lossy compression • Image Compression
• Source Coding – JPEG (details)
• Entropy Coding – JPEG 2000 (Overview)
• Hybrid Coding * Video
Compression
WILPD, B.I.T.S., PILANI EA ZC473
Wednesday, December 8, 2021 2
Multimedia Computing On-Line Lecture-6
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

WILPD, B.I.T.S., PILANI EA ZC473


Wednesday, December 8, 2021 3
Multimedia Computing On-Line Lecture-6
Huffman Coding - Example
Huffman Tree

250
/ \ Char A B C D E F G H
150 100 Freq 90 60 50 20 12 8 7 3
/ \ / \
A B C 50 00 01 10 111 1101 11001 110000 110001

/ \ Huff-
30 D man
/ \ Code
18 E
/ \
10 F
/ \
G H

C.R. = (250*8) / (2*90 + 2*60 + 2*50 + 3*20 + 4*12 + 5*8 + 6*7 + 6*3) = 3.29
WILPD, B.I.T.S., PILANI EA ZC473
Wednesday, December 8, 2021 4
Multimedia Computing On-Line Lecture-6
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:

Char C E T B V S U N R

Freq 130 120 58 30 22 10 9 8 5

Code:

00 01 11 1000 1001 10100 10101 10110 10111

C.R. = (392*8) / (2*130 + 2*120 + 2*58 + 4*30 + 4*22 + 5*10 + 5*9 + 5*8 + 5*5) = 3.187
WILPD, B.I.T.S., PILANI EA ZC473
Wednesday, December 8, 2021 5
Multimedia Computing On-Line Lecture-6
Decompression - 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

Compress DEAF using above Huffman Codes.


111 1101 00 11001

Decompress 110001 1101 00 111


Ans.: HEAD

WILPD, B.I.T.S., PILANI EA ZC473


Wednesday, December 8, 2021 6
Multimedia Computing On-Line Lecture-6
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

WILPD, B.I.T.S., PILANI EA ZC473


Wednesday, December 8, 2021 7
Multimedia Computing On-Line Lecture-6
Reference: A simplified approach to IMAGE Processing
by Randy Crane

Pseudo-code for LZW Compression

initialize table with single character strings


STRING = first input character
While 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 If
End While
output code for STRING
WILPD, B.I.T.S., PILANI EA ZC473
Wednesday, December 8, 2021 8
Multimedia Computing On-Line Lecture-6
Example of LZW Compression
• Compress: babaabaaa • OutPutTable = [98 for b, 97 for a,
• Originally 7-bit representation 128 for ba, 129 for ab, 97 for a,
• Find StringTable, Output and 130 for aa]
Compression Ratio • Compression Ratio:
• Solution: • Original size = 9 x 7 bits = 63 bits
• As originally 7-bit representation, • Compressed size = entries in the
codeword starts from 8-bit (128 output table x 8 bits = 5 x 8 = 40
onwards) bits
• Apply algorithm given in the last • Compression Ratio =
slide to generate the following: (Original Size / Compressed
• StringTable = [ba,ab,baa,aba,aa] , Size)
• Codes = [128 for ba, 129 for ab, = 63/40 = 1.575
130 for baa, 131 for aba, 132 for
aa]

WILPD, B.I.T.S., PILANI EA ZC473


Wednesday, December 8, 2021 9
Multimedia Computing On-Line Lecture-6
Pseudo-code for LZW Decompression
initialize table with single character strings
OLD_CODE = first input character
output translation of OLD_CODE
While 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_CODE
End While WILPD, B.I.T.S., PILANI EA ZC473
Wednesday, December 8, 2021 10
Multimedia Computing On-Line Lecture-6
Reference: A simplified approach to IMAGE Processing
by Randy Crane

Pseudo-code for Arithmetic Compression

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

WILPD, B.I.T.S., PILANI EA ZC473


Wednesday, December 8, 2021 11
Multimedia Computing On-Line Lecture-6
Example of Arithmetic Coding
• Compress: babaabaaa OldLow-OldHigh/ Character/Range/NewLow-
• NewHigh
Originally 8-bit representation
0 - 1 / b / 1 / 0.667 - 1
• Floating Point is represented by say 64 0.667 - 1 / a / 0.333 / 0.667 - 0.889
bits
0.667 - 0.889 / b / 0.222 / 0.815 - 0.889
• Find Compressed Code and Compression 0.815 - 0.889 / a / 7.41E-002 / 0.815 - 0.864
Ratio
0.815 - 0.864 / a / 4.94E-002 / 0.815 - 0.848
0.85 - 0.848 / b / 3.29E-002 / 0.837 - 0.848
• Solution: 0.837 - 0.848 / a / 1.10E-002 / 0.837 - 0.844
• Find frequency, Cummulative Frequency 0.837 - 0.844 / a / 7.32E-003 / 0.837 - 0.842
and Ranges 0.837 - 0.842 / a / 4.88E-003 / 0.837 - 0.840
Char Freq Cumm LowRange HighRange
A 6 6 0 6/9 Coded = 0.837
B 3 9 6/9 9/9
• Compression Ratio:
Original size = 72 bits, Compressed size = 64 bits
• Apply algorithm given in the last slide to Compression Ratio = (72 / 64) = 1.125
generate the following output. (Simulate
the algorithms for the given number of
iterations or until it terminates when no
more characters)

WILPD, B.I.T.S., PILANI EA ZC473


Wednesday, December 8, 2021 12
Multimedia Computing On-Line Lecture-6
Pseudo-code for Arithmetic Decompression

get NUMBER
Do
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 / RANGE
Until no more CHARACTERs

13
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


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


Data Compression
• JPEG • MPEG Coding
– DCT based mode • Types of frames
(Sequential coding) – I, P, B and D frames
– Expanded Lossy DCT- – Motion estimation
based Mode
(Progressive encoding) Audio Coding (T1.Ch6)
– Lossless Mode – PCM
– Hierarchical Mode – DPCM
– ADPCM
– -μ-Law
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


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".
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.
JPEG Compression Standard … 4
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.
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)
JPEG Compression Standard … 7
• Quantization compression.
• ^ F(u; v) = roundF(u; v)/Q(u;
• { The entries of Q(u; v) tend to
v) (9:1) have larger values towards the
• F(u; v) represents a DCT lower right corner. This aims to
coecient, Q(u; v) is a introduce more loss at the higher
spatial frequencies | (due to
quantization Observations 1 and 2).
• matrix" entry, and ^ F(u; v)
represents the quantized DCT • { Table 9.1 and 9.2 show the
cofficients which JPEG will default Q(u; v) values obtained
from psychophysical studies with
use in the succeeding entropy the goal of maximizing the
coding. compression ratio while
• { The quantization step is the minimizing perceptual losses in
JPEG images.
main source for loss in JPEG
JPEG Compression Standard … 8
JPEG Compression Standard … 9
JPEG Compression Standard … 10
JPEG Compression Standard … 12
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.
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.
JPEG Compression Standard … 15
Thanks ….

Any questions please …..

Thanking you all


WILPD, B.I.T.S., PILANI EA ZC473
Wednesday, December 8, 2021 31
Multimedia Computing On-Line Lecture-6

You might also like