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
and any shape filter
are possible
y Image f (x, y)
Simple Neighborhood Operations
•Some simple Neighborhood operations
include:
– Min: Set the pixel value to the minimum in
the neighbourhood
– Max: Set the pixel value to the maximum in
the neighbourhood
– Median: The median value of a set of
numbers is the midpoint value in that set (e.g.
from the set [1, 7, 15, 18, 24] 15 is the
median). Sometimes the median works better
than the average
Simple Neighborhood Operations Example
Original Image x Enhanced Image x
123 127 128 119 115 130
140 145 148 153 167 172
133 154 183 192 194 191
194 199 207 210 198 195
164 170 175 162 173 151
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
The above is repeated for every pixel in the
original image to generate the filtered image
Spatial Filtering: Equation Form
a b
w(s, t ) f ( x s, y t )
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
g ( x, y )
s at b
Filtering can be given
in equation form as
shown above
Notations are based
on the image shown
to the left
Smoothing Spatial Filters
•One of the simplest spatial filtering
operations we can perform is a smoothing
operation
– Simply average all of the pixels in a
Neighborhood around a central value
– Especially useful
1/ 1/ 1/
in removing noise 9 9 9
from images Simple
1/ 1/ 1/
– Also useful for 9 9 9 averaging
highlighting gross filter
1/ 1/ 1/
detail 9 9 9
Smoothing Spatial Filtering
Origin x
104 100 108 1/ 1/ 1/
9 9 9
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)
Original Image Image After Image After
With Noise Averaging Filter Median Filter
•Filtering is often used to remove noise from
images
•Sometimes a median filter works better
than an averaging filter
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Averaging Filter Vs. Median Filter Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Averaging Filter Vs. Median Filter Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Averaging Filter Vs. Median Filter Example
Simple Neighborhood Operations Example
x
123 127 128 119 115 130
140 145 148 153 167 172
133 154 183 192 194 191
194 199 207 210 198 195
164 170 175 162 173 151
y
Strange Things Happen At The Edges!
At the edges of an image we are missing
pixels to form a neighbourhood
Origin x
e e
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
140 145 148 153 167 172
133 154 183 192 194 191
194 199 207 210 198 195
164 170 175 162 173 151
y
Strange Things Happen At The Edges!
(cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Filtered Image:
Zero Padding
Original Filtered Image:
Image Replicate Edge Pixels
Filtered Image:
Wrap Around Edge Pixels
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Strange Things Happen At The Edges!
(cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Strange Things Happen At The Edges!
(cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Strange Things Happen At The Edges!
(cont…)
Correlation & Convolution
•The filtering we have been talking about so
far is referred to as correlation with the filter
itself referred to as the correlation kernel
•Convolution is a similar operation, with just
one subtle difference
a b c r s t eprocessed = v*e +
z*a + y*b + x*c +
d
f
e
g h
e
* u
x
v
y
w
z
w*d + u*e +
t*f + s*g + r*h
Original Image Filter
Pixels
•For symmetric filters it makes no difference
Image Smoothing Example
The image at the right is an
original image of
size 500×500 pixels.
The images in the subsequent
slides show the image after
filtering with an averaging
filter of increasing sizes
3, 5, 9, 15 and 35
Notice how detail begins
to disappear
Image Smoothing Example
𝑴𝒂𝒔𝒌 = 𝟑 × 𝟑
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.
Discontinuities in an image are the points where there
is an abrupt change in intensities among surrounding
pixels.
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
Image Analogous Spatial
Smoothing to Integration
Image Analogous Spatial
Sharpening to Differentiation
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).
Greater the change in intensity, higher will be the
magnitude of the derivative.
Smaller the intensity change, lower the value of
derivative.
In constant areas (with no intensity change), the value
of derivative is zero.
Sharpening Filters
Derivative of a function 𝑓(𝑥) is given by
𝑑𝑓(𝑥) 𝑓 𝑥 + ℎ − 𝑓(𝑥)
= lim
𝑑𝑥 ℎ→0 ℎ
For digital images, ℎ = 1 because the minimum spacing between
two adjacent pixels is 1.
Image sharpening is performed by derivative operators:
1st Order Derivative
𝜕𝑓(𝑥, 𝑦) 𝜕𝑓(𝑥, 𝑦)
= 𝑓 𝑥 + 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
Onset of Ramp or Step Non-Zero Non-Zero
No condition
End of Ramp or Step Non-Zero
defined
Along Ramps
Non-Zero Zero
(Constant Slope)
Behavior of Derivatives
at Image Discontinuities
Edge Models
From left to right, models (ideal representations) of
a step, a ramp, and a roof edge,
and their corresponding intensity profiles.
Edge Models
Points to Remember…
Edges in digital images often are ramp-like transitions in
intensity, in which case
1st Order Derivative of the image would result in thick edges
because the derivative is nonzero along a ramp.
On the other hand, 2nd Order Derivative produce a double
edge effect (positive/negative) separated by zeros at ramp
and step transitions.
From this, we conclude that the second derivative enhances
fine detail much better than the first derivative, a property
that is ideally suited for sharpening images.
Points to Remember…
The sign of 2nd order derivative can be used to determine whether
transition into an edge is from light to dark (negative to
positive) or dark to light (positive to negative).
The Laplacian Filter
Implementation of 2-D, second-order derivatives.
It is an isotropic filter, whose response is independent of the
direction of the discontinuities in the image to which the filter
is applied.
In other words, isotropic filters are rotation invariant, in the
sense that rotating the image and then applying the filter gives
the same result as applying the filter to the image first and then
rotating the result.
𝜕 2 𝑓(𝑥, 𝑦) 𝜕 2 𝑓(𝑥, 𝑦)
𝛻 2 𝑓 𝑥, 𝑦 = 2
+
𝜕𝑥 𝜕𝑦 2
The Laplacian Filter
2 2
2
𝜕 𝑓(𝑥, 𝑦) 𝜕 𝑓(𝑥, 𝑦)
𝛻 𝑓 𝑥, 𝑦 = 2
+
𝜕𝑥 𝜕𝑦 2
𝛻 2 𝑓 𝑥, 𝑦 = 𝑓 𝑥 + 1, 𝑦 + 𝑓 𝑥 − 1, 𝑦 + 𝑓 𝑥, 𝑦 + 1 + 𝑓 𝑥, 𝑦 − 1
−4𝑓(𝑥, 𝑦)
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
Other variants of Laplacian filter are
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 Laplacian
Image Filtered Image Filtered Image
Scaled for Display
But That Is Not Very Enhanced!
The result of a Laplacian filtering is not an enhanced
image
We have to do more work in order to get our final
image
Subtract the Laplacian result from the original image to
generate our final sharpened enhanced image
𝑔 𝑥, 𝑦 = 𝑓 𝑥, 𝑦 + 𝑐 𝛻 2 𝑓 𝑥, 𝑦
Laplacian Image Enhancement
- =
Original Laplacian Sharpened
Image Filtered Image Image
Laplacian Image Enhancement
Laplacian Image Enhancement
Laplacian Image Enhancement
Example of Double Edge Effect in Laplacian
The Gradient Filter
Implementation of 2-D, first-order derivatives.
𝜕𝑓 𝑥, 𝑦
𝑔𝑥 𝜕𝑥
𝛻𝑓 𝑥, 𝑦 = 𝑔𝑟𝑎𝑑 𝑓 𝑥, 𝑦 = =
𝑔𝑦 𝜕𝑓 𝑥, 𝑦
𝜕𝑦
Gradient is a vector quantity having both magnitude and direction.
It points in the direction of the greatest rate of change of 𝑓 at location
(𝑥, 𝑦).
The magnitude (length) of gradient vector denoted as 𝑀(𝑥, 𝑦) signify the
rate of (intensity) change in the direction of the gradient vector.
2 2
𝑀 𝑥, 𝑦 = 𝑚𝑎𝑔 𝛻𝑓 𝑥, 𝑦 = 𝑔𝑥 + 𝑔𝑦
The Gradient Filter
The components of the gradient vector are derivatives, they are
linear operators.
However, the magnitude of this vector is not because of the squaring
and square root operations.
On the other hand, the partial derivatives are not rotation invariant
(isotropic), but the magnitude of the gradient vector is.
However, as in the case of the Laplacian, the isotropic properties of
the discrete gradient are preserved only for a limited number of
rotational increments that depend on the filter masks used to
approximate the derivatives.
Gradient
Roberts Cross-Gradient Operators
The simplest approximations to a
first-order derivative are
𝑔𝑥 = 𝑧8 − 𝑧5
𝑔𝑦 = 𝑧6 − 𝑧5
Two other definitions proposed
by Roberts in the early
development of digital image
processing use cross differences
𝑔𝑥 = 𝑧9 − 𝑧5
𝑔𝑦 = 𝑧8 − 𝑧6
Sobel Operators for Gradient
𝑔𝑥 = 𝑧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.
Blur the original image.
𝑓 ҧ 𝑥, 𝑦 = 𝑇 𝑓(𝑥, 𝑦)
Subtract the blurred image from the original (the resulting
difference is called the mask.)
ҧ 𝑦)
𝑔𝑚𝑎𝑠𝑘 𝑥, 𝑦 = 𝑓 𝑥, 𝑦 − 𝑓(𝑥,
Add the mask to the original.
𝑔 𝑥, 𝑦 = 𝑓 𝑥, 𝑦 + 𝑘 ∗ 𝑔𝑚𝑎𝑠𝑘 𝑥, 𝑦
Combining Spatial Enhancement Methods
Successful image enhancement is
typically not achieved using a
single operation
Rather we combine a range of
techniques in order to achieve a
final result.
Combining Spatial Enhancement Methods
(cont…)
(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)
Image (d) smoothed with
a 5*5 averaging filter
Combining Spatial Enhancement Methods
(cont…)
Compare the original and final images