Chapter 03c Spatial Filtering 6spp
Chapter 03c Spatial Filtering 6spp
University of Ioannina - Department of Computer Science C. Nikou – Digital Image Processing (E12)
1
7 Spatial Filtering: Equation Form 8 Smoothing Spatial Filters
a b • One of the simplest spatial filtering
∑ ∑ w(s, t ) f ( x + s, y + t )
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
is an original image of
99 106 98
95 90 85
* 1/
1/
9
1/
1/
9
1/
1/
9
size 500*500 pixels
9 9 9
Simple 3*3
33
1/ 100
104
9
1/ 108
9
1/
9
3 3 Smoothing
3*3
Original Image Filter • The subsequent images
Pi l
Pixels
show the image after
/9 1106
199 /9 198
/9
Neighbourhood /9 190
195 /9 /9
185 Filter
e = 1/9*106 +
1/ *104 + 1/ *100 + 1/ *108 + filtering with an averaging
9 9 9
1/ *99 + 1/ *98 +
9 9
filter of increasing sizes
y Image f (x, y) 1/ *95 + 1/ *90 + 1/ *85
9 9 9 − 3, 5, 9, 15 and 35
= 98.3333
The above is repeated for every pixel in the original image to
• Notice how detail begins
generate the smoothed image. to disappear
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
generated by allowing different pixels in of lots of the finer detail which leaves only
the neighbourhood different weights in the the gross features for thresholding
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
Original Image Smoothed Image Thresholded Image
Weighted
averaging filter
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
2
13
Averaging Filter Vs. Median Filter 14
Spatial smoothing and image
Example approximation
• Spatial smoothing may be viewed as a
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
process for estimating the value of a pixel
from its neighbours.
Original Image Image After Image After • What is the value that “best” approximates
With Noise Averaging Filter Median Filter
the intensity of a given pixel given the
• Filtering is often used to remove noise intensities of its neighbours?
from images
• Sometimes a median filter works better • We have to define “best” by establishing a
than an averaging filter criterion.
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
15
Spatial smoothing and image 16
Spatial smoothing and image
approximation (cont...) approximation (cont...)
A standard criterion is the the sum of Another criterion is the the sum of absolute
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
i =1
m ⎩ i =1 ⎭ i =1
m ⎩ i =1 ⎭
⎧ 1 x>0
∂E N N N
∂E N
⎪
= 0 ⇔ −2∑ ( x(i ) − m ) = 0 ⇔ ∑ x(i ) =∑ m = 0 ⇔ −∑ sgn ( x(i ) − m ) = 0, sign( x) = ⎨ 0 x = 0
∂m i =1 i =1 i =1 ∂m i =1 ⎪−1 x < 0
⎩
N N
There must be equal in quantity positive and negative values.
⇔ ∑ x(i ) =Nm ⇔ m = 1 ∑ x (i ) The average value
i =1 N i =1 m = median{x(i )}
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
17
Spatial smoothing and image 18
Spatial smoothing and image
approximation (cont...) approximation (cont...)
– The median filter is non linear: Example x[n] 1 1 1 1 1 2 2 2 2 2
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
3
Strange Things Happen At The Edges!
19 Strange Things Happen At The Edges! 20
(cont…)
At the edges of an image we are missing There are a few approaches to dealing with
pixels to form a neighbourhood missing edge pixels:
Origin x – Omit missing pixels
e e
• Only works with some filters
• Can add extra code and slow down processing
– Pad the image
e
• Typically with either all white or all black pixels
e
– Replicate border pixels
– Truncate the image
e e e
– Allow pixels wrap around the image
y Image f (x, y) • Can cause some strange image artefacts
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
Filtered Image:
referred to as correlation with the filter itself
Zero Padding referred to as the correlation kernel
• Convolution is a similar operation, with just one
subtle difference
Original Filtered Image:
Image Replicate Edge Pixels
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 +
Filtered Image: t*f + s*g + r*h
Wrap Around Edge Pixels Original Image Filter
Pixels
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
4
25
Effect of Low Pass Filtering on White 26
Effect of Low Pass Filtering on White
Noise Noise (cont...)
Let f be an observed instance of the image f0 Applying a low pass filter h (e.g. an average
corrupted by noise w: filter) by convolution to the degraded image:
f = f0 + w g = h * f = h *( f 0 + w) = h * f 0 + h * w
with noise samples having mean value The expected value of the output is:
E[w(n)]=0 and being uncorrelated with respect
to location:
E[ g ] = E[h * f 0 ] + E[h * w] = h * f 0 + h * E[ w]
⎧σ 2 , m = n = h * f 0 + h *0 = h * f 0
E[ w(m) w(n)] = ⎨
⎩ 0, m ≠ n The noise is removed in average.
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
27
Effect of Low Pass Filtering on White 28
Effect of Low Pass Filtering on White
Noise (cont...) Noise (cont...)
What happens to the standard deviation of g? Considering that h is an average filter, we have
Let g = h * f 0 + h * w = f 0 + w at pixel n:
1
where the bar represents filtered versions of
the signals, then
w(n) = (h * w)(n) = ∑ w(k )
N k∈Γ ( n )
σ g2 = E[ g 2 ] − ( E[ g ]) = Ε[( f 0 + w) 2 ] − ( f 0 )2
2 Therefore,
⎡⎛ 1 ⎞
2
⎤
= Ε[( f 0 ) 2 + ( w) 2 + 2 f 0 w] − ( f 0 ) 2 E[( w(n)) 2 ] = E ⎢⎜
⎢⎣⎝ N
∑ w(k ) ⎟ ⎥
⎥⎦
k∈Γ ( n ) ⎠
= E[( w) 2 ] + 2 E[ f 0 ]E[ w] = E[( w) 2 ]
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
29
Effect of Low Pass Filtering on White 30
Effect of Low Pass Filtering on White
Noise (cont...) Noise (cont...)
⎡⎛ 1 ⎞
2
⎤ Sum of squares
E ⎢⎜ ∑ w(k ) ⎟ ⎥ 1 1
⎢⎣⎝ N ⎠ ⎥⎦ ∑ E ⎡{w(k )} ⎤ = 2 ∑σ
2 2
k∈Γ ( n )
N2 k ∈Γ ( n )
⎣ ⎦ N k ∈Γ ( n )
1
= ∑ E ⎡{w(k )} ⎤
2
⎣ ⎦ Sum of squares Cross products (uncorrelated as m∫l)
N2 k∈Γ ( n )
2
2
+
N2
∑ ∑ E [ w(n − l )w(n − m)] = 0
+ ∑ ∑ E [ w(n − l ) w(n − m) ] l∈Γ ( n ) m∈Γ ( n )
m ≠l
N2 l∈Γ ( n ) m∈Γ ( n )
m ≠l
Cross products
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
5
Effect of Low Pass Filtering on White
31 32 Sharpening Spatial Filters
Noise (cont...)
Finally, substituting the partial results: • Previously we have looked at smoothing
filters which remove fine detail
⎡⎛ 1 ⎞
2
⎤ 1 • Sharpening spatial filters seek to highlight
σ = E ⎢⎜
2
g
⎢⎣⎝ N
∑ w(k ) ⎟ ⎥= 2
⎥⎦ N
∑σ 2
fine detail
k ∈Γ ( n ) ⎠ k∈Γ
Γ(n)
– Remove blurring from images
1 σ 2 – Highlight edges
= Nσ 2 = • Sharpening filters are based on spatial
N 2
N
differentiation
The effect of the noise is reduced.
This processing is not optimal as it also smoothes image
edges.
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of a function
• Let’s consider a simple 1 dimensional A B
example
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
6
37 1st Derivative (cont…) 38 1st Derivative (cont.)
8 • The gradient of an image:
7
6
5
4
3
2
1 •
0
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
The gradient points in the direction of most rapid increase
in intensity.
0 -1 -1 -1 -1 -1 0 0 6 -6 0 0 0 1 2 -2 -1 0 0 0 7 0 0 0
8 Gradient direction
6
4
2
0 The edge strength is given by the gradient magnitude
-2
-4
-6
-8
∂2 f
= f ( x + 1) + f ( x − 1) − 2 f ( x)
∂2x
∂f ∂f
∂x ∂y
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
-5
-10
-15
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
7
Using Second Derivatives For Image
43 Derivatives 44
Enhancement
• A common sharpening filter is the Laplacian
– Isotropic
• Rotation invariant: Rotating the image and applying
the filter is the same as applying the filter and then
rotating the image.
• In other words, the Laplacian of a rotated image is the
rotated Laplacian of the original image.
– One of the simplest sharpening filters
– We will look at a digital implementation
∂2 f ∂2 f
∇2 f = +
∂2 x ∂2 y
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
∂2 f ∂2 f ∇ 2 f = −4 f ( x , y )
∇ f = 2 + 2
2
∂ x ∂ y + f ( x + 1, y ) + f ( x − 1, y )
∂2 f + f ( x, y + 1)) + f ( x, y − 1))
= f ( x + 1, y ) + f ( x − 1, y ) − 2 f ( x, y )
∂2 x
0 1 0
∂2 f
= f ( x, y + 1) + f ( x, y − 1) − 2 f ( x, y ) 1 -4 1
∂2 y
0 1 0
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
enhanced image
Original Laplacian Laplacian
Image Filtered Image Filtered Image
Scaled for Display
g ( x, y ) = f ( x, y ) − ∇ 2 f
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
8
49 Laplacian Image Enhancement 50 Laplacian Image Enhancement
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
- =
Original Laplacian Sharpened
Image Filtered Image Image
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
• The entire enhancement can be combined • This gives us a new filter which does the
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
-1 5 -1
− f ( x, y + 1) − f ( x, y − 1)
0 -1 0
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
-1 -1 -1
-1 9 -1
-1 -1 -1
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
9
55 Unsharp masking 56 Unsharp masking (cont...)
• Used by the printing industry
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Sharpening mechanism
• Subtracts an unsharped (smooth) image
from the original image f (x,y).
–Blur the image
g
b(x,y)=Blur{f (x,y)} If k>1, the process is referred to
–Subtract the blurred image from the original as highboost filtering
(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), k being non negative
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
T
∇f = ⎡⎣Gx G y ⎤⎦ = ⎢
⎣ ∂x ∂y ⎥⎦
Blurred image
(Gaussian 5x5, σ=3)
• Although
g the derivatives are linear
Mask operators, the gradient magnitude is not.
• Also, the partial derivatives are not rotation
Unsharp masking (k=1) invariant (isotropic).
• The magnitude of the gradient vector is
Highboost filtering (k=4.5) isotropic.
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
10
61 Sobel operator Example 62 1st & 2nd Derivatives
Comparing the 1st and 2nd derivatives we
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
An image of a
contact lens which can conclude the following:
is enhanced in
order to make – 1st order derivatives generally produce thicker
defects (at four
and five o’clock in edges (if thresholded at ramp edges)
th image)
the i
obvious
) more
– 2nd order derivatives have a stronger
response to fine detail e.g. thin lines
• Sobel gradient aids to eliminate constant or slowly – 1st order derivatives have stronger response
varying shades of gray and assist automatic to gray level step
inspection. – 2nd order derivatives produce a double
• It also enhances small discontinuities in a flat gray response at step changes in grey level (which
filed. helps in detecting zero crossings)
C. Nikou – Digital Image Processing (E12) C. Nikou – Digital Image Processing (E12)
63
Combining Spatial Enhancement 64
Combining Spatial Enhancement
Methods Methods (cont…)
• Successful image
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
65
Combining Spatial Enhancement 66
Combining Spatial Enhancement
Methods (cont…) Methods (cont…)
Result of applying a (h)
power-law trans. to Compare the original and final images
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
11
67 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
12