You are on page 1of 38

Digital Image Processing, 2nd ed.

1
www.imageprocessingbook.com

Chapter 8
Image
Compression

www.ImageProcessingPlace.com

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 2
www.imageprocessingbook.com

Why Compression?
• Storage
– Image data is by its nature multidimensional and tend
to take up a lot of space
• Computational expenses
– Encryption of data is computationally expensive
• Transmission time & costs
– ISPs charge by the data amount, phone companies by
the time unit
• Limited hardware resources (memory, CPU etc)
– Cell phones with cameras, USB memory sticks
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 3
www.imageprocessingbook.com

Why Compression?
We need maximum utilization of storage devices & quick
transmission.

Example: To transmit an RGB 512 X 512, 24 bit image


via modem 28.8 kbaud (kilobits/second)

(512  512 pixels)(24 bits / pixel )


 213 second
(28.8  1024 bits / second)

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 4
www.imageprocessingbook.com

Compression Types
The fundamental task of image compression is to reduce
the amount of data required to represent the image. This
is done by transforming and removing image data
redundancies

There are two major categories of compression algorithms:


• Lossless compression algorithms
• Lossy compression algorithms

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 5
www.imageprocessingbook.com

Compression Types
• Lossless compression algorithms
The original data is reconstructed perfectly
Theoretical limit on maximal compression
Practical compression ratios of <10 : 1 (still images)
• Lossy compression algorithms
Decompression results in an approximation of the
original image
Maximal compression rate is a function of
reconstruction quality
Practical compression ratios of >10 : 1 (still images)
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 6
www.imageprocessingbook.com

Data Usage Patterns


Symmetric applications
Data is typically compressed & decompressed the same number of times
Compression and decompression involves roughly the same computations
Video telephony
Asymmetric applications
Data is typically compressed once and decompressed many times
Compression computationally expensive, decompression in real-time
Offline scene rendering in a computer game or movie
Video decoding and rendering - 1/30 seconds round trip time
Time dependent applications
Data is typically time resolved and may be skipped upon failure
Video transmission over networks with packet loss

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 7
www.imageprocessingbook.com

Image Compression

• Reducing the size of image data files


• While retaining necessary information

Original Compressed extracted


Image compress Image file decompress Image file

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 8
www.imageprocessingbook.com

Image Compression

 refer relation between original image and


the compressed file

1. Compression Ratio
A larger number implies a better compression
2. Bits per Pixel
A smaller number implies a better compression

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 9
www.imageprocessingbook.com

Background
>> b = imread(‘bubbles.tif’);
>> imwrite(b,'bubbles.png')
>> imwrite(b,'bubbles.jpg')
>> imwrite(b,'bubbles', 'jpg')
>> imwrite(b,'bubbles.jpg','quality',50)

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 10
www.imageprocessingbook.com

Background
Compression Ratio: CR = n1 / n2
where n1 and n2 denote the umber of information carrying units (usually bits)
in the original image f(x, y) and encoded image f^(x, y), resp.

Bits Per Pixel: Number of Bits / Number of Pixels


Root-Mean-Square Error:
  fˆ ( x, y)  f ( x, y)
M 1 N 1 2
1
erms 
MN x 0 y 0

Lossless Compression: if erms = 0


Lossy Compression: if erms  0
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 11
www.imageprocessingbook.com

Compression Ratio
Example: Image 256 X 256 pixels, 256 level grayscale can be
compressed file size 6554 byte.

Original Image Size = 256 X 256 (pixels) X 1 (byte/pixel)


= 65536 bytes

65536
Compressio n Ratio   10
6554

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 12
www.imageprocessingbook.com

Bits Per Pixel


Example: Image 256 X 256 pixels, 256 level grayscale can
be compressed file size 6554 byte.

Original Image Size = 256 X 256 (pixels) X 1 (byte/pixel)


= 65536 bytes
Compressed file = 6554(bytes) X 8 (bits/pixel)
= 52432 bits
52432
Bits per Pixel   0.8
65536

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 13
www.imageprocessingbook.com

Redundancy vs Compression Ratio

• Reducing Data but Retaining Information


DATA are used to convey information.
Various amounts of data can be used to represent the
same amount of information. It’s “Data redundancy”
Relative data redundancy
1
RD  1
CR

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 14
www.imageprocessingbook.com

Redundancy Types
• Coding Redundancy
• Interpixel Redundancy
• Psychovisual Redundancy

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 15
www.imageprocessingbook.com

Redundancy Types
• Coding Redundancy
– Which is present when less than optimal code
words are used
– It is due to inefficiencies in data representations

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 16
www.imageprocessingbook.com

Redundancy Types
• Interpixel redundancy
– It is defined as failure to identify and utilize data relationships
– If a pixel value can be reasonably predicted from its
neighboring (or preceeding / following) pixels the image is said
to contain interpixel redundancy
– Interpixel redundancy depends on the resolution of the image
o The higher the (spatial) resolution of an image, the more probable it is
that two neighboring pixels will depict the same object
o The higher the frame rate in a video stream, the more probable it is that
the corresponding pixel in the following frame will depict the same
object
These types of predictions are made more difficult by the presence of noise
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 17
www.imageprocessingbook.com

Redundancy Types
• Psychovisual Redundancy
– Which is due to data that is ignored by the human
visual system
– Some information is more important to the human
visual system than other types of information

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 18
www.imageprocessingbook.com

Redundancy Types
• Psychovisual Redundancy
– Psychovisual redundancy stem from the fact that the human eye does not
respond with equal intensity to all visual information
– The human visual system does not rely on quantitative analysis of individual
pixel values when interpreting an image – an observer searches for distinct
features and mentally combines them into recognizable groupings
– In this process certain information is relatively less important than other – this
information is called psychovisually redudant
– Psychovisually redundant image information can be identified and removed –
a process referred to as quantization
– Quantization eliminates data and therefore results in lossy data compression
– Reconstruction errors introduced by quantization can be evaluated objectively
or subjectively depending on the application need & specifications

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 19
www.imageprocessingbook.com

Redundancy Types
Quantization Effects

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 20
www.imageprocessingbook.com

Compression System Model

• Compression
Compressed
Compressed
Input
Input Preprocessing
Preprocessing Encoding
Encoding File
File

• Decompression
Compressed
Compressed Decoding
Decoding Postprocessing
Postprocessing Output
Output
File
File

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 21
www.imageprocessingbook.com

Compression System Model

The channel encoder inserts a form of “controlled


redundancy” (e.g. Hamming codes or parity bits) in the data
representation in order to detect and / or correct errors
introduced by channel noise
This increases the size of the data representation and if the
channel is noise free (i.e. not prone to errors) the channel
encoder and decoder are omitted
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 22
www.imageprocessingbook.com

Compression System Model

Mapper - transforms the image to a (non-visual) format designed to reduce interpixel


redundancies
Quantizer - reduces psychovisual redundancies by quantizing data deemed less
important for visual interpretation (omitted for lossless compression)
Symbol encoder - codes the data efficiently (typically using some form of variable-
length coding scheme) and aims to reduce coding redundancies
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 23
www.imageprocessingbook.com

Coding Redundancy
Pr(rk) = nk / n, k = 1, 2,…, L
L
Lavg   l (rk ) pr (rk ) (Average number of bits per pixel)
k 1
where l(rk) is the number of bits used to represent each value
of rk.

CR = 2 / 1.81 = 1.1, RD = 1 – 1 / CR = 0.09


© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 24
www.imageprocessingbook.com

Coding Redundancy

Exercise:
1. Find CR , RD ?

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 25
www.imageprocessingbook.com

Huffman Coding
• Provides a data representation with the smallest
possible number of code symbols per source
symbol (when coding the symbols of an
information source individually)
• Huffman code construction is done in five steps:
1. Find the histogram probabilities
2. Order the input probabilities (large  small)
3. Add two smallest
4. Repeat steps 2 & 3, until two probabilities are left
5. Backward along the tree by assigning 0 and 1
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 26
www.imageprocessingbook.com

Huffman Coding
 Step 1: Histogram Probability
40
30 p0 = 20/100 = 0.2
20 p1 = 30/100 = 0.3
10 p2 = 10/100 = 0.1
0 1 2 3 p3 = 40/100 = 0.4
 Step 2: Order

p3  0.4
p1  0.3
p0  0.2
p2  0.1
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 27
www.imageprocessingbook.com

Huffman Coding

 Steps 3 & 4: Add 2 smallest


p3  0.4  0.4  0.4 0.6
p1  0.3  0.3 0.6 0.4
p 0  0 .2 0 .3
p 2  0 .1
Natural Code Probability Huffman Code
P0 -> 00 0.2
P1 -> 01 0.3
P2 -> 10 0.1
P3 -> 11 0.4
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 28
www.imageprocessingbook.com

Huffman Coding

 Step 5: Backward along the tree assign 0 and 1


p3  0.4 1  0.4 1  0.4 1 0.6 0
p1  0.3 00  0.3 00 0.6 0 0.4 1
p0  0.2 010 0.3 01
p2  0.1 011
Natural Code Probability Huffman Code
P0 -> 00 0.2 010
P1 -> 01 0.3 00
P2 -> 10 0.1 011
P3 -> 11 0.4 1
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 29
www.imageprocessingbook.com

Huffman Coding
• The original Image : average 2 bits/pixel

• The Huffman Code: average

3
La   li pi  3(0.2)  2(0.3)  3(0.1)  1(0.4)  1.9
i 0

Natural Code Probability Huffman Code (size)


P0 -> 00 0.2 010 (3)
P1 -> 01 0.3 00 (2)
P2 -> 10 0.1 011 (3)
P3 -> 11 0.4 1 (1)
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 30
www.imageprocessingbook.com

Huffman Coding
3
Entropy   pi log 2 ( pi )
i 0

(0.2) log 2 (0.2)  (0.3) log 2 (0.3) 


  
  ( 0 .1) log 2 ( 0 .1)  ( 0 .4 ) log 2 ( 0. 4 ) 
 1.846 bits / pixel

Natural Code Probability Huffman Code


P0 -> 00 0.2 010
P1 -> 01 0.3 00
P2 -> 10 0.1 011
P3 -> 11 0.4 1
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 31
www.imageprocessingbook.com

Coding Redundancy
Huffman Codes – Another Example
Given image:
119 123 168 119 Gray- Code
123 119 168 168 level
119 119 107 119 a1 107
107 107 119 119
a2 119

Histogram: a3 123
3 8 2 3 a4 168

Probability:
0.1875 0.5000 0.1250 0.1875

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 32
www.imageprocessingbook.com

Coding Redundancy
Huffman Codes – Another Example

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 33
www.imageprocessingbook.com

Coding Redundancy
Huffman Codes – Another Example
It contains the smallest possible number of bits per gray-level value.
For our given 4 X 4 image:
Gray- Code
f= level
119 123 168 119
a1 107 011
123 119 168 168
119 119 107 119 a2 119 1
107 107 119 119 a3 123 010
a4 168 00
Fig. 8.2(b) yields the 29-bit string:

1 010 1 011 010 1 1 011 00 00 011 1 1 00 1 1

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 34
www.imageprocessingbook.com

Coding Redundancy
Huffman Codes – Matlab
>> f = repmat([3 3 1 3 3 3 3 3 2 3], 1, 50); % Data to encode

>> symbols = [1 2 3]; % Distinct data symbols appearing in sig

>> h = hist(f(:), 3) % Histogram


h=
50 50 400

>> p = h / sum(h) % Probability of each data symbol


p=
0.1000 0.1000 0.8000

>> dict = huffmandict(symbols, p); % Create the dictionary.


© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 35
www.imageprocessingbook.com

Coding Redundancy
Huffman Codes – Matlab
>> dict{1,:}
ans =
1
ans =
1 1
>> dict{2,:}
ans =
2
ans =
1 0
>> dict{3,:}
ans =
3
ans =
0

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 36
www.imageprocessingbook.com

Coding Redundancy
Huffman Codes – Matlab
>> hf = huffmanenco(f, dict); % Encode the data.
>> df = huffmandeco(hf, dict); % Decode the code.

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 37
www.imageprocessingbook.com

Coding Redundancy
Huffman Codes
Exercise: [2, Problem 8.12]

(a) How many unique Huffman codes are there for a three-symbol
source?
(b) Construct them.

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 38
www.imageprocessingbook.com

References
1. Gonzalez R. C., Woods R. E., Eddins S. L., Digital Image
Processing Using Matlab, Pearson Education, 2006.
2. Gonzalez R. C., Woods R. E., Digital Image Processing,
Pearson Education, 2006.
3. http://www.imageprocessingplace.com/
4. http://www.cs.umu.se/kurser/TDBC30/VT05/

© 2002 R. C. Gonzalez & R. E. Woods

You might also like