IP - 2 - Spatial Filtering
IP - 2 - Spatial Filtering
Image Processing
Lecture 2
Intensity transformation and Spatial
Filtering
Outline
Gray-level transformation
Histogram processing
Spatial Filtering
2
Outline
Gray-level transformation
Histogram processing
Spatial Filtering
3
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 process
the intensity values of the image plane
4
Spatial Domain Process
𝐠 𝐱, 𝐲 = 𝐓[𝐟(𝐱, 𝐲)]
5
Spatial Domain Process
6
Spatial Domain Process
7
Some Basic Intensity
Transformation Functions
8
Image Negatives
Image negatives:
s=L–1-r
9
Example: Image Negatives
10
Log Transformations
Log Transformations:
s = c log(1+r)
11
Example: Log Transformations
12
Power-Law (Gamma)
Transformations
𝐬 = 𝐜𝐫 𝛄
13
Example: Gamma Transformations
14
Example: Gamma Transformations
15
Example: Gamma Transformations
16
Example: Gamma Transformations
17
Piecewise-Linear Transformations
Contrast Stretching
Expands the range of intensity levels in an image so
that it spans the 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.
18
19
20
Bit-plane Slicing
21
Bit-plane Slicing
22
Bit-plane Slicing
23
Outline
Gray-level transformation
Histogram processing
Spatial Filtering
24
Histogram Processing
Histogram Equalization
Histogram Matching
25
Histogram
What is histogram?
Histogram h(rk) = nk
rk is the kth intensity value
nk is the number of pixels in the image with intensity rk
𝐧𝐤
Normalized histogram 𝐩 𝐫𝐤 =
𝐌𝐍
nk is the number of pixels in the image of size MxN
with intensity rk
26
Histogram
Example :
Remarks:
- Advantages ?
- Disadvantages ?
29
Histogram Equalization
The intensity levels in an image may be viewed as
random variables in the interval [0, L-1]
Let pr(r) and ps(s) denote the probability density function
(pdf) of random variables r and s
30
Histogram Equalization
𝐬=𝐓 𝐫 𝟎≤𝐫≤𝐋−𝟏
T(r) is a strictly monotonically increasing function in
the interval 𝟎 ≤ 𝐫 ≤ 𝐋 − 𝟏
𝟎 ≤ 𝐓(𝐫) ≤ 𝐋 − 𝟏 for 𝟎 ≤ 𝐫 ≤ 𝐋 − 𝟏
31
Histogram Equalization
𝐬=𝐓 𝐫 𝟎≤𝐫≤𝐋−𝟏
T(r) is a strictly monotonically increasing function in
the interval 𝟎 ≤ 𝐫 ≤ 𝐋 − 𝟏
𝟎 ≤ 𝐓(𝐫) ≤ 𝐋 − 𝟏 for 𝟎 ≤ 𝐫 ≤ 𝐋 − 𝟏
32
Histogram Equalization
𝐫
Choose 𝐬 = 𝐓 𝐫 = (𝐋 − 𝟏) 𝐩𝐫 𝐰 𝐝𝐰
𝟎
𝐫
𝐝𝐬 𝐝𝐓(𝐫) 𝐝
= = 𝐋−𝟏 𝐩𝐫 𝐰 𝐝𝐰 = (𝐋 − 𝟏)𝐩𝐫 (𝐫)
𝐝𝐫 𝐝𝐫 𝐝𝐫 𝟎
𝐩𝐫 𝐫 𝐝𝐫 𝐩𝐫 𝐫 𝐩𝐫 𝐫 𝟏
𝐩𝐬 𝐬 = = 𝐝𝐬 =
𝐝𝐬 (𝐋 − 𝟏)𝐩𝐫 (𝐫) = 𝐋 − 𝟏
𝐝𝐫
Uniform
33
Example
Suppose that the (continuous) intensity values
in an image have the PDF
2r
, for 0 r L-1
pr (r ) ( L 1) 2
0, otherwise
34
Example
Continuous case:
r
s T (r ) ( L 1) pr ( w)dw
0
35
Histogram Equalization
𝐫
Continuous case: 𝐬 = 𝐓 𝐫 = (𝐋 − 𝟏) 𝐩𝐫 𝐰 𝐝𝐰
𝟎
Discrete values:
𝐤
𝐬𝐤 = 𝐓 𝐫𝐤 = 𝐋 − 𝟏 𝐩𝐫 𝐫𝐣
𝐣=𝟎
𝐤 𝐤
𝐧𝐣 𝐋−𝟏
= 𝐋−𝟏 = 𝐧𝐣 𝐤 = 𝟎, 𝟏, . . , 𝐋 − 𝟏
𝐌𝐍 𝐌𝐍
𝐣=𝟎 𝐣=𝟎
36
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 the
ps(sk) for each sk.
37
Example:
Histogram
Equalization
Rounded
value
0
s0 T (r0 ) 7 pr (rj ) 7 0.19 1.33 1
j 0
1
s1 T (r1 ) 7 pr (rj ) 7 (0.19 0.25) 3.08 3
j 0
s2 4.55 5 s3 5.67 6
s4 6.23 6 s5 6.65 7
s6 6.86 7 s7 7.00 7 38
Example: Histogram Equalization
39
40
Histogram Matching
Ir, pr Iz, pz
0, otherwise
Find the transformation function that will produce an image whose
intensity PDF is
3z 2
, for 0 z ( L -1)
pz ( z ) ( L 1) 3
0, otherwise
44
Histogram Matching: Example
Find the histogram equalization transformation for the input image
2
r r 2w r
s T (r ) ( L 1) pr (w)dw ( L 1) dw
0 0 ( L 1) 2
L 1
z z 3t 2 z3
G( z ) ( L 1) pz (t )dt ( L 1) dt s
0 0 ( L 1) 3
( L 1) 2
𝐆 𝐳𝐪 = (𝐋 − 𝟏) 𝐩𝐳 𝐳𝐢 = 𝐬𝐤
𝐢=𝟎
47
Example: Histogram
Matching
Obtain the scaled histogram-equalized values,
s0 1, s1 3, s2 5, s3 6, s4 7,
s5 7, s6 7, s7 7.
Compute all the values of the transformation function G,
0
G( z0 ) 7 pz ( z j ) 0.00 0
j 0
G ( z1 ) 0.00 0 G ( z2 ) 0.00 0
G ( z3 ) 1.05 1 G ( z4 ) 2.45 2
G ( z5 ) 4.55 5 G ( z6 ) 5.95 6
G ( z7 ) 7.00 7
48
Example: Histogram Matching
49
Example: Histogram Matching
Obtain the scaled histogram-equalized values,
s0 1, s1 3, s2 5, s3 6, s4 7,
s5 7, s6 7, s7 7.
Compute all the values of the transformation function G,
0
G( z0 ) 7 pz ( z j ) 0.00 0
j 0
G ( z1 ) 0.00 0 G ( z2 ) 0.00 0
G ( z3 ) 1.05 1 s0 G ( z4 ) 2.45 2 s1
G ( z5 ) 4.55 5 s2 G ( z6 ) 5.95 6 s3
G ( z7 ) 7.00 7 s4 s5 s6 s7
50
Example: Histogram Matching
s0 1, s1 3, s2 5, s3 6, s4 7, rk zq
s5 7, s6 7, s7 7.
03
1 4
rk
0
25
1 36
2 47
3
4
57
5 67
6 77
7 51
Example: Histogram Matching
52
Example: Histogram Matching
53
z = G-1(s)
54
Local Histogram Processing
55
Local Histogram Processing:
Example
56
Using Histogram Statistics for
Image Enhancement
Average Intensity
𝐋−𝟏 𝐌−𝟏 𝐍−𝟏
𝟏
𝐦= 𝐫𝐢 𝐩 𝐫𝐢 = 𝐟(𝐱, 𝐲)
𝐌𝐍
𝐢=𝟎 𝐱=𝟎 𝐲=𝟎
𝐋−𝟏
𝐮𝐧 (𝐫) = 𝐫𝐢 − 𝐦 𝐧 𝐩(𝐫𝐢 )
𝐢=𝟎
Variance
𝐋−𝟏 𝐌−𝟏 𝐍−𝟏
𝟏
𝛔𝟐 = 𝐮𝟐 𝐫 = 𝐫𝐢 − 𝐦 𝟐𝐩 𝐫𝐢 = 𝐟 𝐱, 𝐲 − 𝐦 𝟐
𝐌𝐍
𝐢=𝟎 𝐱=𝟎 𝐲=𝟎 57
Using Histogram Statistics for
Image Enhancement
Local Average Intensity
𝐋−𝟏
𝐦𝐬𝐱𝐲 = 𝐫𝐢 𝐩𝐬𝐱𝐲 𝐫𝐢
𝐢=𝟎
𝐬𝐱𝐲 denotes a neighborhood
Local Variance
𝐋−𝟏
𝟐
𝛔𝟐 𝐬𝐱𝐲 = 𝐫𝐢 − 𝐦𝐬𝐱𝐲 𝐩𝐬𝐱𝐲 𝐫𝐢
𝐢=𝟎
58
Outline
Gray-level transformation
Histogram processing
Spatial Filtering
59
Introduction
A spatial filter consists of a neighborhood and a
predefined operation
Linear spatial filtering of an image of size MxN with a
filter of size mxn is given by the expression
𝐚 𝐛
𝐠 𝐱, 𝐲 = 𝐰 𝐬, 𝐭 𝐟(𝐱 + 𝐬, 𝐲 + 𝐭)
𝐬=−𝐚 𝐭=−𝐛
a mask
60
Introduction
61
Spatial Correlation
𝐰 𝐱, 𝐲 ∗ 𝐟 𝐱, 𝐲 = 𝐰 𝐬, 𝐭 𝐟(𝐱 + 𝐬, 𝐲 + 𝐭)
𝐬=−𝐚 𝐭=−𝐛
62
Spatial Convolution
𝐰(𝐱, 𝐲) ⋆ 𝐟 𝐱, 𝐲 = 𝐰 𝐬, 𝐭 𝐟(𝐱 − 𝐬, 𝐲 − 𝐭)
𝐬=−𝐚 𝐭=−𝐛
63
To obtain the value
at this position, place
the mask center here
=> result = 7*1=7
64
Smoothing Spatial Filters
65
Spatial Smoothing Linear Filters
66
Two Smoothing Averaging Filter
Masks
67
68
Example: Gross Representation of
Objects
69
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
70
Example: Use of Median
Filtering for Noise Reduction
71
Sharpening Spatial Filters
Foundation
Laplacian Operator
Unsharp Masking and Highboost Filtering
Using First-Order Derivatives for Nonlinear Image
Sharpening — The Gradient
72
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
𝛛𝟐 𝐟
𝟐
= 𝐟 𝐱 + 𝟏 + 𝐟 𝐱 − 𝟏 − 𝟐𝐟(𝐱)
𝛛𝐱
73
74
Sharpening Spatial Filters:
Laplace Operator
The second-order isotropic derivative operator is the
Laplacian for a function (image) f(x,y):
𝛛𝟐𝐟 𝛛𝟐𝐟
𝛁𝟐 𝐟 = 𝟐 + 𝟐
𝛛𝐱 𝛛𝐲
𝛛𝟐 𝐟
𝟐
= 𝐟 𝐱 + 𝟏, 𝐲 + 𝐟 𝐱 − 𝟏, 𝐲 − 𝟐𝐟 𝐱, 𝐲
𝛛𝐱
𝛛𝟐 𝐟
𝟐
= 𝐟 𝐱, 𝐲 + 𝟏 + 𝐟 𝐱, 𝐲 − 𝟏 − 𝟐𝐟(𝐱, 𝐲)
𝛛𝐲
𝛁𝟐 𝐟 = 𝐟 𝐱 + 𝟏, 𝐲 + 𝐟 𝐱 − 𝟏, 𝐲 + 𝐟 𝐱, 𝐲 + 𝟏 + 𝐟 𝐱, 𝐲 − 𝟏 − 𝟒𝐟 𝐱, 𝐲
75
Sharpening Spatial Filters: Laplace Operator
76
Sharpening Spatial Filters:
Laplace Operator
Image sharpening in the way of using the Laplacian:
𝐠 𝐱, 𝐲 = 𝐟 𝐱, 𝐲 + 𝐜 𝛁𝟐 𝐟(𝐱, 𝐲)
where
f(x,y) is input image
g(x,y) is sharpened image
c = -1 if 𝛁𝟐 𝐟(𝐱, 𝐲) corresponding to Fig. 3.37 (a) or (b)
and c = 1 if either of the other two filters is used.
77
78
Unsharp Masking and
Highboost Filtering
Unsharp masking
Sharpening 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 (from step 2) to the original
79
Unsharp Masking and
Highboost Filtering
80
Unsharp Masking: Demo
81
Unsharp Masking and Highboost Filtering: Example
82
Image Sharpening based on
First-Order Derivatives
For function f(x,y) the gradient of f at coordinates (x,y) is
defined as
𝛛𝐟
𝐠𝐱 𝛛𝐱
𝛁𝐟 ≡ 𝐠𝐫𝐚𝐝 𝐟 ≡ 𝐠 = 𝛛𝐟
𝐲
𝛛𝐲
𝐌 𝐱, 𝐲 = 𝐦𝐚𝐠 𝛁𝐟 = 𝐠 𝐱𝟐 + 𝐠 𝐲𝟐
Gradient Image
83
Image Sharpening based on First-
Order Derivatives
The magnitude of vector 𝛁𝐟 is denoted as M(x,y)
𝐌 𝐱, 𝐲 = 𝐦𝐚𝐠 𝛁𝐟 = 𝐠 𝐱𝟐 + 𝐠 𝐲𝟐
𝐌 𝐱, 𝐲 ≈ 𝐠 𝐱 + 𝐠 𝐲
𝐌 𝐱, 𝐲 ≈ 𝐳𝟖 − 𝐳𝟓 + 𝐳𝟔 − 𝐳𝟓
z1 z 2 z 3
z4 z 5 z 6
z7 z 8 z 9
84
Image Sharpening based on First-
Order Derivatives
85
Image Sharpening based on First-
Order Derivatives
86
Example
87
Example:
Combining
Spatial
Enhancement
Methods
Goal:
Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail
88
Example:
Combining
Spatial
Enhancement
Methods
Goal:
Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail
89