Spatial Filtering
Spatial Filtering
Neighborhood Operations
•Neighborhood operations simply operate on
a larger Neighborhood of pixels than point
operations Origin x
•Neighbourhoods are
mostly a rectangle
around a central pixel
(x, y)
•Any size rectangle Neighbourhood
y y
The Spatial Filtering Process
Origin x
a b c r s t
d
g
e
h
f
i
* u
x
v
y
w
z
Original Image Filter
Simple 3*3 Pixels
e 3*3 Filter
Neighbourhood
eprocessed = v*e +
r*a + s*b + t*c +
u*d + w*f +
y Image f (x, y) x*g + y*h + z*i
w(s, t ) f ( x s, y t )
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
g ( x, y )
s at b
99 106 98
95 90 85
* 1/
1/
9
1/
1/
9
1/
1/
9
9 9 9
1/ 100
104
9
1/ 108
9
1/
9
Original Image Filter
Simple 3*3 1/ 1/ 1/
3*3 Smoothing Pixels
999 106
9 989
Neighbourhood 195
/9 190
/9 185
/9
Filter
e = 1/9*106 +
1/ *104 + 1/ *100 + 1/ *108 +
9 9 9
1/ *99 + 1/ *98 +
9 9
y Image f (x, y) 1/ *95 + 1/ *90 + 1/ *85
9 9 9
= 98.3333
The above is repeated for every pixel in the
original image to generate the smoothed image
Weighted Smoothing Filters
•More effective smoothing filters can be
generated by allowing different pixels in the
Neighborhood different weights in the
averaging function
1/ 2/ 1/
– Pixels closer to the 16 16 16
central pixel are more
2/ 4/ 2/
important 16 16 16
– Often referred to as a 1/ 2/ 1/
weighted averaging 16 16 16
Weighted
averaging filter
Averaging Filter Vs. Median Filter Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
x
123 127 128 119 115 130
y
Strange Things Happen At The Edges!
e e e
y Image f (x, y)
Strange Things Happen At The Edges!
(cont…)
•There are a few approaches to dealing with
missing edge pixels:
– Omit missing pixels
• Only works with some filters
• Can add extra code and slow down processing
– Pad the image
• Typically with either all white or all black pixels
– Replicate border pixels
– Truncate the image
– Allow pixels wrap around the image
• Can cause some strange image artefacts
Simple Neighborhood Operations Example
x
123 127 128 119 115 130
y
Strange Things Happen At The Edges!
(cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Filtered Image:
Zero Padding
Filtered Image:
Wrap Around Edge Pixels
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
𝑴𝒂𝒔𝒌 = 𝟑 × 𝟑
Image Smoothing Example
𝑴𝒂𝒔𝒌 = 𝟓 × 𝟓
Image Smoothing Example
𝑴𝒂𝒔𝒌 = 𝟗 × 𝟗
Image Smoothing Example
𝑴𝒂𝒔𝒌 = 𝟏𝟓 × 𝟏𝟓
Image Smoothing Example
𝑴𝒂𝒔𝒌 = 𝟑𝟓 × 𝟑𝟓
Sharpening Filters
Sharpening Filters
Objective is to sharpen the image by enhancing/
highlighting the discontinuities in the image.
Forms of Discontinuities:
Isolated Points The pictorial
representation of
Intensity Ramp these will be
shown in the
Intensity Step up-coming slides
Behavior of Derivatives
at Image Discontinuities
The behavior of derivatives during the
transitions into and out of these discontinuities
help us to detect image features (like lines,
edges, etc.) and noise.
Sharpening Filters
Rate of
Spatial Analogous Change of
Differentiation to Intensity
Sharpening Filters
The strength (magnitude) of the response of the
derivative operator is proportional to the degree of
intensity change (or discontinuity).
𝑑𝑓(𝑥) 𝑓 𝑥 + ℎ − 𝑓(𝑥)
= lim
𝑑𝑥 ℎ→0 ℎ
𝜕𝑓(𝑥, 𝑦) 𝜕𝑓(𝑥, 𝑦)
= 𝑓 𝑥 + 1, 𝑦 − 𝑓(𝑥, 𝑦) = 𝑓 𝑥, 𝑦 + 1 − 𝑓(𝑥, 𝑦)
𝜕𝑥 𝜕𝑦
Sharpening Filters
Image sharpening is performed by derivative operators:
2nd Order Derivative
2 f
f ( x 1, y ) f ( x 1, y ) 2 f ( x, y )
x
2
2 f
f ( x, y 1) f ( x, y 1) 2 f ( x, y )
y
2
Another
Image Smoothing
Smoothing Example
Example
Intensity Profile Example-I
A B
Another
Image Smoothing
Smoothing Example
Example
Intensity Profile Example-I
Behavior of Derivatives
at Image Discontinuities
1st Order 2nd Order
Discontinuity
Derivative Derivative
Constant Regions Zero Zero
0 1 0 0 -1 0
Gives an isotropic
result for rotations in 1 -4 1 -1 4 -1
increments of 90°.
0 1 0 0 -1 0
The Laplacian Filter
1 1 1
1 -8 1
Gives an isotropic 1 1 1
result for rotations in
increments of 45°. -1 -1 -1
-1 8 -1
-1 -1 -1
The Laplacian Filter
- =
Original Laplacian Sharpened
Image Filtered Image Image
Laplacian Image Enhancement
Laplacian Image Enhancement
Laplacian Image Enhancement
2 2
𝑀 𝑥, 𝑦 = 𝑚𝑎𝑔 𝛻𝑓 𝑥, 𝑦 = 𝑔𝑥 + 𝑔𝑦
The Gradient Filter
The components of the gradient vector are derivatives, they are
linear operators.
On the other hand, the partial derivatives are not rotation invariant
(isotropic), but the magnitude of the gradient vector is.
𝑔𝑥 = 𝑧9 + 2𝑧8 + 𝑧7 − 𝑧3 + 2𝑧2 + 𝑧1
𝑔𝑦 = 𝑧3 + 2𝑧6 + 𝑧9 − 𝑧1 + 2𝑧4 + 𝑧7
Gradient
Un-Sharp Masking and
High Boost Filtering
Consists of subtracting an unsharp (smoothed) version of an
image from the original image. This process, called unsharp
masking.
(a)
Laplacian filter of
bone scan (a)
(b)
Sharpened version of
bone scan achieved (c)
by subtracting (a)
and (b) Sobel filter of bone
scan (a) (d)
Combining Spatial Enhancement Methods
(cont…)
Result of applying a (h)
power-law trans. to
Sharpened image (g)
which is sum of (a)
and (f) (g)
The product of (c)
and (e) which will be (f)
used as a mask
(e)