Upload
may-houston
View
223
Download
4
Embed Size (px)
Citation preview
University of Ioannina - Department of Computer Science
Spatial Filtering
Christophoros Nikou
Digital Image Processing
2
C. Nikou – Digital Image Processing (E12)
Contents
In this lecture we will look at spatial filtering techniques:
– Neighbourhood operations– What is spatial filtering?– Smoothing operations– What happens at the edges?– Correlation and convolution– Sharpening filters– Combining filtering techniques
3
C. Nikou – Digital Image Processing (E12)
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 Image f (x, y)
(x, y)Neighbourhood
4
C. Nikou – Digital Image Processing (E12)
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
5
C. Nikou – Digital Image Processing (E12)
Simple Neighbourhood Operations Example
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
Original Image x
y
Enhanced Image x
y
6
C. Nikou – Digital Image Processing (E12)
The Spatial Filtering Process
r s t
u v w
x y z
Origin x
y Image f (x, y)
eprocessed = v*e + r*a + s*b + t*c + u*d + w*f + x*g + y*h + z*i
FilterSimple 3*3
Neighbourhoode 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
C. Nikou – Digital Image Processing (E12)
Spatial Filtering: Equation Form
a
as
b
bt
tysxftswyxg ),(),(),(
Filtering can be given in equation form as shown above
Notations are based on the image shown to the left
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
8
C. Nikou – Digital Image Processing (E12)
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/91/9
1/9
1/91/9
1/9
1/91/9
1/9
Simple averaging filter
9
C. Nikou – Digital Image Processing (E12)
Smoothing Spatial Filtering
1/91/9
1/9
1/91/9
1/9
1/91/9
1/9
Origin x
y Image 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
FilterSimple 3*3
Neighbourhood106
104
99
95
100 108
98
90 85
1/91/9
1/9
1/91/9
1/9
1/91/9
1/9
3*3 SmoothingFilter
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
C. Nikou – Digital Image Processing (E12)
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
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
11
C. Nikou – Digital Image Processing (E12)
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/162/16
1/16
2/164/16
2/16
1/162/16
1/16
Weighted averaging filter
12
C. Nikou – Digital Image Processing (E12)
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
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
Original Image Smoothed Image Thresholded Image
13
C. Nikou – Digital Image Processing (E12)
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 ImageWith Noise
Image AfterAveraging Filter
Image AfterMedian Filter
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
14
C. Nikou – Digital Image Processing (E12)
Spatial smoothing and image approximation
Spatial smoothing may be viewed as a process for estimating the value of a pixel from its neighbours.
What is the value that “best” approximates the intensity of a given pixel given the intensities of its neighbours?
We have to define “best” by establishing a criterion.
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
15
C. Nikou – Digital Image Processing (E12)
Spatial smoothing and image approximation (cont...)
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
2
1
( )N
i
E x i m
2
1
arg min ( )N
m i
m x i m
0E
m
1
2 ( ) 0N
i
x i m
1 1
( )N N
i i
x i m
1
( )N
i
x i Nm
1
1( )
N
i
m x iN
A standard criterion is the the sum of squares differences.
The average value
16
C. Nikou – Digital Image Processing (E12)
Spatial smoothing and image approximation (cont...)
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
1
( )N
i
E x i m
1
arg min ( )N
m i
m x i m
0E
m
1
( ) 0,N
i
sgn x i m
1 0
( ) 0 0
1 0
x
sign x x
x
Another criterion is the the sum of absolute differences.
There must be equal in quantity positive and negative values.
median{ ( )}m x i
17
C. Nikou – Digital Image Processing (E12)
Spatial smoothing and image approximation (cont...)
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
– The median filter is non linear:
– It works well for impulse noise (e.g. salt and pepper).
– It requires sorting of the image values.– It preserves the edges better than an average
filter in the case of impulse noise.– It is robust to impulse noise at 50%.
median{ } median{ } median{ }x y x y
18
C. Nikou – Digital Image Processing (E12)
Spatial smoothing and image approximation (cont...)
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
Example x[n] 1 1 1 1 1 2 2 2 2 2
Impulse noise
x[n] 1 3 1 1 1 2 3 2 2 3
Median
(N=3) x[n] - 1 1 1 1 2 2 2 2 -
Average (N=3)
x[n] - 1.7 1.7 1 1.3 2 2.3 2.3 2.2 -
edge
The edge is smoothed
19
C. Nikou – Digital Image Processing (E12)
Strange Things Happen At The Edges!
Origin x
y Image f (x, y)
e
e
e
e
At the edges of an image we are missing pixels to form a neighbourhood
e e
e
20
C. Nikou – Digital Image Processing (E12)
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
21
C. Nikou – Digital Image Processing (E12)
Strange Things Happen At The Edges! (cont…)
OriginalImage
Filtered Image: Zero Padding
Filtered Image: Replicate Edge Pixels
Filtered Image: Wrap Around Edge Pixels
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
22
C. Nikou – Digital Image Processing (E12)
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.
eprocessed = 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
*
23
C. Nikou – Digital Image Processing (E12)
Effect of Low Pass Filtering on White Noise
Let f be an observed instance of the image f0 corrupted by noise w:
with noise samples having mean value E[w(n)]=0 and being uncorrelated with respect to location:
0f f w
2 ,[ ( ) ( )]
0,
m nE w m w n
m n
24
C. Nikou – Digital Image Processing (E12)
Effect of Low Pass Filtering on White Noise (cont...)
Applying a low pass filter h (e.g. an average filter) by convolution to the degraded image:
The expected value of the output is:
The noise is removed in average.
*g h f 0*( )h f w 0* *h f h w
0[ ] [ * ] [ * ]E g E h f E h w 0* * [ ]h f h E w
0* *0h f h 0*h f
25
C. Nikou – Digital Image Processing (E12)
Effect of Low Pass Filtering on White Noise (cont...)
What happens to the standard deviation of g?
Let
where the bar represents filtered versions of the signals, then
22 2[ ] [ ]g E g E g
0 0* *g h f h w f w
2 20 0[( ) ] ( )f w f
2 2 20 0 0[( ) ( ) 2 ] ( )f w f w f
20[( ) ] 2 [ ] [ ]E w E f E w 2[( ) ]E w
26
C. Nikou – Digital Image Processing (E12)
Effect of Low Pass Filtering on White Noise (cont...)
Considering that h is an average filter, we have at pixel n:
Therefore,
2[( ( )) ]E w n
( ) ( * )( )w n h w n( )
1( )
k n
w kN
2
( )
1( )
k n
E w kN
27
C. Nikou – Digital Image Processing (E12)
Effect of Low Pass Filtering on White Noise (cont...)
2
( )
1( )
k n
E w kN
2
2( )
1( )
k n
E w kN
2( ) ( )
2( ) ( )
l n m nm l
E w n l w n mN
Sum of squares
Cross products
28
C. Nikou – Digital Image Processing (E12)
Effect of Low Pass Filtering on White Noise (cont...)
2 22 2
( ) ( )
1 1( )
k n k n
E w kN N
2( ) ( )
2( ) ( ) 0
l n m nm l
E w n l w n mN
Sum of squares
Cross products (uncorrelated as ml)
29
C. Nikou – Digital Image Processing (E12)
Effect of Low Pass Filtering on White Noise (cont...)
Finally, substituting the partial results:
The effect of the noise is reduced.This processing is not optimal as it also smoothes image edges.
2
2
( )
1( )g
k n
E w kN
22
( )
1
k nN
22
1N
N
2
N
30
C. Nikou – Digital Image Processing (E12)
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
31
C. Nikou – Digital Image Processing (E12)
Spatial Differentiation
Differentiation measures the rate of change of a function
Let’s consider a simple 1 dimensional example
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
32
C. Nikou – Digital Image Processing (E12)
Spatial DifferentiationIm
ag
es
take
n f
rom
Go
nza
lez
& W
oo
ds,
Dig
ital I
ma
ge
Pro
cess
ing
(2
00
2)
A B
33
C. Nikou – Digital Image Processing (E12)
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
•Second derivative filter output– Zero at constant intensities– Non zero at the onset and end of a step or ramp– Zero along ramps of constant slope
34
C. Nikou – Digital Image Processing (E12)
1st Derivative
The formula for the 1st derivative of a function is as follows:
It’s just the difference between subsequent values and measures the rate of change of the function
)()1( xfxfx
f
35
C. Nikou – Digital Image Processing (E12)
1st Derivative (cont.)
The gradient points in the direction of most rapid increase in intensity.
• The gradient of an image:
•
Gradient direction
The edge strength is given by the gradient magnitude
Source: Steve Seitz
36
C. Nikou – Digital Image Processing (E12)
1st Derivative (cont.)
f
x
f
y
f
37
C. Nikou – Digital Image Processing (E12)
1st Derivative (cont…)
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 -1 0 0 6 -6 0 0 0 1 2 -2 -1 0 0 0 7 0 0 0
38
C. Nikou – Digital Image Processing (E12)
2nd Derivative
The formula for the 2nd 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
xfxfxfx
f
39
C. Nikou – Digital Image Processing (E12)
2nd Derivative (cont…)
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
40
C. Nikou – Digital Image Processing (E12)
Using Second Derivatives For Image Enhancement
Edges in images are often ramp-like transitions– 1st derivative is constant and produces thick
edges – 2nd derivative zero crosses the edge (double
response at the onset and end with opposite signs)
A common sharpening filter is the Laplacian– Isotropic– One of the simplest sharpening filters– We will look at a digital implementation
41
C. Nikou – Digital Image Processing (E12)
The Laplacian
The Laplacian is defined as follows:
where the partial 1st order derivative in the x direction is defined as follows:
and in the y direction as follows:
y
f
x
ff
2
2
2
22
),(2),1(),1(2
2
yxfyxfyxfx
f
),(2)1,()1,(2
2
yxfyxfyxfy
f
42
C. Nikou – Digital Image Processing (E12)
The Laplacian (cont…)
So, the Laplacian can be given as follows:
We can easily build a filter based on this
),1(),1([2 yxfyxff )]1,()1,( yxfyxf
),(4 yxf
0 1 0
1 -4 1
0 1 0
43
C. Nikou – Digital Image Processing (E12)
The Laplacian (cont…)
Applying the Laplacian to an image we get a new image that highlights edges and other discontinuities
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
OriginalImage
LaplacianFiltered Image
LaplacianFiltered Image
Scaled for Display
44
C. Nikou – Digital Image Processing (E12)
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
LaplacianFiltered Image
Scaled for Display
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
fyxfyxg 2),(),(
45
C. Nikou – Digital Image Processing (E12)
Laplacian Image Enhancement
In the final sharpened image edges and fine detail are much more obvious
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
- =
OriginalImage
LaplacianFiltered Image
SharpenedImage
46
C. Nikou – Digital Image Processing (E12)
Laplacian Image EnhancementIm
ag
es
take
n f
rom
Go
nza
lez
& W
oo
ds,
Dig
ital I
ma
ge
Pro
cess
ing
(2
00
2)
47
C. Nikou – Digital Image Processing (E12)
Simplified Image Enhancement
The entire enhancement can be combined into a single filtering operation
),1(),1([),( yxfyxfyxf )1,()1,( yxfyxf
)],(4 yxf
fyxfyxg 2),(),(
),1(),1(),(5 yxfyxfyxf )1,()1,( yxfyxf
48
C. Nikou – Digital Image Processing (E12)
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
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
49
C. Nikou – Digital Image Processing (E12)
Simplified Image Enhancement (cont…)Im
ag
es
take
n f
rom
Go
nza
lez
& W
oo
ds,
Dig
ital I
ma
ge
Pro
cess
ing
(2
00
2)
50
C. Nikou – Digital Image Processing (E12)
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
SimpleLaplacian
Variant ofLaplacian
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
51
C. Nikou – Digital Image Processing (E12)
Unsharp masking
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
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
52
C. Nikou – Digital Image Processing (E12)
Unsharp masking (cont...)Im
ag
es
take
n f
rom
Go
nza
lez
& W
oo
ds,
Dig
ital I
ma
ge
Pro
cess
ing
(2
00
2)
Sharping mechanism
When k>1 the process is referred to as highboost filtering
53
C. Nikou – Digital Image Processing (E12)
Unsharp masking (cont...)Im
ag
es
take
n f
rom
Go
nza
lez
& W
oo
ds,
Dig
ital I
ma
ge
Pro
cess
ing
(2
00
2) Original image
Blurred image
Mask
Unsharp masking
Highboost filtering (k=4.5)
54
C. Nikou – Digital Image Processing (E12)
1st Derivative Filtering
Implementing 1st 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:
y
fx
f
G
G
y
xf
55
C. Nikou – Digital Image Processing (E12)
1st Derivative Filtering (cont…)
The magnitude of this vector is given by:
For practical reasons this can be simplified as:
)f( magf
21
22yx GG
21
22
y
f
x
f
yx GGf
56
C. Nikou – Digital Image Processing (E12)
1st 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
321987 22 zzzzzzf
741963 22 zzzzzz
z1 z2 z3
z4 z5 z6
z7 z8 z9
57
C. Nikou – Digital Image Processing (E12)
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
58
C. Nikou – Digital Image Processing (E12)
Sobel Example
Sobel filters are typically used for edge detection
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
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
59
C. Nikou – Digital Image Processing (E12)
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)
60
C. Nikou – Digital Image Processing (E12)
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
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
61
C. Nikou – Digital Image Processing (E12)
Combining Spatial Enhancement Methods (cont…)
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
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)
62
C. Nikou – Digital Image Processing (E12)
Combining Spatial Enhancement Methods (cont…)
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
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
63
C. Nikou – Digital Image Processing (E12)
Combining Spatial Enhancement Methods (cont…)
Compare the original and final images
Ima
ge
s ta
ken
fro
m G
on
zale
z &
Wo
od
s, D
igita
l Im
ag
e P
roce
ssin
g (
20
02
)
64
C. Nikou – Digital Image Processing (E12)
Summary
In this lecture we have looked at the idea of spatial filtering and in particular:
– Neighbourhood operations– The filtering process– Smoothing filters– Dealing with problems at image edges when
using filtering– Correlation and convolution– Sharpening filters– Combining filtering techniques