05 - Histogram Processing

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 38

Histogram Processing

Lecturer: Hossam Abdelmunim


Email : hossameldin.hassan@eng.asu.edu.eg

Computers and Systems Engineering


Histogram Processing
• Histogram of a digital image with gray levels in the
range [0,L-1] is a discrete function
h(rk) = nk
• Where
– rk : the kth gray level
– nk : the number of pixels in the image having gray level rk
– h(rk) : histogram of a digital image with gray levels rk

2
Normalized Histogram (PMF)
• dividing each of histogram at gray level rk by the total
number of pixels in the image, n
pr(rk) = nk / n
• For k = 0,1,…,L-1
• p(rk) gives an estimate of the probability of occurrence
(Probability Mass Function) of gray level rk
• The sum of all components of a normalized histogram
is equal to 1

3
Discrete versus Continuous
Random Variables
Discrete Random Variable Continuous Random Variable
Finite Sample Space Infinite Sample Space
e.g. {0, 1, 2, 3,…,255} e.g. [0,1], [2.1, 5.3]
Probability Mass Function (PMF) Probability Density Function (PDF)
p x i   P X  x i  f x 
1. f ( x )  0 , for all x in R X
1. p (x i )  0, for all i
2.  f ( x)dx  1


2. i 1
p (x i )  1 RX

3. f ( x )  0, if x is not in R X

Cumulative Distribution Function (CDF) p X  x 


p X  x   x i x
p (x i )

4
Histogram Processing
• Basic for numerous spatial domain processing
techniques
• Used effectively for image enhancement
• Information inherent in histograms also is
useful in image compression and
segmentation

5
h(rk) or p(rk)
Example
rk

Dark image
Components of
histogram are
concentrated on the
low side of the gray
scale.
Bright image
Components of
histogram are
concentrated on the
high side of the gray
scale. 6
Example

Low-contrast image
histogram is narrow
and centered toward
the middle of the
gray scale
High-contrast image
histogram covers broad
range of the gray scale
and the distribution of
pixels is not too far from
uniform, with very few
vertical lines being much
higher than the others 9
Desired Histogram for the Best
Contrast is a UNIFORM

10
Histogram Equalization
• As the low-contrast image’s histogram is narrow and
centered toward the middle of the gray scale, if we
distribute the histogram to a wider range the quality
of the image will be improved.

• We can do it by adjusting the probability density


function of the original histogram of the image so
that the probability spread equally

11
Histogram Matching Problem
• Given an Image I with
– Intensity values r  {0, 1, 2,…, 255}
– and PMF, pr(r)

• We want to adjust the intensity values r to construct a


new image J with
– Intensity values z  {0, 1, 2,…, 255}
– and PMF, pz(z)

12
Or Simply

Z=T(r)?
z

13
Conditions of T(r)
• Single-valued (one-to-one relationship) guarantees that
the inverse transformation will exist
• Monotonicity condition preserves the increasing order
from black (0) to white (255) in the output image thus it
won’t cause a negative image
• 0  z = T(r)  255 for 0  r  255 guarantees that the
output gray levels will be in the same range as the input
levels.
• The inverse transformation from z back to r is
r = T -1(z) ; 0  z  255
14
Before we Start the Process:
Remember
• If fx(x) is a PMF of a discrete random variable x, we define the
cumulative distribution function (CDF) Fx (x) as:-
x
Fx ( x)   f x (r ), where x {0,1, 2, ..., 255}
• Note that: r 0
1. P(X  x) = Fx(x)
2. Fx(-) = 0
3. Fx() = 1
4. 0  Fx(x)  1
5. Fx(x1)  Fx(x2) if x1 < x2
6. P(x1 < x  x2) = Fx(x2) –Fx(x1)
15
Histogram Matching Process

16
Step 1
• Compute the PMF, pr(r) of the input image I.
0.04

0.035

0.03

0.025

pr(r)
0.02

0.015

0.01

0.005

0
0 50 100 150 200 250 300

17
Step 2
• Compute the CDF, Pr(r) of the input image I.

0.04 1

0.9
0.035
0.8
0.03
0.7
0.025

s = Pr(r)
0.6
pr(r)

0.02 0.5

0.4
0.015
0.3
0.01
0.2
0.005
0.1

0 0
0 50 100 150 200 250 300 0 50 100 150 200 250

r r

18
Step 3
• Given a histogram pz(z) , Compute its CDF, Pz(z).

19
Step 4
• Construct the map r-z by solving the equation
Pr(r)=Pz(z).

0.9

0.8

0.7
s = Pr(r)

0.6

0.5

0.4

0.3

0.2

0.1

0
0 50 100 150 200 250

20
Step 4 (Continue)
r pr(r) s = Pr(r) z = Pz-1(s)

255

21
Simple Proof
Pz(z)=P(Z≤z)

Pr(r)=P(R≤r)

Z = T(R) and z=T(r)


Pz(z)=P(T(R)≤z)
Pz(z)=P(R≤T-1(z))=P(R≤r)
Hence ,Pz(z)=Pr(r)
Note that dz/dr>0
22
For Histogram Equalization
• pz(z)=1/L if z={0, 1, 2, …, L-1}.

• Pz(z)=(z+1)/L.

• s= Pr(r)=(z+1)/L

• z=L* s-1

• Assuming that the maximum gray level is (L-1).


23
Example

before after Histogram


equalization

24
Example

before after Histogram


equalization

The quality is
not improved
much because
the original
image already
has a broaden
gray-level scale
25
Example
No. of pixels
6
2 3 3 2 5

4 2 4 3 4

3
3 2 3 5
2
2 4 2 4
1
Gray level
4x4 image
0 1 2 3 4 5 6 7 8 9
Gray scale = [0,9]
histogram
26
Gray
0 1 2 3 4 5 6 7 8 9
Level(r)
No. of
0 0 6 5 4 1 0 0 0 0
pixels
k

n
j 0
j 0 0 6 11 15 16 16 16 16 16

k nj 6 11 15 16 16 16 16 16
s 0 0 / / / / / / / /
j 0 n
16 16 16 16 16 16 16 16

z =round( 10*s-1) 0 0 3 6 8 9 9 9 9 9

Note that maximum gray level is 9.


Example (Continue)
No. of pixels
6
3 6 6 3 5

8 3 8 6 4

3
6 3 6 9
2
3 8 3 8
1

Output image
0 1 2 3 4 5 6 7 8 9
Gray scale = [0,9] Gray level
Histogram equalization 28
Equalization of a CT Image

29
Equalization of a Natural Image

30
Another Important Example: Adjust the sneakers-image
such that its new histogram matches that given in the
second row.

31
Results
Initial CDF Desired CDF
1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 50 100 150 200 250 0 50 100 150 200 250

Input Image Resulting Image

32
r-z MAP
260

240

220

200

180

160
z

140

120

100

80

60
0 20 40 60 80 100 120
r

33
Example (Continuous Case)

Assume an image has a gray level probability density


function pr(r) as shown.

pr(r)   2r  2 ;0  r  1
pr ( r )  
2  0 ; elsewhere

1 r

 p ( w )dw  1
0
r

0 1 2 r 34
Example

We would like to apply the histogram specification


(matching) with the desired probability density function
pz(z) as shown.
pz(z)
 2z ;0  z  1
2 pz ( z )  
 0 ; elsewhere
1 z

 p ( w )dw  1
0
z
z
0 1 2 35
Step 1:

Obtain the CDF, s = Pr(r)


r
s=Pr(r)
s  Pr (r )   pr ( w)dw
0
1 r
  (2 w  2)dw
One to one 0
mapping 2 r
function   w  2w
0

0 r   r 2  2r
1
36
Step 2:

Obtain the CDF Pz(z)

z
2 z
Pz (z)   (2 w)dw z z 2
0
0

37
Step 3:

Obtain the r-z mapping

Pz ( z )  Pr (r )
2 2
z   r  2r
2
z  2r  r
We can guarantee that 0  z 1 when 0  r 1
38

You might also like