Digital Image Processing LT 5 Biit Itt F Ti Basic Intensity Transformations
Digital Image Processing LT 5 Biit Itt F Ti Basic Intensity Transformations
Digital Image Processing LT 5 Biit Itt F Ti Basic Intensity Transformations
L t 5 Lecture 5
Basic intensity transformations
Histogram processing
Thresholding
Gonzales & Woods:
Chapter 3 pp. 104-144
Ch t 10 738 761 Chapter 10 pp. 738-761
Chapter 12 pp. 873-875
Gonzales & Woods lacks parts of the following topics: Gonzales & Woods lacks parts of the following topics:
Thresholding with the least error method
Local thresholding
Maria Magnusson, Computer Vision Lab., Dept. of Electrical Engineering, Linkping University
B i i t it t f ti Basic intensity transformations
( ) T( ) r T s = (3.1-2)
Fig. 3.2 Contrast stretching Thresholding function
Negative and Log transformations
( ) r T s ( ) r T s =
(3.1-2)
Fig. 3.3
N ti t f ti Negative transformation
L 1 r L s = 1
(3.2-1)
Fig. 3.4 Original mammogram
More easy to analyze
after negative transformation
L t f ti Log transformation
( ) r c s + 1 log
The log transformation compresses the dynamic
( ) r c s + = 1 log
(3.2-2)
The log transformation compresses the dynamic
range of images with large variations in pixel value.
Fig. 3.5 Fourier transform After Log transformation
Power-Law (Gamma)
t f ti transformations
cr s
cr s =
(3.2-3)
Fig. 3.6
Power-Law transformations for
ti f it gamma correction for monitors
Fig. 3.7
Power-Law transformations for
t t h t contrast enhancement
Original, c=1, O g a ,
too bright
c ,
=3.0
1 1 c=1,
=4.0
c=1,
=5.0,
too dark
Fig. 3.9
Piecewise-linear transformations
f t t h t for contrast enhancement
Original,
too low contrast
Transformation
function
Result,
good contrast
Fig. 3.10
Two methods
Contrast enhancement with
to increase
th t t
inten
the contrast
inten-
sity
trans-
for
Lab 3 task
for-
mation
Original image and
i i l l t bl
Lab 3 task
original color table
color
table
mani-
pula-
tion tion
Contrast enhancement with
i t it t f ti intensity transformation
In general:
( ) ( ) | | y x r T y x s , , =
(3.1-2)
Suppose that we want to increase the contrast of the image:
( ) ( )
s s
s
= 191 64
64
128 2
, 0
r
r
y x r y x s( ) ( )
>
s s =
192
191 64
, 255
, 128 , 2 ,
r
r y x r y x s
255
s
255
r
255
Contrast enhancement with
l t bl i l ti color table manipulation
Pixel value r(x y)
R G B
Pixel value r(x,y)
0:
|
64
0
|
0
0
|
0
0
|
0
Linear
transfor-
mation
64
:
0 0 0
65:
66:
|
2
4
|
2
4
|
2
4
| |
191:
|
254
|
254
192:
|
255
|
|
254
255
|
255
| |
255:
|
255
|
255
|
255
To D/A-converter and
further to the screen.
Computation of
th hi t f ll i the histogram of a small image
Image Histogram
6
h(r
k
)= n
k
r
k
(x,y)
0
0
0
0
4
4
3
3
6
0
0
0
2
4
2
3
2
0 2 2 2
0 1 2 3 4 r
k
0 1 2 3 4 r
k
The normalized histogram is a
PDF b bilit f ti ( ) PDF, a probability function p( )
The histogram of a digital image with intensity
levels in the range [0,L-1] is a discrete function
h(r )=n where r is the k:th intensity value and h(r
k
)=n
k
, where r
k
is the k:th intensity value and
n
k
is the number of pixels in the image with
intensity r
k
. y
k
Denote the total number of pixels in the image by
NM. Then, the normalized histogram p(r
k
)=n
k
/NM
k k
is an estimate of the probability of occurrence of
intensity level r
k
in the image.
Page 120
Histograms of dark and light
i ti l images, respectively
Fig. 3.16
Histograms of low- and high-
t t i ti l contrast images, respectively
Fig. 3.16
The CDF P( ) (cumulative distribution function)
f th li d hi t ( ) of the normalized histogram p( )
( ) ( ) ( )
k r
r p
r
( ) r p
r
r
Discrete
Continuous
k
r r
1 L
1 L
( ) ( )
k r
r P L 1 ( ) ( ) r P L
r
1
( ) ( )
r
}
( )
k
(3.3-4)
(3.3-8)
k
r r
( ) ( ) e e d p r P
r r
}
=
0
( ) ( )
=
=
j
j r k r
r p r P
0
Hi t li ti
Also Lesson 2
Histogram equalization
If th f ll i i t it t f ti i f d th
so esso
If the following intensity transformation is performed, the
histogram will be constant.
(3 3-4) ( ) ( ) ( ) ( ) ( ) e e d p L r P L r T s
r
}
= = = 1 1
The gray scale is utilized maximally
Automatic method
(3.3 4) ( ) ( ) ( ) ( ) ( ) e e d p L r P L r T s
r r
}
= = =
0
1 1
Automatic method
Normalized
histogram before
Normalized
histogram after
Fig. 3.18
Image to utilize for proof
Also Lesson 2
( ) r p
r
( ) r p r
r
A
so esso
s =
r
( ) ( ) r P L
s
r
1
r A
1 L
( ) ( )
1 L
( ) ( )
( )
( ) r dP
L
r p r L
r
r
A
= A
1
1
( ) s p s
s
A
( )
( )
dr
r L
r
A 1
( ) s p
s
r
r A
( ) p
s
1 1 L
Hi t li ti Histogram equalization
Fig. 3.20 Fig. 3.16
Before After
Hi t ifi ti
Also Lesson 2
Histogram specification
( )
so esso
Obtain the normalized histogram p
r
(r) from the input
image and compute s according to (3.3-10)
Use the specified p () in (3 3-11) to obtain G(z) Use the specified p
z
() in (3.3-11) to obtain G(z)
Obtain the inverse transformation z=G
-1
(s)
Obtain the output image by first equalizing the input image p g y q g p g
and then performing the inverse mapping z=G
-1
(s)
(3 3 10)
( ) ( ) ( )
r
}
(3.3-10)
( ) ( ) ( ) e e d p L r T s
r
}
= =
0
1
z
(3.3-11) ( ) ( ) ( ) dt t p L z G
z
}
=
0
1
(3.3-12) ( ) | | ( ) s G r T G z
1 1
= =
Result from histo-
ifi ti
Histogram
gram specification
Histogram
specification
Fig. 3.23
Before
g
Fig. 3.25
After
Comparison with Histogram
li ti equalization
Fig. 3.23
Before
g
Fig. 3.24
After
Th h ldi
hite
Thresholding
( )
white
A l i
( )
( )
( )
s
>
=
T y x f
T y x f
y x g
, if , 0
, if , 1
,
(10.3-1)
A grayscale image
f(x,y) is thresholded
according to:
( )
y f , ,
Black
Histogram
( ) y x g , ( ) y x f , Fig. 10.38
T
The mid-way method
f t ti th h ldi for automatic thresholding
Page 742
( )
0 i iti li
0
i T 0
( ) f p
0 , initialize
0
= i T 0
( ) ( ) | |
i i
T f f p T < =
0
1
( ) ( ) | | f f p
0
1
( ) ( ) | |
i i
T f f p T > =
1
2
( ) ( ) | | 2 /
1 0
1 i i i
T T T + =
+
3
1 : + = i i 4
Lab 4 task Also Le 2
Bayes classifier for
G i f ti
with the class the to
ctor pattern ve Assign the
j x
Gaussian functions
( ) ( )
( ). largest
with the class the to
x d
j x
j
( ) 1
(12.2-17) ( ) ( ) ( ) W j P x p x d
j j j
,..., 2 , 1 , | = = e e
( ) ( ) ( )
( )
( ) 1 , 0 ,
2
1
|
2 2
2
= = =
j P e P f p f d
j
m f
j
j j j
j j
e
o t
e e
o
(12.2-18)
( ) ( ) ( )
0 0 0 0
| P f p P f p = e e
( ) ( ) ( )
1 1 1 1
| P f p P f p = e e
Fig. 12.10
f
0
m
1
m
T fi d th ti l th h ld To find the optimal threshold
Th li d hi t b itt The normalized histogram can be written:
( ) ( ) ( ) f p P f p P f p
1 1 0 0
+ =
Vi look for a f=T such that: ( ) ( ) f p P f p P
1 1 0 0
=
( ) f p P
0 0
( ) f p
( ) f p P
1 1
( ) f p
Fig. 12.10 f
0
T
The least error method
f t ti th h ldi
Not in G&W!
for automatic thresholding
0
0 , initialize
0
= i T 0
1
( ) o P T f f p
i
<
1
2 ( )
1 1 1
, , o P T f f p
i
>
( )
0 0 0
, , o P T f f p <
( ) ( ) = f p P f p P
1 1 0 0
3
( )
1 1 1
, , f f p
4 1 : ,
1
+ = =
+
i i T f
i
Lab 4 task Also Le 2
E 1
Histogram
Ex1
Grayy
scale
image
With the
mid a
With the
least
mid-way
method
Thres-
error
method
Thres-
hold =
90
Thres
hold =
80
E 2
Histogram
Ex2
Grayy
scale
image
With the
mid a
With the
least
mid-way
method
Thres-
error
method
Thres-
hold=
210
Thres
hold=
187
Conclusions for the two methods
f t ti th h ldi for automatic thresholding
Better result with the least error method
Less computations with the mid-way method
The threshold from the mid-way method can be
used as starting threshold for the least error
method method
What is a good starting threshold for the mid-way
method? method?
( ) ( ) f p T =
0
( ) ( ) f p T =
Otsus
th d id
Find the Otsu threshold k
*
such
that o
B
2
, the between class
method, idea
( ) ( )
2 2
2
m m P m m P + o (10 3 14)
B
,
variance, is maximized
( ) ( )
2 2 1 1 G G B
m m P m m P + = o (10.3-14)
=
1 L
G
ip m .
3
-
9
)
=0 i
i G
ip m
(
1
0
.
( )
k
( )
k
1
( )
=
=
i
i
p k P
0
1
(10.3-4) ( )
( )
=
=
i
i
ip
k P
k m
0 1
1
1
1 L
(10.3-6)
1
1
L
(10.3-7) ( )
+ =
=
1
1
2
L
k i
i
p k P (10.3-5) ( )
( )
+ =
=
1
1 2
2
1
L
k i
i
ip
k P
k m
Otsus algorithm, after
ti i l ti equation manipulation
Compute the normalized histogram of the image
Page 747
Compute the normalized histogram of the image
Compute the cumulative sums
1 , ... , 2 , 1 , 0 , = L i p
i
Compute the cumulative sums
( ) 1 , ... , 2 , 1 , 0 ,
0
1
= =
=
L k p k P
k
i
i
(10.3-4)
Compute the cumulative means
0 = i
( ) 1 , ... , 2 , 1 , 0 , = =
L k ip k m
k
i
(10.3-8)
1 L
Compute the global mean
( )
0
=
p
i
i
=
=
1
0
L
i
i G
ip m (10.3-9)
Compute the between-class variance
0 i
(10 3 17) ( ) ( ) ( ) | | ( ) ( ) | | k P k P k m k P m k
2
2
1 = o
Obtain the Otsu threshold k
*
for which o
B
2
is max
(10.3-17) ( ) ( ) ( ) | | ( ) ( ) | | k P k P k m k P m k
G B 1 1 1
1 = o
Otsus algorithm for
t th h ld
Page 755
two thresholds
( ) ( ) ( )
2 2 2
2
P P P + +
Maximize the between-class variance!
(10.3-25)
( ) ( ) ( )
3 3 2 2 1 1
2
G G G B
m m P m m P m m P + + = o
( )
Fig. 10.45
(10.3-30) ( ) ( )
2 1
2
1 0
*
2
*
1
2
, max ,
2 1
k k k k
B
L k k
B
o o
< < <
=
Gray scale image Histogram
Image segmented
into 3 regions
1D L l th h ldi 1D Local thresholding
( )
( ) ( )
+ >
=
elsewhere 0
, , if , 1
,
T y x f y x f
y x g
elsewhere , 0
( )
( ) ( )
>
=
, , if , 1 T y x f y x f
y x g ( )
=
elsewhere , 0
, y x g
2D Local
th h ldi thresholding
A lot of
averaging
Thresholding
Alternative: laplaceNeg-
filtering with a large filter
Lab 4 task
Construction of a laplaceNeg-
filt f l l th h ldi filter for local thresholding
f f f f f - = - - = laplaceneg 25 aver dirac
25 di l l 25 aver dirac laplaceneg =
laplaceneg
dirac aver25
1 1 1 1 1
1 1 1 1 1
0
0
0
0
0
0
0
0
0
0
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1
1
1
1
1
1 1
1
1
0
0 0
0
0
0
0
0
0 -1 -1
-1
-1
-1
-1
-1
-1
-1 24 1 1
1 1 1 1 0 0 0 0 0 -1 -1 -1 -1 -1 1
25 25
OBS! In reality the filters much be larger!
25 25
L l d l b l th h ldi Local and global thresholding
Gray scale image
Thresholding
with a global method
Thresholding
with a local method with a global method with a local method
Note: In this example If n is the stroke width
Fig. 10.49
Note: In this example,
the averaging where
performed only 1-D
If n is the stroke width,
G&W recommends a size of 5xn
for the 1D averaging filter,