DIP-L4
DIP-L4
Spartial Filtering
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 Neighbourhood
(x, y)
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
* /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
Image Smoothing Example
Image Smoothing Example
Image Smoothing Example
Image Smoothing Example
Image Smoothing Example
Image Smoothing Example
Weighted Smoothing Filters
•More effective smoothing filters can be
generated by allowing different pixels in the
neighbourhood different weights in the
averaging function 1
/ 2
/ 1
/
16 16 16
– Pixels closer to the
central pixel are more 2
/16 4
/16 /16
2
important
– Often referred to as a 1
/16 2
/16 /16
1
weighted averaging
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
x
123 127 128 119 115 130
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…)
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:
Wrap Around Edge Pixels
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
(cont…)
Strange Things Happen At The Edges!
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
(cont…)
Strange Things Happen At The Edges!
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
(cont…)
Strange Things Happen At The Edges!
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 +
d
f
e
g
e
h
* u
x
v
y
w
z
z*a + y*b + x*c +
w*d + u*e +
t*f + s*g + r*h
Original Image Filter
Pixels
image
Smoothing/ blurring Example….
1/9* 1/9 1/9
0 *0 *0
1/9 1/9
1/9*
*3 *3
1 4 2 1 5 7
0
1/9 1/9
1/9*
*4 *5
5 7 1 1 6 0
0
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9
1/9 1/9
2 2 3 2 2 2 1/9
*5(2 *7
1/9
)
1/9 1/9
1/9 1/9
4 5 5 7 1 1 *6 *0
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9
1/9 1/9
1/9
*3(2 *3(2
3 2 2 2 2 1
) )
1/9 1/9 1/9
1/9 1/9
*4 *5
5 7 1 1 6 0
1/9 1/9 1/9
1/9 1/9
*2 *5
7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
3 3 1 4 2 1 5 7 2 2 3 2 2 2 2 2
4 5 5 7 1 1 6 0 2 4 5 4 3 3 2 1
0 7 5 4 3 4 3 6 2 4 4 4 3 2 2 1
filter
6 5 4 3 2 1 0 0 2 3 3 3 3 2 1 1
2 3 4 2 1 6 0 1 1 2 2 2 2 2 1 1
Smoothed
Image
Original
image
Assignment
• Write an algorithm to
smoothen any grayscale image
using a 5*5 smoothing mask.
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
Differentiation measures the rate of change of a
function
Let’s consider a simple 1 dimensional example
Spatial Differentiation
A B
1 Derivative
st
5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7
0 -1 -1 -1 -1 0 0 6 -6 0 0 0 1 2 -2 -1 0 0 0 7 0 0 0
2 Derivative (cont…)
nd
5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7
-1 0 0 0 0 1 0 6 -12 6 0 0 1 1 -4 1 1 0 0 7 -7 0 0
Using Second Derivatives For Image
Enhancement
The 2nd derivative is more useful for image
enhancement than the 1st derivative
– Stronger response to fine detail
– Simpler implementation
– We will come back to the 1st order derivative later
on
The first sharpening filter we will look at is the
Laplacian
– Isotropic
– One of the simplest sharpening filters
– We will look at a digital implementation
The Laplacian
The Laplacian is defined as follows:
2 2
2 f f
f 2 2
x y
1 -4 1
0 1 0
The Laplacian (cont…)
Applying the Laplacian to an image we get a new
image that highlights edges and other
discontinuities
g ( x, y ) f ( x, y ) f
Laplacian Image Enhancement
- =
Original Laplacian Sharpened
Image Filtered Image Image
0 -1 0
-1 5 -1
0 -1 0
Variants On The Simple Laplacian
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
1 Derivative Filtering
st
f
Gx x
f f
Gy
y
1st Derivative Filtering (Gradient
operations..…)
The magnitude of this vector is given by:
f mag (f )
G G2
x
2
y 1
2
1
f 2 f 2
2
x y
f
g y
y
( z 3 z 6 z 9 ) ( z1 z 4 z 7 )
-1 -1 -1 -1 0 1
0 0 0 -1 0 1
1 1 1 -1 0 1
Gradient filters variables…..
• Sobel operators
• Slight difference to Perwitt’s operators by
weighing the center locations which provides
smoothing effect.
-1 -2 -1 -1 0 1
f
g x
x
( z 7 2 z 8 z 9 ) ( z1 2 z 2 z 3 )
0 0 0 -2 0 2
gy fy ( z 3 2 z 6 z 9 ) ( z1 2 z 4 z 7 )
1 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
obvious
(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
Images taken from Gonzalez & Woods, Digital Image Processing (2002)