You are on page 1of 53

Digital Image Processing

Dr. M. Ilyas Fakhir

Lecture-5
2
of
36
Histogram Processing
 In Statistics, Histogram is a graphical representation showing a visual
impression of the distribution of data.
 An Image Histogram is a type of histogram that acts as a graphical
representation of the lightness/color distribution in a digital image. It
plots the number of pixels for each value.
 The histogram of a digital image with gray levels in the range [0, L – 1]
is a discrete function:
h(rk) = nk for k = 0, 1, 2, ….., L – 1
Where:
rk : kth gray level (denote the intensities of an L-level digital image f(x,
y))
nk : # of pixels with having gray level rk
 Massively useful in image processing, especially in segmentation
3
of
36
Histogram Processing
 Example:
 Image (left) has 256 distinct gray levels (8 bits)
 Histogram (right) shows frequency (how many times) each gray
level occurs
 x-axis shows the gray level intensities and y-axis shows the
frequency of intensities
4
of
36
Histogram Processing
 E.g. K = 16, 10 pixels have intensity value = 2
 Histograms: only statistical information
 No indication of location of pixels
5
of
36
Histogram Processing
 It is common practice to normalize a histogram by
dividing each of its values by the total number of
pixels in the image, denoted by n. Thus, a
normalized histogram is given by
p(rk) = nk / n, for k = 0, 1, 2, ….., L – 1.
 Thus, p(rk) gives an estimate of the probability of
occurrence of gray level rk.
 Note that the sum of all components of a normalized
histogram is equal to 1.
 n is also known as MN (rows and columns of an
image)
6
of
36
Histogram of the Image
 The horizontal axis of the histograms are values of rk and
the vertical axis are values of p(rk).
7
of
36
Histogram Sliding
 Brightness is changed by shifting the histogram to left or
right.

+50
8
of
36
Histogram Sliding
 Brightness is changed by shifting the histogram to left or
right.

- 30
9
of
36
Histogram in MATLAB
 h = imhist (f, b)
 Where f, is the input image, h is the histogram, b is number of bins
(tick marks) used in forming the histogram (b = 255 is the default)
 We obtain the normalized histogram simply by using the
expression.
p = imhist (f, b) / numel(f)
numel (f): a MATLAB function that gives the number of elements
in array f (i.e. the number of pixels in an image).
10
of
36
Histogram in MATLAB
 A histogram, A stem graph, A bar graph, A Plot graph
>> h = imhist(f);
>> bar (h);
>> stem (h);
>> plot (h);
11
of
36
Histogram Stretching
Contrast can be increased using:
1. Histogram stretching
2. Histogram equalization

 Contrast is the difference between maximum and minimum pixel intensity. Contrast = 225.
12
of
36
Increasing Contrast

f(x,y) g(x,y)

Low contrast image High contrast image


13
of
36
Contrast Stretching

Before
Contrast = 225

After
Contrast = 240
14
of
36
Contrast Stretching

This formula doesn’t work always. If there is 1 pixel


with intensity 255:

Contrast stretching has


no effect!!!
15
of
36
Introduction to Probability

 PMF and CDF are both related to


probability. They will be used in Histogram
Equalization.
 PMF – Probability Mass Function.
 It gives the probability of each number in
the data set (frequency of each element).
16
of
36
Probability – PMF

 Calculating PMF from image matrix


PMF
Image matrix
0 2 2/25
1 2 7 5 6 1 4 4/25
7 2 3 4 5 2 3 3/25
0 1 5 7 3
3 3 3/25
1 2 5 6 7
6 1 0 3 4 4 2 2/25
5 4 4/25
6 3 3/25
7 4 4/25
17
of
36
Probability – PMF

 Calculating PMF from histogram

Frequency of gray
Not monotonically
level values for an 8
increasing function
bits per pixel image.
18
of
36
Probability – CDF
 CDF – Cumulative Distributed Function
 Cumulative sum of values calculated by PMF

 CDF will be calculated using the histogram


 CDF makes the PDF grow monotonically
 Monotonical growth is necessary for histogram
equalization.
19
of
36
Histogram Equalization
 Histogram Equalization is the process of adjusting intensity values of pixels. The process
which increases the dynamic range of the gray level in a low contrast image to cover full range
of gray levels.
 Assuming initially continuous intensity values, let the variable r denote the intensities of an
image to be processed. As usual, we assume that r is in the range [0, L – 1],
with r = 0 representing black and r = L – 1 representing white.
 For r satisfying these conditions, we focus attention on transformations (intensity mappings) of
the form
s = T (r) 0≤r≤L–1
 that produce an output intensity value s, for a given intensity value r in the input image.
20
of
36
Histogram Equalization
 We assume that
(a) T(r) is a monotonic increasing function in the interval 0 ≤ r ≤ L – 1; and
(b) T(r) must satisfy 0 ≤ T(r) ≤ L – 1 for 0 ≤ r ≤ L – 1.
 (a) Monotonic
increasing function,
showing how
multiple values can
map to a single
value. (b) Strictly
monotonic increasing
function. This is
a one-to-one mapping,
both ways.
21
of
36
Histogram Equalization
 We can view intensities r and s as random variables and their
histograms as probability density functions (PDF) pr(r) and ps(s).
 PDF of the transformed (mapped) variable s can be obtained as:

 A transformation function of particular importance in image


processing is the cumulative distribution function (CDF) of a
random variable:
22
of
36
Histogram Equalization
 It satisfies the first condition as the area under the curve increases
as r increases.
 It satisfies the second condition as for r = L – 1 we have s = L – 1.
 To find ps(s) we have to compute

r
ds dT (r ) d
  ( L  1)  pr ( w) dw  ( L  1) pr (r )
dr dr dr 0
23
of
36
Histogram Equalization
 Substituting this result: ds
 ( L  1) pr (r )
dr
to
dr
ps ( s )  pr (r )
ds
yields Uniform pdf

1 1
ps ( s )  pr (r )  , 0  s  L 1
( L  1) pr (r ) L 1
24
of
36
Histogram Equalization
 The formula for histogram equalisation in the discrete
case is given
k
( L  1) k
sk  T (rk )  ( L  1) pr (rj )   nj
j 0 MN j 0
where
 rk: input intensity
 sk: processed intensity
 nj: the frequency of intensity j
 MN: the number of image pixels.
25
of
36
Example
 A 3-bit 64x64 image has the following intensities:
M = 64, N = 64
k
( L  1) k
sk  T (rk )  ( L  1) pr (rj )   nj
j 0 MN j 0

 Applying histogram equalization:


0
s0  T (r0 )  7 pr (rj )  7 pr (r0 )  1.33
j 0
1
s1  T (r1 )  7 pr (rj )  7 pr (r0 )  7 pr (r1 )  3.08
j 0
26
of
36
Example
 Rounding to the nearest integer:

s0  1.33  1 s1  3.08  3 s2  4.55  5 s3  5.67  6


s4  6.23  6 s5  6.65  7 s6  6.86  7 s7  7.00  7

 (a) Original histogram. (b) Transformation function. (c) Equalized


histogram.
27
of
36
Equalization Transformation Function
28
of
36
Equalization Transformation Function
29
of
36
Equalization Transformation Function
Transformations (1)
to (4) were
obtained using
Eq. given below and
the
histograms of the
images on the left
column of previous
Fig. Mapping of one
intensity value rk in
image 1 to its
corresponding value
s is shown.
30
of
36
Equalization Transformation Example
31
of
36
Equalization Transformation Example

Initial Image Image After Equalization

Notice that the minimum value (52) is now 0 and the


maximum value (154) is now 255.
32
of
36
Equalization Transformation Example
33
of
36
Equalization Transformation Example
No. of pixels

6
2 3 3 2 5

4 2 4 3 4

3 2 3 5 3

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
34
of
36
Equalization Transformation Example
Gray
0 1 2 3 4 5 6 7 8 9
Level(j)
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
3.3 6.1 8.4
sx9 0 0 9 9 9 9 9
3 6 8
35
of
36
Equalization Transformation Example
No. of pixels

3 6 6 3 6

8 3 8 6 5

4
6 3 6 9
3
3 8 3 8
2

1
Output image
0 1 2 3 4 5 6 7 8 9
Gray scale = [0,9] Gray level
Histogram equalization
36
of
36
Histogram Specification

 Histogram equalization does not always provide


the desirable results.

 Image of Phobos (Mars moon) and its histogram.


 Many values near zero in the initial histogram
37
of
36
Histogram Specification (cont...)

Histogram
equalization
38
of
36
Histogram specification (cont.)

 In these cases, it is more useful to specify


the final histogram.
 Problem statement:
 Given pr(r) from the image and the target
histogram pz(z), estimate the transformation
z=T(r).
 The solution exploits histogram
equalization.
39
of
36
Histogram specification (cont…)
 Equalize the initial histogram of the image:
r
s  T (r )  ( L  1)  pr ( w) dw
0
 Equalize the target histogram: G ( z )  T (r )
r
s  G ( z )  ( L  1)  pz ( w) dw
0
1 1
 Obtain the inverse transform: z  G ( s )  G (T (r ))
 In practice, for every value of r in the image:
get its equalized transformation s=T(r).
perform the inverse mapping z=G-1(s), where s=G(z) is
the equalized target histogram.
40
of
36
Histogram specification (cont…)
 Assuming continuous intensity values, suppose that an
image has the intensity PDF
 2r
 , for 0  r  L -1
pr (r )   ( L  1) 2

 0, otherwise

 Find the transformation function that will produce an


image whose intensity PDF is
 3z 2
 , for 0  z  ( L -1)
pz ( z )   ( L  1) 3

 0, otherwise

41
of
36
Histogram specification (cont…)
 Find the histogram equalization transformation for the
input image
2
r r 2w r
s  T (r )  ( L  1)  pr ( w)dw  ( L  1)  dw 
0 0 ( L  1) 2
L 1

 Find the histogram equalization transformation for the


specified histogram
z z 3t 2 z3
G ( z )  ( L  1)  pz (t )dt  ( L  1)  dt  s
0 0 ( L  1) 3
( L  1) 2

 The transformation function


1/3
1/3  2 r
2
 2 1/3
z  ( L  1) s 
2
 ( L  1)   ( L  1)r 
 L  1
42
of
36
Histogram specification (cont…)

 The discrete case:

 Equalize the initial histogram of the image:


k
( L  1) k
sk  T (rk )  ( L  1) pr (rj )   nj
j 0 MN j 0
G ( z )  T (r )
 Equalize the target histogram:
q
sk  G ( zq )  ( L  1) pz (ri )
i 0

1
z
 Obtain the inverse transform: q  G ( s k )  G 1
(T (rk ))
43
of
Histogram Specification (cont...)
36 Example
 Consider again the 3-bit 64x64 image:

 It is desired to transform this histogram to:


pz ( z0 )  0.00 pz ( z1 )  0.00 pz ( z2 )  0.00 pz ( z3 )  0.15
pz ( z4 )  0.20 pz ( z5 )  0.30 pz ( z6 )  0.20 pz ( z7 )  0.15

with z0  0, z1  1, z2  2, z3  3, z4  4, z5  5, z6  6, z7  7.
44
of
Histogram Specification (cont...)
36 Example
 The first step is to equalize the input (as
before):
s0  1, s1  3, s2  5, s3  6, s4  6, s5  7, s6  7, s7  7

 The next step is to equalize the output:


0
G ( z0 )  7 pz ( z j )  0.00  0
j 0

G ( z0 )  0 G ( z1 )  0 G ( z2 )  0 G ( z3 )  1
G ( z 4 )  2 G ( z5 )  5 G ( z 6 )  6 G ( z 7 )  7

 Notice that G(z) is not strictly monotonic. We


must resolve this ambiguity by choosing, e.g.
the smallest value for the inverse mapping.
45
of
Histogram Specification (cont...)
36 Example
Perform inverse mapping: find the smallest value
of zq that is closest to sk:
sk  T (ri ) G ( zq ) sk  zq
s0  1 G ( z0 )  0 1 3
s1  3 G ( z1 )  0
34
s2  5 G ( z2 )  0
s3  6 G ( z3 )  1 55
s4  6 G ( z4 )  2 66
s5  7 G ( z5 )  5
77
s6  7 G ( z6 )  6
e.g. every pixel with value s0=1 in the histogram-
s7  7 G ( z7 )  7 equalized image would have a value of 3 (z3) in
the histogram-specified image.
46
of
Histogram Specification (cont...)
36 Example
 Notice that due to discretization, the resulting
histogram will rarely be exactly the same as the
desired histogram.

(a) Histogram of a 3-bit


image.
(b) Specified histogram.
(c) Transformation
function obtained from the
specified histogram.
(d) Result of histogram
specification.
Compare the histograms in
(b) and (d).
47
of
Histogram Specification (cont...)
36 Example

Specified and actual histograms (the values in the


third column are computed in previous example.)
48
of
Histogram Specification (cont...)
36 Example
rk
0
1
2
3
4
5
6
7

Mapping of values sk into corresponding values zq.


49
of
36
Histogram Specification (cont...)

Original image Histogram equalization


50
of
36
Histogram Specification (cont...)

Specified histogram

Transformation G(zq) (1),


and G-1(sk) (2).
Result of
histogram
specification.

Resulting histogram
51
of
36
Histogram Equalization
 Adjust 2 different images to make their histograms (intensity
distributions) similar
 Apply a point operation that changes histogram of modified
image into uniform distribution
52
of
36
Linear Histogram Equalization
 Histogram cannot be made exactly flat – peaks cannot be
increased or decreased by point operations.
 Following point operation makes histogram as flat as
possible:
(assuming M x N image and pixels in range [0, K ‐ 1])
53
of
Effects of Linear Histogram
36
Equalization

You might also like