You are on page 1of 53

Lecture 3:

Histogram
CSC2014 Digital Image Processing

Outline
Image Histogram
Histogram Threshold Selection
Adaptive Thresholding
Contrast Stretching
Histogram Equalization
Histogram Matching

Learning Objectives
Understand what an image histogram represents.
Understand the use of histogram manipulation

techniques to perform image enhancement and


segmentation.

Image Histogram
Histogram of a digital image with gray-

levels in the range [0, L-1] is a discrete


function h(rk) = nk where rk is the kth level
and nk is the number of pixels having the
gray-level rk.
Simplified version:
The histogram of an image shows us the
distribution of gray levels in the image.
Plot of frequency vs intensity values.
4

Count
0

6
4
2
0

Gray
3 Level

Image Histogram

Image Histogram
Histogram

h(rk ) nk

rk : the kth intensity value.


nk : number of pixels in the image with intensity rk.
nk
Normalized Histogram p(rk )
MN
nk: number of pixels in the image (size M x N) with intensity rk .

Count
0

rk

P(rk)

r0 = 0

2/16

r1 = 1

5/16

r2 = 2

4/16

r3 = 3

5/16

0.3
75
0.2
5
0.12
5
0

Gray
3 Level

Image Histogram
Example
Suppose that a 3-bit image (L=8) of size 64 64 pixels (MN

= 4096) has the intensity distribution shown in following


table.

Histogram Threshold
Selection
By selecting a threshold value between the

two
peaks,
we
can
separate
the
background/foreground of the image using the
thresholding approach.

s=

1.0 r > threshold


0.0 r <= threshold

10

Histogram Threshold
Selection
background

coins

11

Histogram Threshold
Selection

12

Histogram Threshold
Selection
Is that means we have to always select the

threshold value manually?

13

Histogram Threshold
Selection
The Otsus method calculates the optimum

threshold separating the two classes of pixels


so that the intra-class variance is minimal.

14

Otsus Method

15

Adaptive Thresholding
Designed

to overcome the limitations of


conventional, global thresholding by using a
different threshold at each pixel location in the
image.

Threshold is determined by the values of the

pixels in the neighbourhood of the pixel.

16

Adaptive Thresholding
Threshold value, t, can be based on several

statistics:
t = mean + C;
t = median + C;
s=
floor((max-min)/2) + C;

1.0 r > threshold


0.0 r <= threshold

10 10 10 10 10

20 20 20 20 20

50 50 50 50 50

20 20 20 20 20

10 10 10 10 10

0
17

Adaptive Thresholding
Illumination may differ over the image.
Roughly uniform in a sufficiently

small,

local

neighbourhood.
Original

Global Thresholding

Adaptive
Thresholding

18

Histogram (Different Types of


Images)

19

Contrast Stretching
Improve contrast of an image by stretching

the range of pixel intensities to occupy a


larger dynamic range in the output image.

20

Contrast Stretching

a upper pixel value limit (e.g.255)


b lower pixel value limit (e.g. 0)
c maximum pixel value of input image
d minimum pixel value of input image

Problem:
New image output depends on choice of maximum and

minimum pixel value which might not be an accurate


representation of the general pixel range with the image.
21

Contrast Stretching
50

50

50

80

81

50

50

80

81

51

51

80

255

81

51

51

80

81

81

51

51

80

81

81

22

Contrast Stretching
Solution:
Ensure that the maximum and minimum pixel

value truly represent image content and robust to


statistical outliers.
For example:
Select c and d as the 5th percentile and 95th
percentile points of the cumulative distribution.

23

Contrast Stretching
25 Pixels x 5% = 1.25
50

50

50

80

81

50

50

80

81

51

51

80

255

81

51

51

80

81

81

51

51

80

81

81

24

Contrast Stretching

25

Histogram Equalization
An automatic transformation function that seeks

to produce an output image


histogram (better contrast).

with

uniform

sk T(rk )

rk sk

26

Histogram Equalization
Cumulative Distribution Function (CDF)

27

Count

rk

P(rk)

r0 = 0

2/16

r1 = 1

5/16

r2 = 2

4/16

r3 = 3

5/16

0.3
75
0.2
5
0.12
5
0

Gray
3 Level

Gray
3 Level

CDF(Prk)
1
0.5
0.12
5

28

Count

Count

0.3
75
0.2
5
0.12
5

0.3
75
0.2
5
0.12
5
0

CDF(Prk)

Gray
3
Level

0.5

0.5

3
Gray
Level

Gray
3
Level

3
Gray
Level

CDF(Prk)

CDF(Prk)
1
Cu = 1
x=3
1*3=3
0.5
Cu =
0.375
x=0
0.375 * 3 =
1.125
0
3

Cu = 0.5
x=1
0.5 * 3 = 1.5

Cu =
0.675
x=2
0.675 * 3 =
2.025

Gray
Level
30

Count

Count

0.3
75
0.2
5
0.12
5

0.3
75
0.2
5
0.12
5
0

CDF(Prk)

Gray
3
Level

0.5

0.5

3
Gray
Level

Gray
3
Level

3
Gray
Level

CDF(Prk)

Histogram Equalization Example


Cu

CDF (Prk)

T (r)

rk

sk

790

0.19

1.35

1813

0.44

3.10

2663

0.65

4.55

3319

0.81

5.67

3648

0.89

6.23

3893

0.95

6.65

4015

0.98

6.86

4096

1.00

7.00

32

Histogram Equalization Example


sk

Ps(sk)

0.00

0.19

0.00

0.25

0.00

0.21

0.24

0.11

33

Histogram Equalization Example


Consider the following 8-level gray image of size

88. Show the original histogram of the image.


Compute the equalized histogram and display
graphically.

34

Histogram (Different Types of


Images)

35

Histogram Equalization

36

Histogram Equalization

37

Histogram Equalization

38

Comparison

Original

Histogram
Equalization

Contrast
Stretching

39

Comparison

Original

Histogram
Equalization

Contrast
Stretching

40

Histogram Equalization Summary


In general, it stretches/compresses an image such

that:
Pixel values that occur frequently occupies a bigger

dynamic range, i.e., get stretched and become more


visible.
Pixel values that occur infrequently will occupy a

smaller dynamic range, i.e., get compressed and


become less visible.
41

Histogram Matching
Also known as histogram specification.
Enables us to match the grayscale distribution

in one image to the grayscale distribution in


another image.

Original

Target

Output

42

Histogram Matching
CDF

CDF

43

Histogram Matching

01 2 3 4 5 6
7

01 2 3 4 5 6
7

01 2 3 4 5 6
7

01 2 3 4 5 6
7

44

Histogram Matching

01 2 3 4 5 6
7

01 2 3 4 5 6
7

01 2 3 4 5 6
7

01 2 3 4 5 6
7

45

Histogram Matching
Example
Get the histogram transformation function and make

the output image with the specified histogram,


listed in the table on the right.

46

Histogram Matching
Example
Find the smallest value of zq, so that G(zq) is

closest to sk
rk

Prk

CDF (Prk)

sk

Zq

Pz(zq)

CDF(Pz(zq))

G(zk)

0.19

0.19

0.00

0.00

0.00

0.25

0.44

0.00

0.00

0.00

0.21

0.65

0.00

0.00

0.00

0.16

0.81

0.15

0.15

1.05

0.08

0.89

0.20

0.35

2.45

0.06

0.95

0.30

0.65

4.55

0.03

0.98

0.20

0.85

5.95

0.02

1.00

0.15

1.00

7.00
47

Histogram Matching
Example
After Equalization:
rk

Prk

CDF (Prk)

sk

rk

New Prk

CDF (New Prk)

0.19

0.19

0.00

0.25

0.44

0.19

0.19

0.21

0.65

0.19

0.16

0.81

0.25

0.44

0.08

0.89

0.44

0.06

0.95

0.21

0.65

0.03

0.98

0.24

0.89

0.02

1.00

0.11

1.00

48

rk
0
1
2
3
4
5
6
7

sk
1
3
5
6
6
7
7
7

Zq
0
1
2
3
4
5
6
7

Pz(zq)
0.00
0.00
0.00
0.15
0.20
0.30
0.20
0.15

CDF(Pz(zq))
0.00
0.00
0.00
0.15
0.35
0.65
0.85
1.00

CDF(After Equalization) of Orignal Hist.


1

G(zk)
0.00
0.00
0.00
1.05
2.45
4.55
5.95
7.00

New Pz(zq)
0.00
0.15
0.20
0.00
0.00
0.30
0.20
0.15

CDF(New Pz(zq))
0.00
0.15
0.35
0.35
0.35
0.65
0.85
1.00

CDF(After Equalization) of Specified Hist.


5

49

rk

Prk

CDF (Prk)

sk

0.19

0.19

0.25

0.44

0.21

0.65

0.16

0.81

0.08

0.89

0.06

0.95

0.03

0.98

0.02

1.00

7
6
5
4
3
2
1
0
0

7
Zq

Pz(zq)

CDF(Pz(zq))

G(zk)

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.00

0.15

0.15

1.05

0.20

0.35

2.45

0.30

0.65

4.55

0.20

0.85

5.95

0.15

1.00

7.00

7
6
5
4
3
2
1
0
0
7

50

1 -> 3
3->4

7
6
5
4
3
2
1
0
0

5 -> 5
6 -> 6
7 -> 7

7
7
6
5
4
3
2
1
0
0
7

51

Histogram Matching
Example

52

Histogram Matching
Example
Match Pr(rk) to Pz(zq):
rk

Pr(rk)

zq

Specified Pz(zq)

r0=0

0.05

z0=0

0.35

r1=1

0.25

z1=1

0.05

r2=2

0.20

z2=2

0.10

r3=3

0.15

z3=3

0.15

r4=4

0.10

z4=4

0.20

r5=5

0.10

z5=5

0.05

r6=6

0.10

z6=6

0.05

r7=7

0.05

z7=7

0.05
53