8/20/2019 IP 2 Spatial Filtering
1/89
Lecture 2
Intensity Transformationand Spatial Filtering
Department of Electronics & TelecommunicationsEngineering
Ho Phuoc Tien
8/20/2019 IP 2 Spatial Filtering
2/89
2
Outline
1. Gray-level transformation
2. Histogram processing
3. Spatial Filtering
8/20/2019 IP 2 Spatial Filtering
3/89
3
Outline
1. Gray-level transformation
2. Histogram processing
3. Spatial Filtering
8/20/2019 IP 2 Spatial Filtering
4/89
4
1.1. Spatial Domain vs. Transform
Domain
Spatial domain
image plane itself, directly process the intensity values of
the image plane
Transform domain
process the transform coefficients, not directly processthe intensity values of the image plane
8/20/2019 IP 2 Spatial Filtering
5/89
5
1.2. Spatial Domain Process
( , ) [ ( , )])
( , ) : input image
( , ) : output image
: an operator on defined over
a neighborhood of point ( , )
g x y T f x y
f x y
g x y
T f
x y
8/20/2019 IP 2 Spatial Filtering
6/89
6
1.2. Spatial Domain Process
8/20/2019 IP 2 Spatial Filtering
7/89
7
1.2. Spatial Domain Process
Intensity transformation function
( ) s T r
8/20/2019 IP 2 Spatial Filtering
8/89
8
1.3. Some Basic Intensity
Transformation Functions
8/20/2019 IP 2 Spatial Filtering
9/89
9
1.4. Image Negatives
Image negatives
1 s L r
8/20/2019 IP 2 Spatial Filtering
10/89
10
Example: Image Negatives
8/20/2019 IP 2 Spatial Filtering
11/89
11
1.5. Log Transformations
Log Transformations
log(1 ) s c r
8/20/2019 IP 2 Spatial Filtering
12/89
12
Example: Log Transformations
8/20/2019 IP 2 Spatial Filtering
13/89
13
1.6. Power-Law (Gamma)
Transformations
s cr
8/20/2019 IP 2 Spatial Filtering
14/89
14
Example: Gamma Transformations
8/20/2019 IP 2 Spatial Filtering
15/89
15
Example: Gamma Transformations
Cathode ray tube(CRT) devices havean intensity-to-voltage response
that is a powerfunction, withexponents varyingfrom approximately1.8 to 2.5
1/2.5 s r
8/20/2019 IP 2 Spatial Filtering
16/89
16
Example: Gamma Transformations
8/20/2019 IP 2 Spatial Filtering
17/89
17
Example: Gamma Transformations
8/20/2019 IP 2 Spatial Filtering
18/89
18
1.7. Piecewise-Linear
Transformations
• Contrast Stretching —
Expands the range of intensity levels in an image so that it spansthe full intensity range of the recording medium or display device.
• Intensity-level Slicing
—Highlighting a specific range of intensities in an image often is of
interest.
8/20/2019 IP 2 Spatial Filtering
19/89
19
8/20/2019 IP 2 Spatial Filtering
20/89
20
8/20/2019 IP 2 Spatial Filtering
21/89
21
1.8. Bit-plane Slicing
8/20/2019 IP 2 Spatial Filtering
22/89
22
1.8. Bit-plane Slicing
8/20/2019 IP 2 Spatial Filtering
23/89
23
1.8. Bit-plane Slicing
8/20/2019 IP 2 Spatial Filtering
24/89
24
Outline
1. Gray-level transformation
2. Histogram processing
3. Spatial Filtering
8/20/2019 IP 2 Spatial Filtering
25/89
25
2.1. Histogram Processing
Histogram Equalization
Histogram Matching
Local Histogram Processing
Using Histogram Statistics for Image Enhancement
8/20/2019 IP 2 Spatial Filtering
26/89
26
2.1. Histogram
Histogram ( )
is the intensity valueis the number of pixels in the image with intensity
k k
th
k
k k
h r n
r k
n r
Normalized histogram ( )
: the number of pixels in the image of
size M N with intensity
k k
k
k
n p r
MN n
r
What is histogram?
8/20/2019 IP 2 Spatial Filtering
27/89
27
2.1. Histogram
Lena, 512 x 512 Histogram
Example :
8/20/2019 IP 2 Spatial Filtering
28/89
28
8/20/2019 IP 2 Spatial Filtering
29/89
29
2.1. Histogram
Remarks:
Consider a histogram as a probability density
function (pdf)
- Advantages ?
- Disadvantages ?
8/20/2019 IP 2 Spatial Filtering
30/89
30
2.2. Histogram Equalization
The intensity levels in an image may be viewed asrandom variables in the interval [0, L-1].
Let ( ) and ( ) denote the probability density
function (PDF) of random variables and .
r s p r p s
r s
8/20/2019 IP 2 Spatial Filtering
31/89
31
2.2. Histogram Equalization
( ) 0 1 s T r r L
. T(r) is a strictly monotonically increasing function
in the interval 0 -1;
. 0 ( ) -1 for 0 -1.
a
r L
b T r L r L
8/20/2019 IP 2 Spatial Filtering
32/89
32
2.2. Histogram Equalization
. T(r) is a strictly monotonically increasing function
in the interval 0 -1;
. 0 ( ) -1 for 0 -1.
a
r L
b T r L r L
( ) 0 1 s T r r L
( ) is continuous and differentiable.T r
( ) ( ) s r p s ds p r dr (Property ofthe pdf for aRV s=T(r))
8/20/2019 IP 2 Spatial Filtering
33/89
33
2.2. Histogram Equalization
0( ) ( 1) ( )
r
r s T r L p w dw
0
( )
( 1) ( )
r
r
ds dT r d
L p w dwdr dr dr
( 1) ( )
r L p r
( ) 1( ) ( )( )
( 1) ( ) 1r r r
sr
p r dr p r p r p s L p r dsds L
dr
Choose
Uniform
8/20/2019 IP 2 Spatial Filtering
34/89
34
Example
2
Suppose that the (continuous) intensity valuesin an image have the PDF
2 , for 0 r L-1( 1)( )
0, otherwise
Find the transformation function for equalizing
the image histogra
r
r L p r
m.
8/20/2019 IP 2 Spatial Filtering
35/89
35
Example
0
Continuous case:
( ) ( 1) ( )r
r s T r L p w dw
8/20/2019 IP 2 Spatial Filtering
36/89
36
2.2. Histogram Equalization
0
Continuous case:
( ) ( 1) ( )r
r s T r L p w dw
0
Discrete values:
( ) ( 1) ( )k
k k r j
j
s T r L p r
0 0
1( 1) k=0,1,..., L-1
k k j
j
j j
n L L n
MN MN
8/20/2019 IP 2 Spatial Filtering
37/89
37
Example: Histogram Equalization
Suppose that a 3-bit image (L=8) of size 64 × 64 pixels (MN = 4096)
has the intensity distribution shown in following table.Get the histogram equalization transformation function and give theps(sk ) for each sk .
8/20/2019 IP 2 Spatial Filtering
38/89
38
Example: Histogram Equalization
0
0 0
0
( ) 7 ( ) 7 0.19 1.33r j j
s T r p r
11
1 1
0
( ) 7 ( ) 7 (0.19 0.25) 3.08r j
j
s T r p r
3
2 3
4 5
6 7
4.55 5 5.67 6
6.23 6 6.65 7
6.86 7 7.00 7
s s
s s
s s
Roundedvalue
8/20/2019 IP 2 Spatial Filtering
39/89
39
Example: Histogram Equalization
8/20/2019 IP 2 Spatial Filtering
40/89
40
8/20/2019 IP 2 Spatial Filtering
41/89
41
2.3. Histogram Matching
Histogram matching (histogram specification): generate
a processed image that has a specified histogram
Ir, pr Iz, pz
Specified histogramIs, uniform
Ir, pr, and pz known => find Iz
8/20/2019 IP 2 Spatial Filtering
42/89
42
2.3. Histogram Matching
Let ( ) and ( ) denote the continous probabilitydensity functions of the variables and . ( ) is the
specified probability density function.
Let be the random variable with the prob
r z
z
p r p z r z p z
s
0
0
ability
( ) ( 1) ( )
Define a random variable with the probability
( ) ( 1) ( )
r
r
z
z
s T r L p w dw
z
G z L p t dt s
1 1( ) ( ) z G s G T r Compute:
8/20/2019 IP 2 Spatial Filtering
43/89
43
2.3. Histogram Matching - procedure
Obtain pr (r) from the input image and then obtain the values of s
Use the specified PDF and obtain the transformation function G(z)
Mapping from s to z
0( 1) ( )
r
r s L p w dw
0( ) ( 1) ( )
z
z G z L p t dt s
1( ) z G s
8/20/2019 IP 2 Spatial Filtering
44/89
44
Histogram Matching: Example
Assuming continuous intensity values, suppose that an image has
the intensity PDF
Find the transformation function that will produce an image whose
intensity PDF is
2
2, for 0 -1
( 1)( )
0 , otherwise
r
r r L
L p r
2
3
3, for 0 ( -1)( ) ( 1)
0, otherwise
z
z z L p z L
8/20/2019 IP 2 Spatial Filtering
45/89
45
Histogram Matching: Example
Find the histogram equalization transformation for the input image
Find the histogram equalization transformation for the specified histogram
The transformation function
20 0
2( ) ( 1) ( ) ( 1)
( 1)
r r
r
w s T r L p w dw L dw
L
2 3
3 20 0
3( ) ( 1) ( ) ( 1)
( 1) ( 1)
z z
z
t z G z L p t dt L dt s
L L
2
1
r
L
1/32
1/3 1/32 2 2( 1) ( 1) ( 1)
1
r z L s L L r
L
8/20/2019 IP 2 Spatial Filtering
46/89
46
2.3. Histogram Matching: Discrete
Cases• Obtain pr (r j) from the input image and then obtain the values of sk,
round the value to the integer range [0, L-1].
• Use the specified PDF and obtain the transformation function
G(zq), round the value to the integer range [0, L-1].
• Mapping from sk to zq
0 0
( 1)( ) ( 1) ( )
k k
k k r j j
j j
L s T r L p r n
MN
0
( ) ( 1) ( )q
q z i k i
G z L p z s
1( )q k z G s
8/20/2019 IP 2 Spatial Filtering
47/89
47
Example: Histogram Matching
Suppose that a 3-bit image (L=8) of size 64 × 64 pixels (MN = 4096)
has the intensity distribution shown in the following table (on theleft). Get the histogram transformation function and make the outputimage with the specified histogram, listed in the table on the right.
8/20/2019 IP 2 Spatial Filtering
48/89
48
Example: Histogram Matching
Obtain the scaled histogram-equalized values,
Compute all the values of the transformation function G,
0 1 2 3 4
5 6 7
1, 3, 5, 6, 7,
7, 7, 7.
s s s s s
s s s
0
0
0
( ) 7 ( ) 0.00 z j j
G z p z
1 2
3 4
5 6
7
( ) 0.00 ( ) 0.00
( ) 1.05 ( ) 2.45( ) 4.55 ( ) 5.95
( ) 7.00
G z G z
G z G z
G z G z
G z
0
0 0
1 2
65
7
8/20/2019 IP 2 Spatial Filtering
49/89
49
Example: Histogram Matching
8/20/2019 IP 2 Spatial Filtering
50/89
50
Example: Histogram Matching
Obtain the scaled histogram-equalized values,
Compute all the values of the transformation function G,
0 1 2 3 4
5 6 7
1, 3, 5, 6, 7,
7, 7, 7.
s s s s s
s s s
0
0
0
( ) 7 ( ) 0.00 z j j
G z p z
1 2
3 4
5 6
7
( ) 0.00 ( ) 0.00
( ) 1.05 ( ) 2.45( ) 4.55 ( ) 5.95
( ) 7.00
G z G z
G z G z
G z G z
G z
0
0 0
1 2
65
7
s0s2 s3
s5 s6 s7
s1
s4
8/20/2019 IP 2 Spatial Filtering
51/89
51
Example: Histogram Matching
0 1 2 3 4
5 6 7
1, 3, 5, 6, 7,
7, 7, 7.
s s s s s
s s s
0
1
2
3
4
5
6
7
k r
0 3
1 4
2 5
3 6
4 7
5 7
6 7
7 7
k qr z
8/20/2019 IP 2 Spatial Filtering
52/89
52
Example: Histogram Matching
8/20/2019 IP 2 Spatial Filtering
53/89
53
Example: Histogram Matching
8/20/2019 IP 2 Spatial Filtering
54/89
54
z = G-1(s)
2 4 L l Hi P i
8/20/2019 IP 2 Spatial Filtering
55/89
55
2.4. Local Histogram Processing
Define a neighborhood and move its center from pixel topixel
At each location, the histogram of the points in the
neighborhood is computed. Either histogram equalization orhistogram specification transformation function is obtained
Map the intensity of the pixel centered in the
neighborhood
Move to the next location and repeat the procedure
L l Hi t P i
8/20/2019 IP 2 Spatial Filtering
56/89
56
Local Histogram Processing:
Example
2 5 U i Hi t St ti ti f
8/20/2019 IP 2 Spatial Filtering
57/89
57
2.5. Using Histogram Statistics for
Image Enhancement
1
2 22
0
( ) ( ) ( )
L
i i
i
u r r m p r
1
0
( ) L
i i
i
m r p r
1
0
( ) ( ) ( ) L
n
n i i
i
u r r m p r
1 1
0 0
1( , )
M N
x y
f x y MN
1 1
2
0 0
1 ( , ) M N
x y
f x y m MN
Average Intensity
Variance
2 5 U i Hi t St ti ti f
8/20/2019 IP 2 Spatial Filtering
58/89
58
2.5. Using Histogram Statistics for
Image Enhancement
1
0
Local average intensity
( )
denotes a neighborhood
xy xy
L
s i s ii
xy
m r p r
s
12 2
0
Local variance
( ) ( ) xy xy xy
L
s i s s i
i
r m p r
O tli
8/20/2019 IP 2 Spatial Filtering
59/89
59
Outline
1. Gray-level transformation
2. Histogram processing
3. Spatial Filtering
3 1 I t d ti
8/20/2019 IP 2 Spatial Filtering
60/89
60
3.1. Introduction
A spatial filter consists of (a) a neighborhood, and (b)
a predefined operation
Linear spatial filtering of an image of size MxN with afilter of size mxn is given by the expression
( , ) ( , ) ( , )
a b
s a t b g x y w s t f x s y t
a mask
3 1 I t d ti
8/20/2019 IP 2 Spatial Filtering
61/89
61
3.1. Introduction
3 2 S ti l C l ti
8/20/2019 IP 2 Spatial Filtering
62/89
62
3.2. Spatial Correlation
The correlation of a filter ( , ) of size
with an image ( , ), denoted as ( , ) ( , )
w x y m n
f x y w x y f x y
( , ) ( , ) ( , ) ( , )a b
s a t b
w x y f x y w s t f x s y t
3 3 S ti l C l ti
8/20/2019 IP 2 Spatial Filtering
63/89
63
3.3. Spatial Convolution
The convolution of a filter ( , ) of size
with an image ( , ), denoted as ( , ) ( , )
w x y m n
f x y w x y f x y
( , ) ( , ) ( , ) ( , )a b
s a t b
w x y f x y w s t f x s y t
8/20/2019 IP 2 Spatial Filtering
64/89
64
To obtain the valueat this position, placethe mask center here=> result = 7*1=7
Same order as themask, consider an
impulse !
3 4 S thi S ti l Filt
8/20/2019 IP 2 Spatial Filtering
65/89
65
3.4. Smoothing Spatial Filters
Smoothing filters are used for blurring and for noise
reduction
Blurring is used in removal of small details and
bridging of small gaps in lines or curves
Smoothing spatial filters include linear filters and
nonlinear filters.
3 5 Spatial Smoothing Linear
8/20/2019 IP 2 Spatial Filtering
66/89
66
3.5. Spatial Smoothing Linear
Filters
The general implementation for filtering an M N image
with a weighted averaging filter of size m n is given
( , ) ( , )
( , )
( , )
where 2 1
a b
s a t b
a b
s a t b
w s t f x s y t
g x y
w s t
m a
, 2 1.n b
Two Smoothing Averaging Filter
8/20/2019 IP 2 Spatial Filtering
67/89
67
Two Smoothing Averaging Filter
Masks
8/20/2019 IP 2 Spatial Filtering
68/89
68
Example: Gross Representation of
8/20/2019 IP 2 Spatial Filtering
69/89
69
Example: Gross Representation of
Objects
3 6 Order-statistic (Nonlinear)
8/20/2019 IP 2 Spatial Filtering
70/89
70
3.6. Order-statistic (Nonlinear)
Filters
Nonlinear
Based on ordering (ranking) the pixels contained in
the filter mask
Replacing the value of the center pixel with the
value determined by the ranking result
E.g., median filter, max filter, min filter
Example: Use of Median Filtering
8/20/2019 IP 2 Spatial Filtering
71/89
71
Example: Use of Median Filtering
for Noise Reduction
8/20/2019 IP 2 Spatial Filtering
72/89
72
3.7. Sharpening Spatial Filters
Foundation
Laplacian Operator
Unsharp Masking and Highboost Filtering
Using First-Order Derivatives for Nonlinear ImageSharpening — The Gradient
3 7 Sharpening Spatial Filters:
8/20/2019 IP 2 Spatial Filtering
73/89
73
3.7. Sharpening Spatial Filters:
Foundation
The first-order derivative of a one-dimensional function
f(x) is the difference
The second-order derivative of f(x) as the difference
( 1) ( ) f f x f x x
2
2 ( 1) ( 1) 2 ( )
f f x f x f x
x
8/20/2019 IP 2 Spatial Filtering
74/89
74
3 7 Sharpening Spatial Filters:
8/20/2019 IP 2 Spatial Filtering
75/89
75
3.7. Sharpening Spatial Filters:
Laplace Operator
The second-order isotropic derivative operator is the
Laplacian for a function (image) f(x,y)2 2
2
2 2
f f f
x y
2
2 ( 1, ) ( 1, ) 2 ( , )
f f x y f x y f x y
x
2
2 ( , 1) ( , 1) 2 ( , )
f f x y f x y f x y
y
2 ( 1, ) ( 1, ) ( , 1) ( , 1)
- 4 ( , )
f f x y f x y f x y f x y
f x y
Sh i S ti l Filt L l O t
8/20/2019 IP 2 Spatial Filtering
76/89
76
Sharpening Spatial Filters: Laplace Operator
3 7 Sharpening Spatial Filters:
8/20/2019 IP 2 Spatial Filtering
77/89
77
3.7. Sharpening Spatial Filters:
Laplace Operator
Image sharpening in the way of using the Laplacian:
2
2
( , ) ( , ) ( , )
where, ( , ) is input image,
( , ) is sharpenend images,
-1 if ( , ) corresponding to Fig. 3.37(a) or (b)
and 1 if either of the other two filters is us
g x y f x y c f x y
f x y
g x y
c f x y
c
ed.
8/20/2019 IP 2 Spatial Filtering
78/89
78
3 8 Unsharp Masking and
8/20/2019 IP 2 Spatial Filtering
79/89
79
3.8. Unsharp Masking and
Highboost Filtering
Unsharp masking
Sharpen images consists of subtracting an unsharp
(smoothed) version of an image from the original image
e.g., printing and publishing industry
Steps
1. Blur the original image
2. Subtract the blurred image from the original
3. Add the mask to the original
3 8 Unsharp Masking and
8/20/2019 IP 2 Spatial Filtering
80/89
80
3.8. Unsharp Masking and
Highboost Filtering
Let ( , ) denote the blurred image, unsharp masking is
( , ) ( , ) ( , )
Then add a weighted portion of the mask back to the original
( , ) ( , ) * ( , )
mask
mask
f x y
g x y f x y f x y
g x y f x y k g x y
0k
when 1, the process is referred to as highboost filtering.k
8/20/2019 IP 2 Spatial Filtering
81/89
81
Unsharp Masking: Demo
Unsharp Masking and Highboost Filtering: Example
8/20/2019 IP 2 Spatial Filtering
82/89
82
Unsharp Masking and Highboost Filtering: Example
3.9. Image Sharpening based on
8/20/2019 IP 2 Spatial Filtering
83/89
83
First-Order Derivatives
For function ( , ), the gradient of at coordinates ( , )
is defined as
grad( ) x
y
f x y f x y
f
g x f f f g
y
2 2
The of vector , denoted as ( , )
( , ) mag( ) x y
magnitude f M x y
M x y f g g
Gradient Image
3.9. Image Sharpening based on
8/20/2019 IP 2 Spatial Filtering
84/89
84
3.9. Image Sharpening based on
First-Order Derivatives
2 2
The of vector , denoted as ( , )
( , ) mag( ) x y
magnitude f M x y
M x y f g g
( , ) | | | | x y M x y g g
z1 z2 z3
z4 z5 z6
z7 z8 z9
8 5 6 5( , ) | | | | M x y z z z z
3.9. Image Sharpening based on
8/20/2019 IP 2 Spatial Filtering
85/89
85
3.9. Image Sharpening based on
First-Order Derivatives
z1 z2 z3z4 z5 z6
z7 z8 z9
9 5 8 6
Roberts Cross-gradient Operators
( , ) | | | | M x y z z z z
7 8 9 1 2 3
3 6 9 1 4 7
Sobel Operators
( , ) | ( 2 ) ( 2 ) |
| ( 2 ) ( 2 ) |
M x y z z z z z z
z z z z z z
3.9. Image Sharpening based on
8/20/2019 IP 2 Spatial Filtering
86/89
86
First-Order Derivatives
E l
8/20/2019 IP 2 Spatial Filtering
87/89
87
Example
Example:
8/20/2019 IP 2 Spatial Filtering
88/89
88
Combining
SpatialEnhancement
Methods
Goal:
Enhance the
image by
sharpening it
and by bringingout more of the
skeletal detail
Example:
8/20/2019 IP 2 Spatial Filtering
89/89
Combining
SpatialEnhancement
Methods
Goal:
Enhance the
image by
sharpening it
and by bringingout more of the
skeletal detail
Top Related