Image and Multidimensional Signal Processinginside.mines.edu/~whoff/courses/EENG510/lectures/25...22...

36
Image and Multidimensional Signal Processing Colorado School of Mines Colorado School of Mines Image and Multidimensional Signal Processing Professor William Hoff Dept of Electrical Engineering &Computer Science http://inside.mines.edu/~whoff/

Transcript of Image and Multidimensional Signal Processinginside.mines.edu/~whoff/courses/EENG510/lectures/25...22...

Image and Multidimensional Signal Processing Colorado School of Mines

Colorado School of Mines

Image and Multidimensional Signal Processing

Professor William Hoff

Dept of Electrical Engineering &Computer Science

http://inside.mines.edu/~whoff/

Image and Multidimensional Signal Processing Colorado School of Mines

Image Compression

2

Image and Multidimensional Signal Processing Colorado School of Mines

Image Compression

• Goal: Reduce amount of data, for transmission or storage • Try to preserve information, so that image can be

reconstructed: – Exactly (loss-less compression) – Approximately (lossy compression)

3

'

bC

b

Compression ratio where b = # bits uncompressed b’ = #bits compressed

Image and Multidimensional Signal Processing Colorado School of Mines

Redundancy

• We take advantage of redundancy in the input image: – Coding redundancy – Spatial and temporal redundancy – Irrelevant information

4

Coding redundancy: Only a few gray values are present; we can represent using short code words (with few bits)

Spatial redundancy: All gray values are present, but pixels along each row are the same; if you know the value of the first pixel you know the whole row

Irrelevant information: Only a few gray values are present, clustered around value 128; could represent using a constant 128 and would look the same

Image and Multidimensional Signal Processing Colorado School of Mines

Coding Redundancy

• Use short code words instead of long ones

• Can use variable length codes, so that most common values have shortest codes

• Average bit length

5

1

0

( ) ( )L

avg k r k

k

L l r p r

Image and Multidimensional Signal Processing Colorado School of Mines

Measuring Image Information

• We can compute the theoretical minimum number of bits needed to code an image

• First, consider an “event” E – If the event is unlikely to happen, then its probability P(E) is small, and 1/P(E) is large – When the event does occur, it is significant and provides us with a lot of information

• We define the “self information” of event E

• This is how much information is attached to E • Example:

– If P(E) = 1, I = 0 bit – If P(E) = ½, I = 1 bit – If P(E) = ¼, I = 2 bits

))((log)(

1log)( 22 EP

EPEI

6

Image and Multidimensional Signal Processing Colorado School of Mines

Measuring Image Information

• Say we have a set of random events, drawn from a possible set of values {a1, a2, …, aJ}

• We’ll call these the “source symbols” – They could be randomly generated from a transmitter

• The probabilities of generating these symbols are

{P(a1), P(a2), …, P(aJ)}

• The information carried by a single symbol is -log2(P(aj))

• Thus, the average information per symbol is

1

( ) log ( )J

j j

j

H P a P a

Assumes that events are independent

7

Image and Multidimensional Signal Processing Colorado School of Mines

Entropy of an image

• For images, the “events” {r0, r1, …, rL-1} are possible gray level values

• We can use the histogram to estimate the probabilities of the symbols

• The average information per pixel is (also called the entropy of the image)

• Examples: – 8 gray levels, same probability for all values: p0=p1= … = p7=1/8. H=?

– 8 gray levels, but only one has nonzero probability, say level 1: p0=p2= … = p7 = 0, p1=1. H=?

1

2

0

( ) log ( )L

r k r k

k

H p r p r

8

This is the best you can do (for uncorrelated values)

Image and Multidimensional Signal Processing Colorado School of Mines

Entropy - example

1

2

0

( ) log ( )L

r k r k

k

H p r p r

rk P(ai) log P(ai) -P log P

87 0.25 -2 0.5

128 0.47 -1.08927 0.511956

186 0.25 -2 0.5

255 0.03 -5.05889 0.151767

H (bits) 1.663722

9

Image and Multidimensional Signal Processing Colorado School of Mines

Fidelity Criteria

• RMS (root mean square) error – an objective measure of error between original and the compressed image

• Can also use SNR

• And subjective measures

– E.g., rate quality on a scale of 1 to 10

2/1

1

0

1

0

2

),(),(ˆ1

M

x

N

y

rms yxfyxfMN

e

1 12

0 0

1 1 2

0 0

ˆ ( , )

ˆ ( , ) ( , )

M N

x y

ms M N

x y

f x y

SNR

f x y f x y

10

Image and Multidimensional Signal Processing Colorado School of Mines

Image Compression & Decompression Model

• Mapper: Transforms data to a form that can be more easily compressed (eg, Fourier or wavelet transform)

• Quantizer: Reduces amount of data (eg., throws away smallest transform coefficients)

• Symbol coder: Codes the resulting data using the shortest code words (eg., variable length coding)

11

Image and Multidimensional Signal Processing Colorado School of Mines

Loss-less Compression Methods

• “Loss-less”: the compressed image can be reconstructed exactly

• We’ll look at these methods: – Huffman coding

– Arithmetic coding

– LZW coding

– Run-length coding

12

Image and Multidimensional Signal Processing Colorado School of Mines

Huffman Coding

• Takes advantage of coding redundancy

• Generates a variable length code, as close as possible to the theoretical minimum length

• Doesn’t take advantage of inter-pixel redundancy

• Widely used as a component (the symbol coder) in many compression methods

Algorithm:

(1) Find the gray level probabilities

(2) Order the probabilities, from smallest to largest

(3) Combine the smallest two by addition

(4) Repeat steps 2-3 until only two probabilities are left

(5) By working backward along the tree, generate code by alternating assignment of 0 and 1

13

Image and Multidimensional Signal Processing Colorado School of Mines

Example

Symbol Probability

a1 0.1

a2 0.4

a3 0.06

a4 0.1

a5 0.04

a6 0.3

Sort the probabilities, from largest to smallest

Start with gray level probabilities

Combine the smallest two by addition

14

Image and Multidimensional Signal Processing Colorado School of Mines

Example

15

Image and Multidimensional Signal Processing Colorado School of Mines

Example Work backward along the tree, generate code by alternating assignment of 0 and 1

Final result: Sym Code

16

Image and Multidimensional Signal Processing Colorado School of Mines

Example

• Example – generate code sequence for symbols

a2 a1 a3 a1

• Example – decode the sequence 010100111100

• Average length of code?

17

Image and Multidimensional Signal Processing Colorado School of Mines

Example

• Compare to theoretical minimum

J

j

jj aPaPH1

2 )(log)()(z

P(ai) log P(ai) -P log P

a2 0.4

a6 0.3

a1 0.1

a4 0.1

a3 0.06

a5 0.04

H

18

Image and Multidimensional Signal Processing Colorado School of Mines

Example

• Compare to theoretical minimum

J

j

jj aPaPH1

2 )(log)()(z

P(ai) log P(ai) -P log P

a2 0.4 -1.32193 0.528771

a6 0.3 -1.73697 0.52109

a1 0.1 -3.32193 0.332193

a4 0.1 -3.32193 0.332193

a3 0.06 -4.05889 0.243534

a5 0.04 -4.64386 0.185754

H 2.143534

19

Image and Multidimensional Signal Processing Colorado School of Mines

Example

• Find Huffman code for

Gray Level Probability

00 0.2

01 0.3

10 0.1

11 0.4

Algorithm: (1) Find the gray level probabilities (2) Sort the probabilities (3) Combine the smallest two by addition (4) Repeat steps 2-3 until only two are left (5) Work backward, generate code

Lavg = ?

H = ?

20

Image and Multidimensional Signal Processing Colorado School of Mines

Example

• Find Huffman code for

Gray Level Probability

00 0.2

01 0.3

10 0.1

11 0.4

0.4

0.3

0.2

0.1

0.4

0.3

0.3

0.6

0.4

0

1

1 1

1

00

01

00 00

010

011

010

011

Algorithm: (1) Find the gray level probabilities (2) Sort the probabilities (3) Combine the smallest two by addition (4) Repeat steps 2-3 until only two are left (5) Work backward, generate code

21

Image and Multidimensional Signal Processing Colorado School of Mines

Arithmetic Coding

• A sequence of values is assigned a single arithmetic code word

• The code word is a fractional number between 0 and 1 (e.g., 0.4731)

• Each symbol is assigned an interval based on its probability of occurrence

• Code words are fixed length

22

Image and Multidimensional Signal Processing Colorado School of Mines

Example

Message: a1a2a3a3a4

23

With each new interval, you find the subintervals by multiplying the total interval by the probability of each symbol, and then add it to the lower bound of the interval to get the upper bound.

Image and Multidimensional Signal Processing Colorado School of Mines

Example

Message: a1a2a3a3a4

24

0.2

0.072

0.08

Can use 0.068

0.2(0.4)+0

(0.08-0.04)(0.8)+0.04

0.04

0.4

0.8

(0.072-0.056)(0.8)+0.056

0.0688

Image and Multidimensional Signal Processing Colorado School of Mines

Example

• Final code word: 0.068

• Three decimal digits for five symbols, or 3/5 = 0.6 digits per symbol

– Equivalently, 1.99 bits

– 100.6 = 2x -> x = 1.99 bits

• Theoretical minimum: 1.92 bits or 0.58 digits

P(ai) log P(ai) -P log P

a1 0.2 -2.3219 0.46439

a2 0.2 -2.3219 0.46439

a3 0.4 -1.3219 0.52877

a4 0.2 -2.3219 0.46439

H 1.92193

25

Image and Multidimensional Signal Processing Colorado School of Mines

Example

• The sequence “a1 a1 a1” can be encoded as what number?

26

Image and Multidimensional Signal Processing Colorado School of Mines

LZW Coding

• Stands for Lempel-Ziv-Welch

• Works by coding short strings of data

• Used in GIF, TIFF, and PDF file formats

• Creates a “dictionary” of code words – For an 8-bit image, the first 256 words are assigned to the gray values

0,1,2, … , 255

– As sequences are discovered, new code words (i.e., 256 through 511) are assigned to represent them

– Eg: The sequence 126-126 may be assigned to code word 256

27

Image and Multidimensional Signal Processing Colorado School of Mines

Example

• Original image

39 39 126 126

39 39 126 126

39 39 126 126

39 39 126 126 Dictionary Index Entry

0 0

1 1

: :

255 255

256 ?

: :

511 ?

Initial Dictionary

28

Image and Multidimensional Signal Processing Colorado School of Mines

29

Image and Multidimensional Signal Processing Colorado School of Mines

• Original image

• Coded sequence

39 39 126 126

39 39 126 126

39 39 126 126

39 39 126 126

Final Dictionary

39 39 126 126

256 258

260 259

257 126

Dictionary Index Entry

0 0

1 1

: :

255 255

256 39-39

257 39-126

258 126-126

259 126-39

260 39-39-126

261 126-126-39

262 39-39-126-126

263 126-39-39

264 39-126-126

30

Image and Multidimensional Signal Processing Colorado School of Mines

Run Length Coding

• Inter-pixel redundancy

• Usually used for binary images

• Output the number of consecutive 0’s along a row, then the # of 1’s, etc

• Best case for compression: a row is all zeros (or all ones)

• Worst case for compression?

0 0 0 0 0 0 0 0

1 1 1 1 0 0 0 0

0 1 1 0 0 0 0 0

0 1 1 1 1 1 0 0

0 1 1 1 0 0 1 0

0 0 1 0 0 1 1 0

1 1 1 1 0 1 0 0

0 0 0 0 0 0 0 0

Output code

31

8

0 4 4

1 2 5

:

Image and Multidimensional Signal Processing Colorado School of Mines

RLC Applied to Non-binary Images

• For an m-bit image, the gray values are am-12m-1 + am-22m-2 + … + a121 + a020

• Apply RLC separately to each bit plane

• This can be a problem in areas where values fluctuate about certain transition points – Example: 12710 = 011111112

– But 12810 = 100000002

– So each bit keeps flipping from 0 to 1

32

Image and Multidimensional Signal Processing Colorado School of Mines

Example

33

Image and Multidimensional Signal Processing Colorado School of Mines

Better: Use Gray Code

• First code the image using the “Gray Code” rather than the normal binary code

• Gray code:

– Each adjacent code word differs only by one bit

– So if a pixel differs from its neighbor by one gray level, only one bit in the gray code is different

34

Image and Multidimensional Signal Processing Colorado School of Mines

Gray Codes

00

01

11

10

00

01

10

11

0

1

2

3

2 bit binary

2 bit Gray

35

3 bit Gray

Image and Multidimensional Signal Processing Colorado School of Mines

36