17
Image Coding and Compression Lecture 17 Patrick Karlsson [email protected] Centre for Image Analysis Uppsala University Computer Assisted Image Analysis May 19 2006 Karlsson (Uppsala University) Image Coding and Compression Image Analysis 1 / 33 Reading Instructions and Assignment Chapters and Assignment for This Lecture Chapter 8.1 - 8.3.1, 8.4 - 8.4.3, 8.5.1 - 8.5.2 and 8.6 in Gonzales-Woods. The suggested problem for this lecture is not a problem from the course literature (see next slide). Karlsson (Uppsala University) Image Coding and Compression Image Analysis 2 / 33

Reading Instructions and Assignment

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Reading Instructions and Assignment

Image Coding and CompressionLecture 17

Patrick [email protected]

Centre for Image AnalysisUppsala University

Computer Assisted Image AnalysisMay 19 2006

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 1 / 33

Reading Instructions and AssignmentChapters and Assignment for This Lecture

Chapter 8.1− 8.3.1, 8.4− 8.4.3, 8.5.1− 8.5.2and 8.6 in Gonzales-Woods.

The suggested problem for this lecture is not aproblem from the course literature (see nextslide).

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 2 / 33

Page 2: Reading Instructions and Assignment

Suggested problem for lecture 17Huffman coding

Calculate the Huffman code on the image to theright. Show all steps in the coding procedure, andalso calculate Lavg .

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 3 / 33

Data and information

Data is not the same thing as information.

Data is the means with which information is expressed. Theamount of data can be much larger than the amount ofinformation.

Data that provide no relevant information = redundant data orredundancy.

Image coding or compression has a goal to reduce the amount of databy reducing the amount of redundancy.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 4 / 33

Page 3: Reading Instructions and Assignment

Definitions

n1 = data.

n2 = data− redundancy (i.e., data after compression).

Compression ratio = CR = n1n2

.

Relative redundancy = RD = 1− 1CR

.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 5 / 33

Different Types of Redundancy

CR Coding Redundancy.

IR Interpixel Redundancy.

PVR Psycho-Visual Redundancy.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 6 / 33

Page 4: Reading Instructions and Assignment

Image compression and decompression

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 7 / 33

Image Compression

Image compression can be:Reversible (loss less), with no loss of information.

I A new image is identical to the original image (afterdecompression).

I Reversibility is necessary in most image analysis applications.I The compression ratio is typically 2 to 10 times.

Non reversible (lossy), with loss of some information.I Lossy compression is often used in image communication, video,

WWW, etc.I It is usually important that the image visually is still nice.I The compression ratio is typically 10 to 30 times.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 8 / 33

Page 5: Reading Instructions and Assignment

Objective measures of image qualityError

e(x , y) = fapprox(x , y)− foriginal(x , y).

Total Error

etot =M−1∑x=0

N−1∑y=0

(fapprox − foriginal

).

Root-Mean-Square

eRMS =1

MN

√√√√M−1∑x=0

N−1∑y=0

(fapprox − foriginal

)2.

Signal-to-Noise Ratio

SNRMS =

∑M−1x=0

∑N−1y=0 (fapprox)2∑M−1

x=0∑N−1

y=0

(fapprox − foriginal

)2 .

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 9 / 33

Subjective measures of image quality

Let a number of test persons grade the images asbad/acceptable/good etc.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 10 / 33

Page 6: Reading Instructions and Assignment

Information measure

If p(E) is the probability of an event E , then I(E) = −logp(E) is ameasure of the information that the event provides.

The average information is called entropy.

Shannon Entropy

H(z) =L−1∑k=0

p(rk ) log(rk ) ,

where rk is gray level k , and L is the number of gray levels.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 11 / 33

Coding redundancy

Basic idea: Different gray levels occur with different probability(non uniform histogram). Use shorter code words fore the morecommon gray levels and longer code words for the less commongray levels. This is called Variable Length Coding.

The amount of data in an M × N image with L gray levels is equalto M × N × Lavg , where

Lavg =L−1∑k=0

l(rk )pr (rk ),

l(rk ) is the number of bits used to represent gray level rk , andp(rk ) is the probability of gray level rk in the image.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 12 / 33

Page 7: Reading Instructions and Assignment

Example 3-bit imagegray level rk probability p(rk ) source code0 0.1 000 011 0.4 001 02 0.03 010 113 0.05 011 104 0.3 100 15 0.1 101 006 0.01 110 1117 0.01 111 000

Lavg =L−1∑k=0

l(rk )pr (rk ).

Source: Lavg = (constant l(rr ) = 3) = 3× 1 = 3.

Code: Lavg = 0.1× 2 + 0.4× 1 + . . . = 1.32.

This will however not work since the code is not unambiguous.What does for example the code 010 mean? Use Huffman coding!

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 13 / 33

The Huffman code

Yields the smallest possible number of unique code symbols persource symbol.Step 1.

1 Sort the gray levels by decreasing probability.2 Add the two smallest probabilities.3 Sort the new value into the list.4 Repeat until only two probabilities remain.

Step 2.1 Give the code 0 to the highest probability, and the code 1 to the

lowest probability in the present node.2 Go backwards through the tree and add 0 to the highest and 1 to

the lowest probability in each node until all gray levels have aunique code.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 14 / 33

Page 8: Reading Instructions and Assignment

Example of Huffman codingrk p(rk ) node 1 node 2 node 3 node 4 node 5 node 61 0.4 →0.4 0.4 0.4 0.4 0.4 ↘0.64 0.3 →0.3 0.3 0.3 0.3 0.3 ↗0.40 0.1 →0.1 0.1 0.1 ↘0.2 →0.35 0.1 →0.1 0.1 0.1 ↗0.1 ↗3 0.05 →0.05 0.05 →0.12 0.03 →0.03 →0.05 ↗6 0.01 →0.02 ↗7 0.01 ↗

Lavg = 3rk code node 1 node 2 node 3 node 4 node 5 node 61 1 1 1 1 1 1 ↙04 00 00 00 00 00 00 ↖10 011 011 011 011 ↙010 ←015 0100 0100 0100 0100 ↖011 ↙3 01010 01010 01010 ←01012 010110 010110 ←01011 ↙6 0101110 ←010111 ↙7 0101111 ↙Karlsson (Uppsala University) Image Coding and Compression Image Analysis 15 / 33

The Huffman code, cont.

Lavg =L−1∑k=0

l(rk )pr (rk ) = 2.27

CR =n1

n2=

32.27

= 1.32

RD = 1− 1CR

=n1 − n2

n1=

3− 2.273

= 0.24

The Huffman code results in unambiguous code.

The code is reversible without loss.

The table for the translation of the code has to be stored togetherwith the coded image.

The Huffman code does not take correlation between adjacentpixels into consideration.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 16 / 33

Page 9: Reading Instructions and Assignment

Interpixel RedundancyAlso called spatial or geometric redundancy

There is often correlation between adjacent pixels, i.e., the valueof the neighbors of an observed pixel can often be predicted fromthe value of the observed pixel.

Coding methods:

Run-Length coding.

Difference coding.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 17 / 33

Run-length codingEvery code word is made up of a pair (g, l) where g is the graylevel, and l is the number of pixels with that gray level (length, or“run”).E.g.,

56 56 56 82 82 82 83 8056 56 56 56 56 80 80 80

creates the run-length code (56, 3)(82, 3)(83, 1)(80, 4)(56, 5).The code is calculated row by row.

Very efficient coding for binary data.Important to know position, and the image dimensions must bestored with the coded image.Used in most fax machines.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 18 / 33

Page 10: Reading Instructions and Assignment

Difference coding

f (xi) =

{xi if i = 0,xi − xi−1 if i > 0.

E.g.,

original 56 56 56 82 82 82 83 80 80 80 80codef (xi) 56 0 0 26 0 0 1 −3 0 0 0

The code is calculated rob by row.

Both run-length coding, and difference coding are reversible, andcan be combined with, e.g., Huffman coding.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 19 / 33

Example of combined difference and Huffman coding

Original image. Difference image.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 20 / 33

Page 11: Reading Instructions and Assignment

Huffman code of original image

Lavg = 3.1Karlsson (Uppsala University) Image Coding and Compression Image Analysis 21 / 33

Huffman code of Difference image

Lavg = 2Karlsson (Uppsala University) Image Coding and Compression Image Analysis 22 / 33

Page 12: Reading Instructions and Assignment

Bit-plane coding

Divide the gray level/color image into series of binary images (withone image per bit). Code each image separately using the abovedescribed methods. An 8-bit image will be represented by 8 codedbinary images.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 23 / 33

Psycho-Visual Redundancy

If the image will only be used for visual observation (i.e.,illustrations on the web, etc.), much of the information is usuallypsycho-visually redundant. It can be removed without changingthe visual quality of the image. This kind of compression is usuallyirreversible.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 24 / 33

Page 13: Reading Instructions and Assignment

Psycho-Visual redundancy is often reduced byquantification

E.g., Uniform quantification of gray levels

Remove the least significant bits of the data.

Causes edge effects.

The edge effects can be reduced bu Improved Gray Scale (IGS).

Remove the least significant bit, and add a “random number”based on the sum of the least significant bits of the present, andthe previous pixel.

IGS reduces edge effects, but will at the same time unsharpentrue edges.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 25 / 33

Improved Gray Scale (IGS)

(a) Original image. (b) Uniform quantization to 16 levels. (c) IGSquantization to 16 levels.

Pixel Gray level Sum IGSi − 1 N/A 00000000 N/A

i 01101100 01101100 0110i + 1 10001011 10010111 1001i + 2 10000111 10001110 1000i + 3 11110100 11110100 1111

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 26 / 33

Page 14: Reading Instructions and Assignment

More quantification methodsMotion pictures

Method 1.1 Transfer the first image to the observer.2 Find the changes from the previous image.3 Transfer only the changes.

Method 2.1 Transfer the most important information (e.g., the lowest

frequencies) first.2 Send the less important information later.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 27 / 33

Transform coding

1 Divide the image into n × n sub-images.2 Transform each sub-image using a reversible transform (e.g., the

Hotelling transform, the discrete Fourier transform (DFT) or thediscrete cosine transform (DCT)).

3 Quantify, i.e., truncate the transformed image (e.g., by using DFT,and DCT frequencies with small amplitude can be removedwithout much information loss). The quantification can be eitherimage dependent (IDP) or image independent (IIP).

4 Code the resulting data, normally using some kind of “variablelength coding”, e.g., Huffman code.

The coding is not reversible (unless step 3 is skipped).

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 28 / 33

Page 15: Reading Instructions and Assignment

Some common image formats

JPEG Joint Photographic Experts Group - Exists in manydifferent versions but is always some kind oftransformation coding. JPEG is not reversible due toquantification.

MPEG Motion Picture Experts Group - Similar to JPEG, but themotion in comparison to the previous image is calculated,and used in the compression.

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 29 / 33

Example of JPEG compression

75% 27kB 50% 17kB

25% 11kB 10% 6kBKarlsson (Uppsala University) Image Coding and Compression Image Analysis 30 / 33

Page 16: Reading Instructions and Assignment

Some more common image formats

LZW (Lempel-Ziv-Welch) A “word-based” code. The data isrepresented by pointers to a library of symbols (seeHuffman code). LZW compression is loss less, and canoften be chosen when TIFF (Tagged Image File Format)images are stored. The result is a smaller file whichusually takes a bit longer to decode. An image filedirectory (set of symbols) is included in the header.

GIF (Graphics Interchange Format) Creates a coding for colorimages where each color is coded by only a few bits(usually three). GIF also uses LZW compression forstorage and transfers. GIF is fully reversible (loss less) ifless then 256 colors are present in the original image.

Remember that the time used for coding, and decoding is importantwhen choosing coding method!

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 31 / 33

Choice of image formats

Images used for image analysis should always be stored in a lossless format.

Images for the WWW have to be either GIF, JPEG or PNG (due tothe license issues GIF).

Chose GIF for graphs and hand drawn figures with few colorshades (JPEG transform coding and truncation can causeartifacts around sharp edges).

Chose JPEG for photos and figures with many colors, and smoothtransitions between colors (GIF reduces the number of colors to256).

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 32 / 33

Page 17: Reading Instructions and Assignment

Transform coding

Karlsson (Uppsala University) Image Coding and Compression Image Analysis 33 / 33