0% found this document useful (0 votes)
6 views

Lecture 5 Updated

The document discusses histogram processing in digital image processing, explaining how histograms represent the distribution of pixel intensities in an image. It covers normalization, histogram sliding, contrast stretching, and histogram equalization, detailing their mathematical foundations and applications in enhancing image quality. Additionally, it introduces histogram specification as a method for achieving desired histogram shapes in images.

Uploaded by

safi
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 5 Updated

The document discusses histogram processing in digital image processing, explaining how histograms represent the distribution of pixel intensities in an image. It covers normalization, histogram sliding, contrast stretching, and histogram equalization, detailing their mathematical foundations and applications in enhancing image quality. Additionally, it introduces histogram specification as a method for achieving desired histogram shapes in images.

Uploaded by

safi
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 46

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) = h(rk) / n = nk / MN ,
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 – 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.
18
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.
19
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.
20
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:
21
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
22
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
23
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.
24
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 ( r j )   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
25
of
36
Example
 Rounding to the nearest integer: rk sk ps(sk)
0 1 790/4096=0.19
s0 1.33  1 s1 3.08  3 s2 4.55  5 s3 5.67  6
1 3 1023/4096=0.25
s4 6.23  6 s5 6.65  7 s6 6.86  7 s7 7.00  7
2 5 850/4096=0.21
3,4 6 (656+329)/4069=0.24
5,6, 7 (245+122+81)/
7 4096=0.11

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


26
of
36
Equalization Transformation Function
27
of
36
Equalization Transformation Function
28
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.
29
of
36
Equalization Transformation Example
30
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.
31
of
36
Equalization Transformation Example
32
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
33
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
34
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
35
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
36
of
36
Histogram Specification (cont...)

Histogram
equalization
37
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.
38
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

 Obtain the inverse transform: zq G  1 ( sk ) G  1 (T (rk ))


39
of
Histogram Specification (cont...)
36 Example
 Consider again the 3-bit 64x64 image:
Zk nk
0 0
1 0
2 0
3 614
4 819
5 1230
6 819
7 614

 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.


40
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:

G ( z0 ) 0 G ( z1 ) 0 G ( z2 ) 0 G ( z3 ) 1
G ( z4 ) 2 G ( z5 ) 5 G ( z6 ) 6 G ( z7 ) 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.
41
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
s7 7 G ( z7 ) 7 histogram-equalized image would have a
value of 3 (z3) in the histogram-specified
42
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).
43
of
Histogram Specification (cont...)
36 Example

Specified and actual histograms (the values in the


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

Mapping of values sk into corresponding values zq.


45
of
36
Histogram Specification (cont...)

Original image Histogram equalization


46
of
36
Histogram Specification (cont...)

Specified histogram

Transformation G(zq) (1),


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

Resulting histogram

You might also like