Lecture 1.2.4-Spatial Filtering
Lecture 1.2.4-Spatial Filtering
Engineering
DEPARTMENT OF COMPUTER
SCIENCE & ENGINEERING
Bachelor of Engineering (Computer Science &
Engineering)
Subject Name : Computer Vision
Subject Code: 21CSH/ITH-422
Topic: Lecture-1.2.4
Lecture- Spatial Filtering
By :Er. Abha Agrawal
DISCOVER . LEARN . EMPOWER
1
Syllabus
• UNIT-1: Introduction to Image Processing
2
CONTENTS
• Neighbourhood operations
• What is spatial filtering?
• Smoothing operations
• What happens at the edges?
• Correlation and convolution
• Sharpening filters
• Combining filtering techniques
3
Neighbourhood Operations
Neighbourhood operations simply operate on a larger neighbourhood of pixels than
point operations
Origin x
Neighbourhoods are
mostly a rectangle
around a central pixel
Any size rectangle
and any shape filter
are possible (x, y)
Neighbourhood
y Image f (x, y)
Simple Neighbourhood Operations
Some simple neighbourhood 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 Neighbourhood Operations Example
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
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 neighbourhood around a central value
• Especially useful
in removing noise
from images
• Also useful for 1
/9 1
/9 1
/9
highlighting gross Simple
detail 1
/9 /9
1
/9
1
averaging
filter
1
/9 /9
1 1
/9
Smoothing Spatial Filtering
Origin x
104 100 108 1
/9 1
/9 1
/9
* /9 /9 /9
1 1 1
99 106 98
95 90 85 1
/9 1
/9 1
/9
1
/9 100
104 1
/9 108
1
/9
Original Image Filter
Simple 3*3 /9 106
1
99 1
/9 198
/9
3*3 Smoothing Pixels
Neighbourhood /9 190
1
95 /9 185
/9 Filter
e = 1/9*106 +
1
/9*104 + 1/9*100 + 1/9*108 +
1
/9*99 + 1/9*98 +
y Image f (x, y) 1
/9*95 + 1/9*90 + 1/9*85
= 98.3333
The above is repeated for every pixel in the original image to
generate the smoothed image.
Image Smoothing Example
The image at the top left
is an original image of
size 500*500 pixels
The subsequent images
show the image after
filtering with an averaging
filter of increasing sizes
• 3, 5, 9, 15 and 35
Notice how detail begins
to disappear
Weighted Smoothing Filters
More effective smoothing filters can be generated by allowing different pixels in the
neighbourhood different weights in the averaging function
• Pixels closer to the
central pixel are more
important 1
/16 2/16 1/16
• Often referred to as a
weighted averaging 2
/ 4
/ 2
/
16 16 16
1
/16 /16
2
/16
1
Weighted
averaging filter
Another Smoothing Example
By smoothing the original image we get rid of lots of the finer
detail which leaves only the gross features for thresholding
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
edge
Impulse x[n] 1 3 1 1 1 2 3 2 2 3
noise
Median
(N=3) x[n] - 1 1 1 1 2 2 2 2 -
e e e
y Image f (x, y)
Strange Things Happen At The Edges! (cont…)
a b c r s eprocessed = v*e +
t
d e
f g h
e
* u v
x y z
w
For symmetric filters it makes no difference.
z*a + y*b + x*c +
w*d + u*e +
t*f + s*g + r*h
Original Image Filter
Pixels
Sharpening Spatial Filters
Previously we have looked at smoothing filters which remove fine detail
Sharpening spatial filters seek to highlight fine detail
• Remove blurring from images
• Highlight edges
Sharpening filters are based on spatial differentiation
Spatial Differentiation
Images taken from Gonzalez & Woods, Digital Image Processing (2002) Differentiation measures the rate of change of a function
Let’s consider a simple 1 dimensional example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
A
B
Spatial Differentiation
Derivative Filters Requirements
First derivative filter output
• Zero at constant intensities
• Non zero at the onset of a step or ramp
• Non zero along ramps
Applying the Laplacian to an image we get a new image that highlights edges and
other discontinuities
- =
Original Laplacian Sharpened
Image Filtered Image Image
In the final sharpened image edges and fine detail are much more
obvious
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Laplacian Image Enhancement
36
Simplified Image Enhancement (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
This gives us a new filter which does the whole job for us in one step
0 -1 0
-1 5 -1
0 -1 0
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Simplified Image Enhancement (cont…)
Variants On The Simple Laplacian
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
There are lots of slightly different versions of the Laplacian that can be used:
0 1 0 1 1 1
Simple Variant of
1 -4 1 1 -8 1
Laplacian Laplacian
0 1 0 1 1 1
-1 -1 -1
-1 9 -1
-1 -1 -1
Unsharp masking
Images taken from Gonzalez & Woods, Digital Image Processing (2002) Used by the printing industry
Subtracts an unsharped (smooth) image from the original image
f(x,y).
•Blur the image
b(x,y)=Blur{f(x,y)}
•Subtract the blurred image from the original (the result is
called the mask)
gmask(x,y)=f(x,y)-b(x,y)
•Add the mask to the original
g(x,y)=f(x,y)+k gmask(x,y) with k non negative
Unsharp masking (cont...)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Sharping mechanism
Blurred image
Mask
Unsharp masking
-1 -2 -1 -1 0 1
0 0 0 -2 0 2
To filter an image it is filtered using both operators the results of
which are added 1 together
2 1 -1 0 1
Sobel Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
An image of a
contact lens which
is enhanced in
order to make
defects (at four
and five o’clock in
the image) more
Sobel filters are typically used for edge detection obvious
1st & 2nd Derivatives
Comparing the 1st and 2nd derivatives we can conclude the following:
• 1st order derivatives generally produce thicker edges (if thresholded at ramp
edges)
• 2nd order derivatives have a stronger response to fine detail e.g. thin lines
• 1st order derivatives have stronger response to grey level step
• 2nd order derivatives produce a double response at step changes in grey level
(which helps in detecting zero crossings)
Combining Spatial Enhancement Methods
Successful image enhancement is
typically not achieved using a single
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
operation
Rather we combine a range of techniques
in order to achieve a final result
This example will focus on enhancing the
bone scan to the right
Combining Spatial Enhancement Methods (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
(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)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
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)
• Video Link-
• https://www.youtube.com/watch?v=0uhs117VZj4
• Web Link-
• https://www.geeksforgeeks.org/spatial-filtering-and-its-types/
54
55