Image Enhancement-Spatial Filtering From: Digital Image Processing, Chapter 3
Image Enhancement-Spatial Filtering From: Digital Image Processing, Chapter 3
of
19
Digital Image Processing
Image Enhancement-
Spatial Filtering
From:
Digital Image Processing, Chapter 3
2
of
19
Contents
Next, we will look at spatial filtering
techniques:
What is spatial filtering?
Smoothing Spatial filters.
Sharpening Spatial Filters.
Combining Spatial Enhancement Methods
3
of
19
Neighbourhood Operations
Neighbourhood operations simply operate
on a larger neighbourhood of pixels than
point operations
Neighbourhoods are
mostly a rectangle
around a central pixel
Any size rectangle
and any shape filter
are possible
Origin x
y
I mage f (x, y)
(x, y)
Neighbourhood
4
of
19
Neighbourhood Operations
For each pixel in the origin image, the
outcome is written on the same location at
the target image.
Origin x
y
I mage f (x, y)
(x, y)
Neighbourhood
Target Origin
5
of
19
Simple Neighbourhood Operations
Simple neighbourhood operations example:
Min: Set the pixel value to the minimum in
the neighbourhood
Max: Set the pixel value to the maximum in
the neighbourhood
6
of
19
The Spatial Filtering Process
j k l
m n o
p q r
Origin x
y
I mage f (x, y)
e
processed
= n*e +
j*a + k*b + l*c +
m*d + o*f +
p*g + q*h + r*i
Filter (w)
Simple 3*3
Neighbourhood
e 3*3 Filter
a b c
d e f
g h i
Original Image
Pixels
*
The above is repeated for every pixel in the
original image to generate the filtered image
7
of
19
Spatial Filtering: Equation Form
= =
+ + =
a
a s
b
b t
t y s x f t s w y x g ) , ( ) , ( ) , (
Filtering can be given
in equation form as
shown above
Notations are based
on the image shown
to the left
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
8
of
19
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
highlighting gross
detail
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
Simple
averaging
filter
9
of
19
Smoothing Spatial Filtering
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
Origin x
y
I mage f (x, y)
e =
1
/
9
*106 +
1
/
9
*104 +
1
/
9
*100 +
1
/
9
*108 +
1
/
9
*99 +
1
/
9
*98 +
1
/
9
*95 +
1
/
9
*90 +
1
/
9
*85
= 98.3333
Filter
Simple 3*3
Neighbourhood
106
104
99
95
100 108
98
90 85
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
1
/
9
3*3 Smoothing
Filter
104 100 108
99 106 98
95 90 85
Original Image
Pixels
*
The above is repeated for every pixel in the
original image to generate the smoothed image
10
of
19
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
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
11
of
19
Image Smoothing Example
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
12
of
19
Image Smoothing Example
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
13
of
19
Image Smoothing Example
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
14
of
19
Image Smoothing Example
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
15
of
19
Image Smoothing Example
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
16
of
19
Image Smoothing Example
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
17
of
19
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
Often referred to as a
weighted averaging
1
/
16
2
/
16
1
/
16
2
/
16
4
/
16
2
/
16
1
/
16
2
/
16
1
/
16
Weighted
averaging filter
18
of
19
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
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
Original Image Smoothed Image Thresholded Image
* Image taken from Hubble Space Telescope
19
of
19
Averaging Filter Vs. Median Filter
Example
Filtering is often used to remove noise from
images
Sometimes a median filter works better than
an averaging filter
Original Image
With Noise
Image After
Averaging Filter
Image After
Median Filter
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
20
of
19
Averaging Filter Vs. Median Filter
Example
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
Original
21
of
19
Averaging Filter Vs. Median Filter
Example
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
Averaging
Filter
22
of
19
Averaging Filter Vs. Median Filter
Example
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
Median
Filter
23
of
19
Strange Things Happen At The Edges!
Origin x
y
I mage f (x, y)
e
e
e
e
At the edges of an image we are missing
pixels to form a neighbourhood
e e
e
24
of
19
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
25
of
19
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
For symmetric filters it makes no difference
e
processed
= v*e +
z*a + y*b + x*c +
w*d + u*e +
t*f + s*g + r*h
r s t
u v w
x y z
Filter
a b c
d e e
f g h
Original Image
Pixels
*
26
of
19
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
27
of
19
Spatial Differentiation
Differentiation measures the rate of change of
a function
Lets consider a simple 1 dimensional
example
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
28
of
19
Spatial Differentiation
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
A B
29
of
19
1
st
Derivative
The formula for the 1
st
derivative of a
function is as follows:
Its just the difference between subsequent
values and measures the rate of change of
the function
) ( ) 1 ( x f x f
x
f
+ =
c
c
30
of
19
1
st
Derivative (cont)
Image Strip
0
1
2
3
4
5
6
7
8
1st Derivative
-8
-6
-4
-2
0
2
4
6
8
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
f(x)
f(x)
31
of
19
2
nd
Derivative
The formula for the 2
nd
derivative of a
function is as follows:
Simply takes into account the values both
before and after the current value
) ( 2 ) 1 ( ) 1 (
2
2
x f x f x f
x
f
+ + =
c
c
32
of
19
2
nd
Derivative (cont)
Image Strip
0
1
2
3
4
5
6
7
8
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
2nd Derivative
-15
-10
-5
0
5
10
-1 0 0 0 0 1 0 6
-12
6 0 0 1 1 -4 1 1 0 0 7 -7 0 0
f(x)
f(x)
33
of
19
1
st
and 2
nd
Derivative
Image Strip
0
1
2
3
4
5
6
7
8
1st Derivative
-8
-6
-4
-2
0
2
4
6
8
2nd Derivative
-15
-10
-5
0
5
10
f(x)
f(x)
f(x)
34
of
19
Using Second Derivatives For Image
Enhancement
The 2
nd
derivative is more useful for image
enhancement than the 1
st
derivative
Stronger response to fine detail
Simpler implementation
We will come back to the 1
st
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
35
of
19
The Laplacian
The Laplacian is defined as follows:
where the partial 1
st
order derivative in the x
direction is defined as follows:
and in the y direction as follows:
y
f
x
f
f
2
2
2
2
2
c
c
+
c
c
= V
) , ( 2 ) , 1 ( ) , 1 (
2
2
y x f y x f y x f
x
f
+ + =
c
c
) , ( 2 ) 1 , ( ) 1 , (
2
2
y x f y x f y x f
y
f
+ + =
c
c
36
of
19
The Laplacian (cont)
So, the Laplacian can be given as follows:
We can easily build a filter based on this
) , 1 ( ) , 1 ( [
2
y x f y x f f + + = V
)] 1 , ( ) 1 , ( + + + y x f y x f
) , ( 4 y x f
0 1 0
1 -4 1
0 1 0
37
of
19
The Laplacian (cont)
Applying the Laplacian to an image we get a
new image that highlights edges and other
discontinuities
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
Original
Image
Laplacian
Filtered Image
Laplacian
Filtered Image
Scaled for Display
38
of
19
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
Laplacian
Filtered Image
Scaled for Display
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
f y x f y x g
2
) , ( ) , ( V =
39
of
19
Laplacian Image Enhancement
In the final sharpened image edges and fine
detail are much more obvious
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
-
=
Original
Image
Laplacian
Filtered Image
Sharpened
Image
40
of
19
Laplacian Image Enhancement
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
41
of
19
Simplified Image Enhancement
The entire enhancement can be combined
into a single filtering operation
) , 1 ( ) , 1 ( [ ) , ( y x f y x f y x f + + =
) 1 , ( ) 1 , ( + + + y x f y x f
)] , ( 4 y x f
f y x f y x g
2
) , ( ) , ( V =
) , 1 ( ) , 1 ( ) , ( 5 y x f y x f y x f + =
) 1 , ( ) 1 , ( + y x f y x f
42
of
19
Simplified Image Enhancement (cont)
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
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
43
of
19
Simplified Image Enhancement (cont)
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
44
of
19
Variants On The Simple Laplacian
There are lots of slightly different versions of
the Laplacian that can be used:
0 1 0
1 -4 1
0 1 0
1 1 1
1 -8 1
1 1 1
-1 -1 -1
-1 9 -1
-1 -1 -1
Simple
Laplacian
Variant of
Laplacian
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
45
of
19
Unsharp Mask & Highboost Filtering
Using sequence of linear spatial filters in
order to get Sharpening effect.
-Blur
- Subtract from original image
- add resulting mask to original image
46
of
19
Highboost Filtering
47
of
19
1
st
Derivative Filtering
Implementing 1
st
derivative filters is difficult in
practice
For a function f(x, y) the gradient of f at
coordinates (x, y) is given as the column
vector:
(
(
(
(
c
c
c
c
=
(
= V
y
f
x
f
G
G
y
x
f
48
of
19
1
st
Derivative Filtering (cont)
The magnitude of this vector is given by:
For practical reasons this can be simplified as:
) f (V = V mag f
| |
2
1
2 2
y x
G G + =
2
1
2
2
(
(
|
|
.
|
\
|
c
c
+
|
.
|
\
|
c
c
=
y
f
x
f
y x
G G f + ~ V
49
of
19
1
st
Derivative Filtering (cont)
There is some debate as to how best to
calculate these gradients but we will use:
which is based on these coordinates
( ) ( )
3 2 1 9 8 7
2 2 z z z z z z f + + + + ~ V
( ) ( )
7 4 1 9 6 3
2 2 z z z z z z + + + + +
z
1
z
2
z
3
z
4
z
5
z
6
z
7
z
8
z
9
50
of
19
Sobel Operators
Based on the previous equations we can
derive the Sobel Operators
To filter an image it is filtered using both
operators the results of which are added
together
-1 -2 -1
0 0 0
1 2 1
-1 0 1
-2 0 2
-1 0 1
51
of
19
Sobel Example
Sobel filters are typically used for edge
detection
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
An image of a
contact lens which
is enhanced in
order to make
defects (at four
and five oclock in
the image) more
obvious
52
of
19
1
st
& 2
nd
Derivatives
Comparing the 1
st
and 2
nd
derivatives we
can conclude the following:
1
st
order derivatives generally produce thicker
edges
2
nd
order derivatives have a stronger
response to fine detail e.g. thin lines
1
st
order derivatives have stronger response
to grey level step
2
nd
order derivatives produce a double
response at step changes in grey level
53
of
19
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
This example will focus on
enhancing the bone scan to
the right
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
54
of
19
Combining Spatial Enhancement
Methods (cont)
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
Laplacian filter of
bone scan (a)
Sharpened version of
bone scan achieved
by subtracting (a)
and (b) Sobel filter of bone
scan (a)
(a)
(b)
(c)
(d)
55
of
19
Combining Spatial Enhancement
Methods (cont)
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)
The product of (c)
and (e) which will be
used as a mask
Sharpened image
which is sum of (a)
and (f)
Result of applying a
power-law trans. to
(g)
(e)
(f)
(g)
(h)
Image (d) smoothed with
a 5*5 averaging filter
56
of
19
Combining Spatial Enhancement
Methods (cont)
Compare the original and final images
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
G
o
n
z
a
l
e
z
&
W
o
o
d
s
,
D
i
g
i
t
a
l
I
m
a
g
e
P
r
o
c
e
s
s
i
n
g
(
2
0
0
2
)