Lecture 5 Updated
Lecture 5 Updated
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)
Before
Contrast = 225
After
Contrast = 240
14
of
36
Contrast Stretching
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
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.
Histogram
equalization
37
of
36
Histogram specification (cont.)
G ( z0 ) 0 G ( z1 ) 0 G ( z2 ) 0 G ( z3 ) 1
G ( z4 ) 2 G ( z5 ) 5 G ( z6 ) 6 G ( z7 ) 7
Specified histogram
Resulting histogram