University of Ioannina - Department of Computer Science
Intensity Transformations(Histogram Processing)
Christophoros [email protected]
Digital Image Processing
2
C. Nikou – Digital Image Processing (E12)
Contents
Over the next few lectures we will look at image enhancement techniques working in the spatial domain:
– Histogram processing– Spatial filtering – Neighbourhood operations
3
C. Nikou – Digital Image Processing (E12)
Image Histograms
The histogram of an image shows us the distribution of grey levels in the imageMassively useful in image processing, especially in segmentation
Grey Levels
Freq
uenc
ies
4
C. Nikou – Digital Image Processing (E12)
Histogram ExamplesIm
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
5
C. Nikou – Digital Image Processing (E12)
Histogram Examples (cont…)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
6
C. Nikou – Digital Image Processing (E12)
Histogram Examples (cont…)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
7
C. Nikou – Digital Image Processing (E12)
Histogram Examples (cont…)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
8
C. Nikou – Digital Image Processing (E12)
Histogram Examples (cont…)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
9
C. Nikou – Digital Image Processing (E12)
Histogram Examples (cont…)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
10
C. Nikou – Digital Image Processing (E12)
Histogram Examples (cont…)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
11
C. Nikou – Digital Image Processing (E12)
Histogram Examples (cont…)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
12
C. Nikou – Digital Image Processing (E12)
Histogram Examples (cont…)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
13
C. Nikou – Digital Image Processing (E12)
Histogram Examples (cont…)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
14
C. Nikou – Digital Image Processing (E12)
Histogram Examples (cont…)
• A selection of images and their histograms
• Notice the relationships between the images and their histograms
• Note that the high contrast image has the most evenly spaced histogram
Imag
es ta
ken
from
Gon
zale
z &
Woo
ds, D
igita
l Im
age
Pro
cess
ing
(200
2)
15
C. Nikou – Digital Image Processing (E12)
Contrast Stretching
• We can fix images that have poor contrast by applying a pretty simple contrast specification
• The interesting part is how do we decide on this transformation function?
16
C. Nikou – Digital Image Processing (E12)
Histogram Equalisation
• Spreading out the frequencies in an image (or equalising the image) is a simple way to improve dark or washed out images.
• At first, the continuous case will be studied:– r is the intensity of the image in [0, L-1].– we focus on transformations s=T(r):• T(r) is strictly monotonically increasing.• T(r) must satisfy:
0 ( ) 1, 0 1T r L for r L≤ ≤ − ≤ ≤ −
17
C. Nikou – Digital Image Processing (E12)
Histogram Equalisation (cont...)
• The condition for T(r) to be monotonically increasing guarantees that ordering of the output intensity values will follow the ordering of the input intensity values (avoids reversal of intensities).
• If T(r) is strictly monotonically increasing then the mapping from s back to r will be 1-1.
• The second condition (T(r) in [0,1]) guarantees that the range of the output will be the same as the range of the input.
18
C. Nikou – Digital Image Processing (E12)
Histogram Equalisation (cont...)
a) We cannot perform inverse mapping (from s to r).b) Inverse mapping is possible.
19
C. Nikou – Digital Image Processing (E12)
Histogram Equalisation (cont...)
• We can view intensities r and s as random variables and their histograms as probability density functions (pdf) pr(r) and ps(s).
• Fundamental result from probability theory:– If pr(r) and T(r) are known and T(r) is
continuous and differentiable, then
1( ) ( ) ( )s r rdrp s p r p r
ds dsdr
= =
20
C. Nikou – Digital Image Processing (E12)
Histogram Equalisation (cont...)
• The pdf of the output is determined by the pdf of the input and the transformation.
• This means that we can determine the histogram of the output image.
• A transformation of particular importance in image processing is the cumulative distribution function (CDF) of a random variable:
0
( ) ( 1) ( )r
rs T r L p w dw= = − ∫
21
C. Nikou – Digital Image Processing (E12)
Histogram Equalisation (cont...)
• It satisfies the first condition as the area under the curve increases as r increases.
• It satisfies the second condition as for r=L-1we have s=L-1.
• To find ps(s) we have to compute
dsdr 0
( 1) ( )r
rdL p w dwdr
= − ∫ ( 1) ( )rL p r= −( )dT rdr
=
22
C. Nikou – Digital Image Processing (E12)
Histogram Equalisation (cont...)
Substituting this result:
to
yields
( ) ( )s rdrp s p rds
=
( 1) ( )rds L p rdr
= −
1( ) ( )( 1) ( )s r
r
p s p rL p r
=−
1 , 0 11
s LL
= ≤ ≤ −−
Uniform pdf
23
C. Nikou – Digital Image Processing (E12)
Histogram Equalisation (cont...)
The formula for histogram equalisation in the discrete case is given
where• rk: input intensity• sk: processed intensity• nj: the frequency of intensity j• MN: the number of image pixels.
0( ) ( 1) ( )
k
k k r jj
s T r L p r=
= = − ∑0
( 1) k
jj
L nMN =
−= ∑
24
C. Nikou – Digital Image Processing (E12)
Histogram Equalisation (cont...)Example
A 3-bit 64x64 image has the following intensities:
Applying histogram equalization: 0
0 0 00
( ) 7 ( ) 7 ( ) 1.33r j rj
s T r p r p r=
= = = =∑1
1 1 0 10
( ) 7 ( ) 7 ( ) 7 ( ) 3.08r j r rj
s T r p r p r p r=
= = = + =∑
0( ) ( 1) ( )
k
k k r jj
s T r L p r=
= = − ∑
25
C. Nikou – Digital Image Processing (E12)
Histogram Equalisation (cont...)Example
Rounding to the nearest integer:
0 1 2 3
4 5 6 7
1.33 1 3.08 3 4.55 5 5.67 66.23 6 6.65 7 6.86 7 7.00 7
s s s ss s s s
= → = → = → = →= → = → = → = →
26
C. Nikou – Digital Image Processing (E12)
Histogram Equalization (cont…)Example
Notice that due to discretization, the resulting histogram will rarely be perfectly flat. However, it will be extended.
Imag
es ta
ken
from
Gon
zale
z &
Woo
ds, D
igita
l Im
age
Pro
cess
ing
(200
2)
27
C. Nikou – Digital Image Processing (E12)
Equalisation Transformation FunctionIm
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
28
C. Nikou – Digital Image Processing (E12)
Equalisation ExamplesIm
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
1
29
C. Nikou – Digital Image Processing (E12)
Equalisation Transformation Functions
The functions used to equalise the images in the previous example
Imag
es ta
ken
from
Gon
zale
z &
Woo
ds, D
igita
l Im
age
Pro
cess
ing
(200
2)
30
C. Nikou – Digital Image Processing (E12)
Equalisation ExamplesIm
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
2
31
C. Nikou – Digital Image Processing (E12)
Equalisation Transformation Functions
The functions used to equalise the images in the previous example
Imag
es ta
ken
from
Gon
zale
z &
Woo
ds, D
igita
l Im
age
Pro
cess
ing
(200
2)
32
C. Nikou – Digital Image Processing (E12)
Equalisation Examples (cont…)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002) 3
4
33
C. Nikou – Digital Image Processing (E12)
Equalisation Transformation Functions
The functions used to equalise the images in the previous examples
Imag
es ta
ken
from
Gon
zale
z &
Woo
ds, D
igita
l Im
age
Pro
cess
ing
(200
2)
34
C. Nikou – Digital Image Processing (E12)
Histogram Specification
• Histogram equalization does not always provide the desirable results.
Imag
es ta
ken
from
Gon
zale
z &
Woo
ds, D
igita
l Im
age
Pro
cess
ing
(200
2)
• Image of Phobos (Mars moon) and its histogram.• Many values near zero in the initial histogram
35
C. Nikou – Digital Image Processing (E12)
Histogram Specification (cont...)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
Histogram equalization
36
C. Nikou – Digital Image Processing (E12)
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.
Imag
es ta
ken
from
Gon
zale
z &
Woo
ds, D
igita
l Im
age
Pro
cess
ing
(200
2)
37
C. Nikou – Digital Image Processing (E12)
Histogram specification (cont…)
•Equalize the initial histogram of the image:
•Equalize the target histogram:
•Obtain the inverse transform: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.
Imag
es ta
ken
from
Gon
zale
z &
Woo
ds, D
igita
l Im
age
Pro
cess
ing
(200
2)
0
( ) ( 1) ( )r
rs T r L p w dw= = − ∫
0
( ) ( 1) ( )r
zs G z L p w dw= = − ∫1( )z G s−= 1( ( ))G T r−=
( ) ( )G z T r=
38
C. Nikou – Digital Image Processing (E12)
Histogram specification (cont…)
The discrete case:
•Equalize the initial histogram of the image:
•Equalize the target histogram:
•Obtain the inverse transform:
Imag
es ta
ken
from
Gon
zale
z &
Woo
ds, D
igita
l Im
age
Pro
cess
ing
(200
2)
0( ) ( 1) ( )
k
k k r jj
s T r L p r=
= = − ∑
1( )q kz G s−= 1( ( ))kG T r−=
( ) ( )G z T r=0
( 1) k
jj
L nMN =
−= ∑
0( ) ( 1) ( )
q
k q z ii
s G z L p r=
= = − ∑
39
C. Nikou – Digital Image Processing (E12)
Histogram Specification (cont...)Example
Consider again the 3-bit 64x64 image:
It is desired to transform this histogram to:
with
0 1 2 3
4 5 6 7
( ) 0.00 ( ) 0.00 ( ) 0.00 ( ) 0.15( ) 0.20 ( ) 0.30 ( ) 0.20 ( ) 0.15
z z z z
z z z z
p z p z p z p zp z p z p z p z
= = = == = = =
0 1 2 3 4 5 6 70, 1, 2, 3, 4, 5, 6, 7.z z z z z z z z= = = = = = = =
40
C. Nikou – Digital Image Processing (E12)
Histogram Specification (cont...)Example
The first step is to equalize the input (as before):
The next step is to equalize the output:
Notice that G(z) is not strictly monotonic. We must resolve this ambiguity by choosing, e.g. the smallest value for the inverse mapping.
0 1 2 3 4 5 6 71, 3, 5, 6, 6, 7, 7, 7s s s s s s s s= = = = = = = =
0 1 2 3
4 5 6 7
( ) 0 ( ) 0 ( ) 0 ( ) 1( ) 2 ( ) 5 ( ) 6 ( ) 7
G z G z G z G zG z G z G z G z
= = = == = = =
41
C. Nikou – Digital Image Processing (E12)
Histogram Specification (cont...)Example
Perform inverse mapping: find the smallest value of zq that is closest to sk:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
( ) ( )1 ( ) 03 ( ) 05 ( ) 06 ( ) 16 ( ) 27 ( ) 57 ( ) 67 ( ) 7
k i qs T r G zs G zs G zs G zs G zs G zs G zs G zs G z
== == == == == == == == =
k qs z→
1 3→
3 4→
5 5→
6 6→
7 7→e.g. every pixel with value s0=1 in the histogram-equalized image would have a value of 3 (z3) in the histogram-specified image.
42
C. Nikou – Digital Image Processing (E12)
Histogram Specification (cont...)Example
Notice that due to discretization, the resulting histogram will rarely be exactly the same as the desired histogram.
Imag
es ta
ken
from
Gon
zale
z &
Woo
ds, D
igita
l Im
age
Pro
cess
ing
(200
2)
43
C. Nikou – Digital Image Processing (E12)
Histogram Specification (cont...)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
Histogram equalizationOriginal image
44
C. Nikou – Digital Image Processing (E12)
Histogram Specification (cont...)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
Histogram equalization
45
C. Nikou – Digital Image Processing (E12)
Histogram Specification (cont...)Im
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
Specified histogram
Transformation function and its inverse
Resulting histogram
46
C. Nikou – Digital Image Processing (E12)
Local Histogram ProcessingIm
ages
take
n fro
m G
onza
lez
& W
oods
, Dig
ital I
mag
e P
roce
ssin
g (2
002)
• Image in (a) is slightly noisy but the noise is imperceptible.• HE enhances the noise in smooth regions (b).• Local HE reveals structures having values close to the values of the
squares and small sizes to influence HE (c).
47
C. Nikou – Digital Image Processing (E12)
Summary
We have looked at:– Different kinds of image enhancement– Histograms– Histogram equalisation– Histogram specification
Next time we will start to look at spatial filtering and neighbourhood operations