Lesson09 Image compression - Politecnico di...

60
VIDEO SIGNALS Lossless coding

Transcript of Lesson09 Image compression - Politecnico di...

Page 1: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

VIDEO SIGNALS

Lossless codingg

Page 2: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

LOSSLESS CODINGLOSSLESS CODING

The goal of lossless image compression is to The goal of lossless image compression is to represent an image signal with the smallest possible number of bits without loss of any information, y ,thereby speeding up transmission and minimizing storage requirements. Th b f bi i h i l i i ll The number of bits representing the signal is typically expressed as an average bit rate (average number of bits per sample for still images and average number bits per sample for still images, and average number of bits per second for video).

Page 3: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

LOSSY COMPRESSIONLOSSY COMPRESSION

The goal of lossy compression is to achieve the best possible fidelity given an available best possible fidelity given an available communication or storage bit-rate capacity, or to minimize the number of bits representing the i i l bj ll bl l f image signal subject to some allowable loss of information.

In this way a much greater reduction in bit rate can In this way, a much greater reduction in bit rate can be attained as compared to lossless compression, which is necessary for enabling many real-time y g yapplications involving the handling and transmission of audiovisual information.

Page 4: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

WHY CODING?WHY CODING?

C di t h i i l f th ff ti t i i Coding techniques are crucial for the effective transmission or storage of data-intensive visual information.

In fact, a single uncompressed color image or video frame , g p gwith a medium resolution of 500 x 500 pixels would require 100 s for transmission over an ISDN (Integrated Services Digital Network) link having a capacity of 64,000 bit& (64Digital Network) link having a capacity of 64,000 bit& (64Kbps).

The resulting delay is intolerably large, considering that a delay as small as 1 2 s is needed to conduct an interactive delay as small as 1-2 s is needed to conduct an interactive “slide show,” and a much smaller delay (of the order of 0.1 s) is required for video transmission or playback.

Page 5: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

HOW LOSSLESS IS POSSIBLE?HOW LOSSLESS IS POSSIBLE?

Lossless compression is possible because, in general, there is significant redundancy present in image signals is significant redundancy present in image signals.

This redundancy is proportional to the amount of correlation among the image data samples.

For example, in a natural still image, there is usually a high degree of spatial correlation among neighboring image samples samples.

Also, for video, there is additional temporal correlation among samples in successive video frames. a o g sa p es success e deo a es

In color images there is correlation, known as spectral correlation, between the image samples in the different

l spectral components.

Page 6: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

LOSSY VS LOSSLESSLOSSY VS LOSSLESS

In lossless coding the decoded image data should be In lossless coding, the decoded image data should be identical both quantitatively (numerically) and qualitatively (visually) to the original encoded image.

f Although this requirement preserves exactly the accuracy of representation, it often severely limits the amount of compression that can be achieved to a compression factor of 2 or 3.

In order to achieve higher compression factors,perceptually lossless coding methods attempt to remove redundant as lossless coding methods attempt to remove redundant as well as perceptually irrelevant information;

These methods require that the encoded and decoded images be only visually and not necessarily numerically images be only visually, and not necessarily numerically, identical.

Page 7: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

SO WHY LOSSLESS?SO, WHY LOSSLESS?

Although a higher reduction in bit rate can be achieved with lossy compression, there exist several applications that require lossless coding, such as the compression of digital medical imagery

d f i il i i f bi l i and facsimile transmissions of bitonal images. These applications triggered the development of

l d d f l l i several standards for lossless compression, including the lossless JPEG standard, facsimile compression standards and the JBIG compression compression standards and the JBIG compression standard.

Page 8: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

BASICS OF LOSSLESS IMAGE CODING

The encoder (a) takes as input an image and generates as output a compressed bit stream.

The decoder (b) takes as input the compressed bit stream and recovers the original uncompressed image.

Page 9: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

DIFFERENT LOSSLESS APPROACHESDIFFERENT LOSSLESS APPROACHES

Lossless compression is usually achieved by using variable Lossless compression is usually achieved by using variable length codewords, where the shorter codewords are assigned to the symbols that occur more frequently.

This variable-length codeword assignment is known as variable-length coding (VLC) and also as entropy coding.

Entropy coders, such as Huffman and arithmetic coders, Entropy coders, such as Huffman and arithmetic coders, attempt to minimize the average bit rate (average number of bits per symbol) needed to represent a sequence of symbols, based on the probability of symbol occurrence. symbols, based on the probability of symbol occurrence.

An alternative way to achieve compression is to code variable-length strings of symbols using fixed-length binary codewords codewords.

This is the basic strategy behind dictionary (Lempel-Ziv) codes.

Page 10: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

HUFFMAN CODINGHUFFMAN CODING

ff f f Huffman hit upon the idea of using a frequency-sorted binary tree and quickly proved this method the most efficient.

1. Take the two least probable symbols in the 1. Take the two least probable symbols in the alphabet(longest codewords equal length differing in last digit)(longest codewords, equal length, differing in last digit)

C bi h b l i i l b l 2. Combine these two symbols into a single symbol, and repeat.

Page 11: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

HUFFMAN CODING CharacterNumber of

O ( )Percentage

( )HUFFMAN CODING CharacterOccurrences (n) (p)

e 3320 30.5119h 1458 13.3995Character Binary Code

00l 1067 9.8061o 1749 16.0739p 547 5 0271

e 00

h 011

l 110

o 010 p 547 5.0271t 2474 22.7369w 266 2.4446

o 010

p 1110

t 10

w 1111Total: 10881 100

w 266 81310

11 1

w 1111

8131880 4354p 547

l 1067

00

1

1 1

h 1458 32076527o 1749

10

0

10

t 2474e 3320

0

Page 12: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

HUFFMAN DRAWBACKSHUFFMAN DRAWBACKS

H ff di d i h i di i i i Huffman coding and arithmetic coding require a priori knowledge of the source symbol probabilities or of the

i i l d l source statistical model. In some cases, a sufficiently accurate source model is

difficult to obtain, especially when several types of data (such as text, graphics, and natural pictures) are intermixed.

Page 13: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

LEMPEL ZIV LEMPEL-ZIV

Universal coding schemes do not require a priori knowledge or explicit modeling of the source statistics.

A popular lossless universal coding scheme is a A popular lossless universal coding scheme is a dictionary-based coding method developed by Zi d L l d k L l Zi (LZ) Ziv and Lempel and known as Lempel-Ziv (LZ) coding.

Page 14: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

LEMPEL ZIVLEMPEL-ZIV

Dictionary-based coders dynamically build a coding table (called dictionary) of variable-length symbol strings as they ( y) g y g yoccur in the input data.

As the coding table is constructed, fixed length binary codewords are assigned to the variable-length input symbol codewords are assigned to the variable length input symbol strings by indexing into the coding table.

In LZ coding, the decoder can also dynamically reconstruct th di g t bl d th i t th d bit the coding table and the input sequence as the code bits are received without any significant decoding delays.

Although LZ codes do not explicitly make use of the source g p yprobability distribution, they asymptotically approach the source entropy rate for very long sequences.

Page 15: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

LZWLZW

Because of their adaptive nature, dictionary-based Because of their adaptive nature, dictionary based codes are ineffective for short input sequences since these codes initially result in a lot of bits being outputbeing output. So, short input sequences can result in data expansion

instead of compression.p There are several variations of LZ coding.

They mainly differ in how the dictionary is implemented, initialized, updated, and searched.

One popular LZ coding algorithm is known as the Lempel-Ziv-Welch (LZW) algorithm, a version of LZ coding Lempel Ziv Welch (LZW) algorithm, a version of LZ coding developed by Welch.

This is the algorithm used for implementing the compress command in the UNIX operating systemcompress command in the UNIX operating system.

Page 16: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

LZW ALGORITHMLZW ALGORITHM

Let S be the source alphabet consisting 3 Encode w by outputting the index Let S be the source alphabet consisting of N symbols sk (1≤k ≤N)

The basic steps of the LZW algorithm can be stated as follows:

1 Initialize the first N entries of the

3. Encode w by outputting the index (address) of the matching entry as the codeword for w.

4. Add to the dictionary the string ws formed byconcatenating w and the next input 1. Initialize the first N entries of the

dictionary with the individual source symbols of s, as shown:

byconcatenating w and the next input symbol s (following w).

5. Repeat from Step 2 for the remaining input symbols starting with the symbol s, until the entire input sequence is p qencoded.

2. Parse the input sequence and find the longest input string of successive longest input string of successive symbols w (including the first still unencoded symbol s in the sequence) that has a matching entry in the dictionary.

The resulting code is given by the fixed-length binary representation ofthe following sequence of dictionary addresses: 1 2 5 3 6 2.

Page 17: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

LZW DECODERLZW DECODER

The length of the generated binary codewords depends on the The length of the generated binary codewords depends on the maximum allowed dictionary size.

If the maximum dictionary size is M entries, the length of the codewords would be log( M ) rounded to the next smallest integer.

The decoder constructs the same dictionary as the codewords are received. The basic decoding steps can be described as follows.g p

1. Start with the same initial dictionary as the encoder. Also, initialize w to be the empty string.

2. Get the next “codeword‘: and decode it by outputing the symbol string sm t d t dd “ d d” i di tistored at address “codeword” in dictionary.

3. Add to the dictionary the string ws formed by concatenating the previous decoded string w (if any) and the first symbol s of the current decoded string.g

4. Set w = sm and repeat from Step 2 until all the codewords are decoded. Note that the constructed dictionary has a prefix property; i.e.,every

string w in the dictionary has its prefix string

Page 18: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

LZW COMPRESSION ALGORITHMLZW COMPRESSION ALGORITHM

Lempel-Ziv-Welch Algorithm:

w = NIL;w NIL;while ( read a character k )

{if wk exists in the dictionaryw = wk;

elseadd wk to the dictionary;output the code for w;w = k;

}

Argument: ‘ the ‘ requires 5 bytes (80 bits) to encode the requires 5 bytes (80 bits) to encode by assigning a single symbol to it we can express it with 9 bits

Page 19: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

PERCEPTUALLY LOSSLESS CODINGPERCEPTUALLY LOSSLESS CODING

Perceptual based algorithms attempt to discriminate between Perceptual-based algorithms attempt to discriminate between signal components that are and are not detected by the human receiver. Th l i h i l ki i f h They exploit the spatiotemporal masking properties of the human visual system and establish thresholds of justnoticeable distortion based on psychophysical contrast masking phenomena.

The interest is in bandlimited signals because of the fact that visual perception is mediated by a collection of individual visual perception is mediated by a collection of individual mechanisms in the visual cortex, denoted channels or filters, that are selective in terms of frequency and orientation.

Page 20: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

PERCEPTUALLY LOSSLESS CODINGPERCEPTUALLY LOSSLESS CODING

Neurons respond to stimuli above a certain contrast contrast.

The necessary contrast to provoke a response from the neurons is defined as the detection threshold the neurons is defined as the detection threshold.

The inverse of the detection threshold is the contrast sensitivity contrast sensitivity.

Contrast sensitivity varies with frequency (including spatial frequency temporal frequency and spatial frequency, temporal frequency, and orientation) and can be measured using detection experimentsexperiments.

Page 21: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

PERCEPTUALLY LOSSLESSPERCEPTUALLY LOSSLESS

Perceptually lossless image compression (a) Original Lena image, 8 bpp;Perceptually lossless image compression (a) Original Lena image, 8 bpp; (b) decoded Lena image, 0.361 bpp. The perceptual thresholds are computed for a viewing distance equal to 6 times the image height.

Page 22: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

JPEG codingg

Page 23: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

JPEG IMAGE COMPRESSIONJPEG IMAGE COMPRESSION

JPEG: Joint Photographic Experts Group.JPEG i l i h NOT fil f t Th JPEG: compression algorithm, NOT a file format. The

original JPEG format was JFIF and later SPIFF. Current popular choices include C-Cube JFIF; Adobe popular choices include C Cube JFIF; Adobe TIFF/JPEG.

Page 24: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

KEY FEATURESKEY FEATURES

Both sequential and progressive modes of encoding are Both sequential and progressive modes of encoding are permitted.These modes refer to the manner in which quantized DCT coefficients are encoded. In sequential coding, the coefficients are encoded on a block by block basis in a single coefficients are encoded on a block-by-block basis in a single scan that proceeds from left to right and top to bottom.

In contrast, in progressive encoding only partial information about the coefficients is encoded in the first scan followed by encoding the residual information in successive scans.

Low complexity implementations in both hardware and Low complexity implementations in both hardware and software are feasible.

Page 25: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

KEY FEATURESKEY FEATURES

All types of images regardless of source content resolution All types of images, regardless of source, content, resolution, color formats, etc., are permitted.

A graceful tradeoff in bit rate and quality is offered, except at A graceful tradeoff in bit rate and quality is offered, except at very low bit rates.

A hierarchical mode with multiple levels of resolution is allowed.

Bit resolution of 8-12 bits is permitted.A d d fil f t JPEG Fil I t h g F t A recommended file format, JPEG File Interchange Format (JFIF), enables the exchange of JPEG bit streams among a variety of platforms.y p

Page 26: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

GRAY SCALE CODEC SCHEMEGRAY SCALE CODEC SCHEME

Page 27: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

STEPS IN JPEG COMPRESSIONSTEPS IN JPEG COMPRESSION

1. (Optionally) If the color is represented in RGB mode, translate it to YUV.

2 Divide the file into 8 X 8 blocks 2. Divide the file into 8 X 8 blocks. 3. Transform the pixel information from the spatial

domain to the frequency domain with the Discrete C i T f Cosine Transform.

4. Quantize the resulting values by dividing each coefficient by an integer value and rounding off to the coefficient by an integer value and rounding off to the nearest integer.

5. Look at the resulting coefficients in a zigzag order. Do a run length encoding of the coefficients ordered in Do a run-length encoding of the coefficients ordered in this manner. Follow by Huffman coding.

27

Page 28: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

STEP 1A: CONVERTING RGB TO YUVSTEP 1A: CONVERTING RGB TO YUV

YUV color mode stores color in terms of its luminance (brightness) and chrominance (hue).

The human eye is less sensitive to chrominance than luminancethan luminance.

YUV is not required for JPEG compression, but it gives a better compression rate.

28

Page 29: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

RGB VS YUVRGB VS. YUV

It’ i l ith ti t t RGB t YUV Th It’s simple arithmetic to convert RGB to YUV. The formula is based on the relative contributions that red, green and blue make to the luminance and green, and blue make to the luminance and chrominance factors.

There are several different formulas in use depending There are several different formulas in use depending on the target monitor. For example:

Y = 0 299 * R + 0 587 * G + 0 114 * BY = 0.299 R + 0.587 G + 0.114 BU = -0.1687 * R – 0.3313* G + 0.5 * B + 128V 0 5 * R 0 4187 * G 0 813 * B + 128V = 0.5 * R – 0.4187 * G – 0.813 * B + 128

29

Page 30: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

STEP 1B: DOWNSAMPLINGSTEP 1B: DOWNSAMPLING

The chrominance information can (optionally) be downsampled.

The notation 4:1:1 means that for each block of four pixels you have 4 samples of luminance of four pixels, you have 4 samples of luminance information (Y), and 1 each of the two h i t (U d V) chrominance components (U and V).

MCU – minimum Y Ycoded unit Y Y

Y YU, V

30

Y Y

Page 31: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

STEP 2: DIVIDE INTO 8 X 8 BLOCKSSTEP 2: DIVIDE INTO 8 X 8 BLOCKS

N t th t ith YUV l h 16 i l f Note that with YUV color, you have 16 pixels of information in each block for the Y component (though only 8 in each direction for the U and V components)only 8 in each direction for the U and V components).

If the file doesn’t divide evenly into 8 X 8 blocks, extra pixels are added to the end and discarded after the pixels are added to the end and discarded after the compression.

The values are shifted “left” by subtracting 128 The values are shifted left by subtracting 128.(See JPEG Compression for details.)

31

Page 32: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

STEP 3: APPLY THE DCT TRANSFORMSTEP 3: APPLY THE DCT TRANSFORM

In DCT coding, each component of the image is In DCT coding, each component of the image is subdivided into blocks of 8 x 8 pixels.

A two-dimensional DCT is applied to each block of data to obtain an 8 x 8 array of coefficients.

If x[m, n] represents the image pixel values in a block then the DCT is computed for each block of block, then the DCT is computed for each block of the image data as follows:

Page 33: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

DCT REPRESENTATIONDCT REPRESENTATION

In pixels

Mathematical representation

Page 34: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

IDCTIDCT

Page 35: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

STEP 4: QUANTIZE THE COEFFICIENTSSTEP 4: QUANTIZE THE COEFFICIENTSCOMPUTED BY THE DCT

Th DCT i l l i h h DCT ill i The DCT is lossless in that the reverse DCT will give you back exactly your initial information (ignoring the

di h l f i fl i i rounding error that results from using floating point numbers.)

The values from the DCT are initially floating-point. They are changed to integers by quantization.y g g y q

35

Page 36: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

STEP 4: QUANTIZATIONSTEP 4: QUANTIZATION

Quantization involves dividing each coefficient by an integer between 1 and 255 and rounding off.

The quantization table is chosen to reduce the The quantization table is chosen to reduce the precision of each coefficient to no more than necessary.

The quantization table is carried along with the q gcompressed file.

36

Page 37: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

QUANTIZERQUANTIZER

If we wish to recover the original image exactly from the DCT coefficient array, then it is necessary to represent the DCT coefficients with high precision coefficients with high precision.

Such a representation requires a large number of bits. In lossy compression the DCT coefficients are mapped into

a relatively small set of possible values that are represented compactly by defining and coding suitable symbols. y

The quantization unit performs this task of a many-to-one mapping of the DCT coefficients, so that the possible outputs are limited in number outputs are limited in number.

A key feature of the quantized DCT coefficients is that many of them are zero, making them suitable for efficient coding.

Page 38: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

COEFFICIENT TO SYMBOL MAPPING UNITCOEFFICIENT-TO-SYMBOL MAPPING UNIT

The quantized DCT coefficients are mapped to new symbols to facilitate a compact representation in the symbol coding unit that follows.

The symbol definition unit can also be viewed as part of the symbol coding unit However it is shown here as a separate symbol coding unit. However, it is shown here as a separate unit to emphasize the fact that the definition of symbols to be coded is an important task.

An effective definition of symbols for representing AC coefficients in JPEG is the “runs” of zero coefficients followed by a nonzero terminating coefficient followed by a nonzero terminating coefficient.

For representing DC coefficients, symbols are defined by computing the difference between the DC coefficient in the p gcurrent block and that in the previous block.

Page 39: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

STEP 5: ARRANGE IN “ZIGZAG” ORDERSTEP 5: ARRANGE IN ZIGZAG ORDER

This is done so that the This is done so that the coefficients are in order of increasing frequencyincreasing frequency.

The higher frequency coefficients are more likely to be 0 after are more likely to be 0 after quantization.

This improves the compression of This improves the compression of run-length encoding.

Do run length encoding and Do run-length encoding and Huffman coding.

39

Page 40: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

JPEG VS GIFJPEG VS. GIF

Color depth: JPEG stores full color info (24 bits/pixel).( /p ) GIF stores only 8 bits/pixel (intended for

inexpensive color displays)

Edge preservation:JPEG d bl h d ( i hi h JPEG tends to blur sharp edges (esp. in high compression)

GIF does very well with ‘graphics’ images (e g line GIF does very well with graphics images (e.g. line drawings)

Page 41: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

JPEG VS GIF (CONT )JPEG VS. GIF (CONT.)

B&W imagery: JPEG is not suitable for two-tone imagery GIF is lossless for gray scale images (up to GIF is lossless for gray scale images (up to

256 gray values)

Page 42: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

COMPRESSION PERFORMANCECOMPRESSION PERFORMANCE

JPEG: 10:1 to 20:1 compression without visible loss

(effective storage requirement drops to 1 2 bits/pixel)(effective storage requirement drops to 1-2 bits/pixel) 30:1 to 50:1 compression with small to moderate

visual deterioration100 1 i f l li li i 100:1 compression for low quality applications

GIF:GIF: 3:1 compression by reducing color space to 8 bits LZW coding may improve compression up to 5:1

Page 43: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

LZW COMPRESSION ALGORITHMLZW COMPRESSION ALGORITHM

Lempel-Ziv-Welch Algorithm:

w = NIL;w NIL;while ( read a character k )

{if wk exists in the dictionaryw = wk;

elseadd wk to the dictionary;output the code for w;w = k;

}

Argument: ‘ the ‘ requires 5 bytes (80 bits) to encode the requires 5 bytes (80 bits) to encode by assigning a single symbol to it we can express it with 9 bits

Page 44: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

Block Truncation Code

Page 45: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

BLOCK TRUNCATION CODINGBLOCK TRUNCATION CODING

Statistical and structural methods have been developed for image compression

the former being based on the principles of source coding with emphasis on the algebraic source coding with emphasis on the algebraic structure of the pixels in an image, whereas the l tt th d l it th t i t t latter methods exploit the geometric structure of the image.

Page 46: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

BASICS OF BTCBASICS OF BTC

The basic BTC algorithm is a lossy fixed length compression The basic BTC algorithm is a lossy fixed length compression method that uses a Q-level quantizer to quantize a local region of the image. Th i l l h h h b f h The quantizer levels are chosen such that a number of the moments of a local region in the image are preserved in the quantized output.

In its simplest form, the objective of BTC is to preserve the sample mean and sample standard deviation of a gray-scale image.image.

Additional constraints can be added to preserve higher-order moments. For this reason BTC is a block adaptive moment preserving quantizerpreserving quantizer.

Page 47: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

BTCBTC

The first step of the algorithm is to divide the image into nonoverlapping rectangular regions. For the sake of simplicity we let the blocks be square sake of simplicity we let the blocks be square regions of size n x n, where n is typically 4.

For a two level (1 bit) quantizer the idea is to select For a two-level (1 bit) quantizer, the idea is to select two luminance values to represent each pixel in the block block.

These values are chosen such that the sample mean and standard deviation of the reconstructed mean and standard deviation of the reconstructed block are identical to those of the original block.

Page 48: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

BTCBTC

An n x n bit map is then used to determine weather An n x n bit map is then used to determine weather a pixel luminance value is above or below a certain threshold threshold.

In order to illustrate how BTC works, we will let the fsample mean of the block be the threshold;

a “1” would then indicate if an original pixel value is b hi h h ld d above this threshold, and

“0” if it is below.

Since BTC produces a bit map to represent a block, it is classified as a binary pattern image coding method

Page 49: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

BTCBTC

B k i g th bit f h bl k th By knowing the bit map for each block, the decompression/reconstruction algorithm knows whether a pixel is brighter or darker than the average whether a pixel is brighter or darker than the average.

Thus, for each block two gray-scale values, a and b, are needed to represent the two regions are needed to represent the two regions. These are obtained from the sample mean and sample

standard deviation of the block, and , they are stored together with the bit map.

Page 50: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

BTC ENCODERBTC ENCODER

Page 51: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

BTC DECODER

Page 52: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

BTCBTC

the image was compressed from 8 bits per pixel to 2 bits per the image was compressed from 8 bits per pixel to 2 bits per pixel (bpp).

This is done because BTC requires 16 bits for the bit map, 8 bi f h l d 8 bi f h l d d bits for the sample mean, and 8 bits for the sample standard deviation.

Thus, the entire 4 x 4 block requires 32 bits, and hence the Thus, the entire 4 x 4 block requires 32 bits, and hence the data

rate is 2 bpp. F thi l it i t d t d h ll d t From this example it is easy to understand how a smaller data rate can be achieved by selecting a bigger block size, or by allocating fewer bits for the sample mean or the sample

d d d i istandard deviation

Page 53: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

DATA RATE VS. BLOCK SIZE.

Page 54: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

MOMENTS PRESERVATION IN THE OUTPUTMOMENTS PRESERVATION IN THE OUTPUT

Page 55: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

BTC COMPRESSIONBTC COMPRESSION

Page 56: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

Vector quantizationq

Page 57: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

QUANTIZATIONQUANTIZATION Quantization is a field of study that has matured over the

t f d d past few decades. In simplest terms, quantization is a mapping of a large set

of values to a smaller set of values. The concept is here pillustrated

It shows on the left a sequence of unquantized samples with amplitudes assumed to be of infinite precision, and on the right that same sequence quantized to integer values.t e g t t at sa e seque ce qua t ed to tege a ues

Page 58: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

VECTOR QUANTIZATIONVECTOR QUANTIZATION Obviously, quantization is an irreversible process, since it

involves discarding information. If it is done wisely, the error introduced by the process can

be held to a minimumbe held to a minimum. The generalization of this notion is called vector

quantization, commonly denoted VQ.quantization, commonly denoted VQ. It too is a mapping from a large set to a smaller set, but it

involves quantizing blocks of samples together.

Page 59: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

APPLICATIONSAPPLICATIONS

The general concept of VQ can be applied to any The general concept of VQ can be applied to any type of digital data.

For a one-dimensional signal as illustrated in For a one dimensional signal as illustrated in previous slide vectors can be formed by extracting contiguous blocks from

the sequence. For two-dimensional signals (i.e,, digital images)

b f d b ki 2 D bl k h vectors can be formed by taking 2-D blocks, such as rectangular blocks, and unwrapping them to form vectors form vectors.

Similarly, the same idea can be applied to 3-D data (i.e., video), color and multispectral data, transform ( , ), p ,coefficients, and so on.

Page 60: Lesson09 Image compression - Politecnico di Milanomarconlab.deib.polimi.it/courses/VideoProc/slides/... · JPEG IMAGE COMPRESSION JPEG: Joint Photographic Experts Group. JPEG i JPEG:

ENCODER AND DECODERENCODER AND DECODER