2. Spatial Filtering : 2
Basics of Spatial FilteringBasics of Spatial Filtering
Image enhancement operations can also work with the values of
the image pixels in the neighborhood and the corresponding values
of a subimage that has the same dimensions as the neighborhood.
The subimage is called a filter, mask, kernel, template, or window.
The values in a filter subimage are referred to as coefficients, rather
than pixels.
The process consists simply of moving the filter mask from point to
point in an image.
For linear spatial filtering, the response is given by a sum of
products of the filter coefficients and the corresponding image
pixels in the area spanned by the filter mask
4. Spatial Filtering : 4
Linear Spatial Filter (Cont.)Linear Spatial Filter (Cont.)
The result (or response), R, of linear filtering with the filter mask at
a point (x, y) in the image is:
In general, linear filtering of an image f of size M X N with a filter
mask of size m X n is given by the expression:
( 1, 1) ( 1, 1) ( 1,0) ( 1, )
(0,0) ( , ) (1,0) ( 1, ) (1,1) ( 1, 1)
R w f x y w f x y
w f x y w f x y w f x y
= − − − − + − − +
+ + + + + + +
L
L
( 1)/2 ( 1)/2
( 1)/2 ( 1)/2
( , ) ( , ) ( , )
m n
s m t n
g x y w s t f x s y t
− −
=− − =− −
= + +∑ ∑
5. Spatial Filtering : 5
Linear Spatial Filter (Cont.)Linear Spatial Filter (Cont.)
When interest lies on the response, R, of an m X n mask at any
point (x, y), and not on the mechanics of implementing mask
convolution
1 1 2 2
1
mn mn
mn
i i
i
R w z w z w z
w z
=
= + + +
= ∑
L
6. Spatial Filtering : 6
Linear Spatial Filter (Cont.)Linear Spatial Filter (Cont.)
1 1 2 2 9 9
9
1
i i
i
R w z w z w z
w z
=
= + + +
= ∑
L
7. Spatial Filtering : 7
Noise reduction by Spatial FilteringNoise reduction by Spatial Filtering
Image averaging takes advantage of information redundancy from
the individual images to reduce noise
Not always possible to acquire so many images!
Alternative option: Take advantage of information redundancy from
different pixels within the same image to reduce noise
8. Spatial Filtering : 8
Averaging FilterAveraging Filter
Instead of averaging between images, we can average neighboring pixels
9
1
1
9
i
i
R z
=
= ∑
10. Spatial Filtering : 10
Weighted Average FilterWeighted Average Filter
Problem: Simple averaging of neighboring pixels lead to
over-smoothing
Possible solution: Instead of weighting all neighboring pixels
equally, assign higher weights to pixels that are closer to the
pixel being convolved
14. Spatial Filtering : 14
Order-Statistic FiltersOrder-Statistic Filters
Nonlinear spatial filters
Steps
Order pixels within an area
Replace value of center pixel with value determined by ordering
Best known example: median filters
( ) ( ) ( )H af bg aH f bH g+ ≠ +
15. Spatial Filtering : 15
Median FilterMedian Filter
Provides good noise reduction for certain types of noise such as
impulse noise
Considerably less blurring than weighted averaging filter
Forces a pixel to be like its neighbors
Steps
Order pixels within an area
Replace value of center pixel with median value (half of all
pixels have intensities greater than or equal to the median
value)
18. Spatial Filtering : 18
Spatial Filtering: SharpeningSpatial Filtering: Sharpening
Goal: highlight or enhance details in images
Some applications:
Photo enhancement
Medical image visualization
Industrial defect detection
19. Spatial Filtering : 19
Sharpening Spatial FilteringSharpening Spatial Filtering
Goal: highlight or enhance details in images
Some applications:
Photo enhancement
Medical image visualization
Industrial defect detection
Basic principle:
Averaging (blurring) is analogous to integration
Therefore, logically, sharpening accomplished by differentiation
20. Spatial Filtering : 20
Derivatives of digital functionDerivatives of digital function
First-order derivative
Second-order derivative
( 1) ( )
f
f x f x
x
∂
= + −
∂
2
2
( 1) ( 1) 2 ( )
f
f x f x f x
x
∂
= + + − −
∂
21. Spatial Filtering : 21
Derivatives of digital functionDerivatives of digital function
First-order derivatives generally produce thicker edges
Second-order derivatives have stronger response to fine detail
(e.g., thin lines and points)
First-order derivatives have stronger response to step changes
Second-order derivatives produce double response at step
changes
22. Spatial Filtering : 22
Derivatives of digital function:Derivatives of digital function:
ExampleExample
23. Spatial Filtering : 23
Derivatives of digital function:Derivatives of digital function:
ExampleExample
24. Spatial Filtering : 24
Sharpening using LaplacianSharpening using Laplacian
Second-order derivatives is better suited for most applications for
sharpening
How?
By constructing a filter based on discrete formulation of second-
order derivatives
Simplest isotropic derivative operator: Laplacian
2 2
2
2 2
f f
f
x y
∂ ∂
∇ = +
∂ ∂
25. Spatial Filtering : 25
How to create Laplacian filterHow to create Laplacian filter
2 2
2
2 2
f f
f
x y
∂ ∂
∇ = +
∂ ∂
2
2
( 1, ) ( 1, ) 2 ( , )
f
f x y f x y f x y
x
∂
= + + − −
∂
2
2
( , 1) ( , 1) 2 ( , )
f
f x y f x y f x y
y
∂
= + + − −
∂
[ ]2
( 1, ) ( 1, ) ( , 1) ( , 1) 4 ( , )f f x y f x y f x y f x y f x y∇ = + + − + + + − −
27. Spatial Filtering : 27
Sharpening using LaplacianSharpening using Laplacian
Problem: While applying Laplacian highlights fine detail, it
de-emphasizes smooth regions (e.g., background features)
Results in featureless background with grayish fine details
Solution: Add original image to recover background
features
2
2
( , ) ( , ), negative filter center
( , )
( , ) ( , ), positive filter center
f x y f x y
g x y
f x y f x y
−∇
=
+∇
29. Spatial Filtering : 29
Sharpening using Unsharp MaskingSharpening using Unsharp Masking
Subtract blurred version of image from the
image itself to produce sharp image
( , ) ( , ) ( , )g x y f x y f x y= −
31. Spatial Filtering : 31
Sharpening using High-boostSharpening using High-boost
FilteringFiltering
Generalization of unsharp masking
As A increases, contribution of sharpening decreases
( , ) ( , ) ( , )g x y Af x y f x y= −
32. Spatial Filtering : 32
Sharpening using High-boostSharpening using High-boost
FilteringFiltering
34. Spatial Filtering : 34
First Derivatives for EnhancementFirst Derivatives for Enhancement
Implemented as magnitude of gradient in image processing
f
x
f
f
y
∂
∂
∇ =
∂
∂
1/222
f f
f
x y
∂ ∂
∇ = + ÷ ÷
∂ ∂
35. Spatial Filtering : 35
ImplementationImplementation
Problem: Expensive to implement directly
Solution: Approximate using absolute values
Can be implemented using two 2x2 filters
Even filters difficult to implement, so 3x3 filters are used for
approximation
9 5 8 6f z z z z∇ ≈ − + −
7 8 9 1 2 3
3 6 9 1 4 7
( 2 ) ( 2 )
( 2 ) ( 2 )
f z z z z z z
z z z z z z
∇ ≈ + + − + +
+ + + − + +