Chapter 2
(Part 2)
Spatial Filtering For
Image Enhancement
Dr. Archana Ekbote
Outline of the Chapter
❖ Concept of Spatial Filtering
❖ Smoothing Filters
❖ Sharpening Filters
01
Spatial Filtering
➢ Spatial Filtering is also known as Mask Processing.
➢ Filtering operations are performed directly on the pixels of an image
i.e. directly in the Spatial Domain.
➢ Spatial Filtering operations are carried out by choosing a group of
pixels at a time Local operation
➢ All the chosen pixels participate in the transformation action.
➢ Pixels are grouped by defining a neighborhood.
02
Neighborhood of a pixel
3x3 neighborhood
5x5 neighborhood
7x7 neighborhood
2x2 neighborhood
4x4 neighborhood
– Odd numbered matrix taken in order to conveniently identify center pixel.
– For even numbered matrix, the center pixel should be defined properly
e.g. top-left corner for 2 x 2
03
Mechanics of Spatial Filtering
➢ All pixels in the image are processed according to the values of a
Subimage whose dimensions are same as that of neighborhood
matrix defined. Local operation
➢ Spatial filtering is not zero memory operation Can’t be performed
INPLACE
➢ The Subimage is called a filter, mask, kernel, template or window.
➢ Values of mask are called filter coefficients.
04
Mechanics of Spatial Filtering contd…
Ref. :- R. C. Gonzalez, and R. E. Woods, Digital Image Processing, Pearson
05
Mechanics of Spatial Filtering contd…
➢ Spatial Filtering = 2-D Convolution of Image with Mask
New pixel value of center pixel =
Over Neighborhood
( Mask coefficients Image coefficients )
For image of size M N and mask of size m n
a b
g ( x, y ) = w(s, t ) f ( x + s, y + t )
s =− a t =− b
where a = (m − 1) / 2 and b = (n − 1) / 2
➢ 2-D Convolution will expand the image
➢ Border pixels need to be processed carefully to avoid expansion of
the image.
06
Spatial Frequency
➢ It is the rate of change of gray levels w.r.t coordinates.
➢ An image with abrupt variations in gray level is considered to contain
very high spatial frequency components.
➢ A image with more or less similar adjacent gray levels is considered
to have low spatial frequency components.
07
Classification of Spatial Filters
Spatial Filters
Smoothing Filters Sharpning Filters
(Low-Pass Filters) (High-Pass Filters)
Linear Filters Non-Linear Filters Gradient Laplacian
(Order Statistics Filters) (1st order derivative) (2nd order derivative)
Median filter Sobel
Min filter Prewitt
Max filter Roberts
Mid-point filter
08
Smoothing Filters
09
Linear Low Pass Filtering
➢ Also called as Averaging Filters
➢ It tries to remove abrupt variations (high frequency component) in the
gray levels.
➢ Helps in eliminating random noise (e.g. Gaussian noise) since noise
appears as a sudden variation in the gray level values.
➢ Simple approach: replace the center pixel value by average of the
neighborhood
➢ Results in blurring (larger the neighborhood, more will be the blurring)
10
Mask for Low Pass Filtering
Simple Average Mask
1 1 1 1 1 1 1 1
1
x 1 1 1 1 1 1 1 1
9
1
1 1 1 x 1 1 1 1 1
25
3x3 Mask 1 1 1 1 1
1 1 1 1 1
5x5 Mask
Weighted Average Mask / Gaussian Mask
1 2 1
1
x 2 4 2
16
1 2 1
3x3 Mask
11
Example of Low Pass filtering
1 1 1 5 3 4 7 10
1
x 1 1 1
9
3 2 4 7 10
1 1 1
2 2 4 5 7
10 0 0 11 10
1 2 2 3 4
1 3 2 10 10
1 1 1 1 2
1 1 3 10 10
2 3 1 1 2 6 2 3 8 10
1 1 1 1 2 4 2 4 8 10
Image 1 2 4 6 8
1 2 1
1 2 2 2 3 4
x 2 4 2
16
1 2 1 1 1 1 1 2
12
Effect of Mask size
Original Image LP Filtering with 3x3 Mask LP Filtering with 5x5 Mask
LP Filtering with 9x9 Mask LP Filtering with 15x15 Mask LP Filtering with 35x35 Mask
Ref. :- R. C. Gonzalez, and R. E. Woods, Digital Image Processing, Pearson
13
Order Statistics Filters
➢ Non-linear Spatial Filters
➢ Response is based on ordering (ranking) the pixel contained in the
image area encompassed by the filter and then replacing the value of
the centre pixel with the value determined by the ranking result.
➢ Examples of Order Statistics Filters
– Median filter
– Min filter
– Max filter
– Midpoint Filter
14
Median Filter
➢ Center pixel is replaced by the median of the neighborhood pixels
fˆ ( x, y ) = median{ g ( s, t )}
( s ,t )S xy
➢ Forces the center pixel not to assume the extreme values
➢ Very effectively eliminates salt-and-pepper noise
➢ Median filter fails to eliminate salt-and-pepper noise if the noisy pixels
are more than half of the neighborhood coefficients
15
Median Filter contd…
Example of Median Filter using 3x3 mask
2 3 4 6 6 2 3 4 6 6
0 15 2 6 6 2 2 4 6 6
Median
1 2 3 15 14 1 2 3 6 13
Filter
1 3 12 0 13 2 3 4 12 13
2 4 11 12 11 2 4 11 11 11
Median of [0,1,2,2,2,3,3,4,15] = 2
16
Median Filter contd…
Original Image Image after adding salt-and-pepper noise
Image after averaging Image after median filtering
17
Median Filter contd…
3x3 median mask 5x5 median mask
18
Max and Min Filters
➢ Center pixel is replaced by the Maximum or Minimum of the
neighborhood pixels
For max filter
fˆ ( x, y ) = max {g ( s, t )}
( s ,t )S xy
For min filter
fˆ ( x, y ) = min {g ( s, t )}
( s ,t )S xy
➢ Max Filter is useful in finding the brightest point in the image
➢ Min Filter is useful for finding darkest point in the image
19
Max and Min Filters contd…
Example of Max and Min Filters using 3x3 mask
2 3 4 6 6 15 15 15 6 6
2 15 2 6 6 15 15 15 6 6
Max
4 2 3 5 2 15 15 15 6 6
Filter
4 3 3 0 3 4 4 5 5 5
2 4 4 2 1 4 4 4 4 3
2 2 2 2 6
2 2 2 2 2
Min
2 2 0 0 0
Filter
2 2 0 0 0
2 2 0 0 0
20
Midpoint Filters
➢ Center pixel is replaced by the Midpoint of the Maximum and Minimum
gray level of the neighborhood pixels
fˆ ( x, y ) = max {g ( s, t )} + min {g ( s, t )}
1
2 ( s ,t )S xy ( s ,t )S xy
➢ Works best for the randomly distributed noise (Gaussian or Uniform
noise)
21
Midpoint Filters contd…
Example of Midpoint Filters using 3x3 mask
2 3 4 6 6 8 8 9 4 6
0 15 2 6 6 8 8 9 9 11
Midpoint
1 2 3 15 14 8 8 8 8 8
Filter
1 3 12 0 13 3 7 6 8 8
2 4 11 12 11 3 7 6 7 7
22
Sharpening Filters
23
Sharpening Filters
➢ These are High Pass Filters.
➢ It emphasize the high frequency component of an image and
attenuates low frequency component.
➢ Sharp edges & abrupt change in pixel values are made further
dominant.
➢ Areas of constant gray levels are converted to dark after High Pass
filtering.
24
Sharpening Filters contd…
Why Sharpening / High Pass Filters ???
➢ Input image may be blurred due to poor focus or other defects of the
imaging device.
➢ Principal objective of sharpening filters is to highlight fine details (like
edges) or to enhance detail that has been blurred.
25
Sharpening Filters contd…
Blurring = Averaging = Integrating ; Sharpening = Differentiation
➢ Image differentiation enhances edges & other discontinuities
including noise.
➢ Image differentiation for digital images can be considered as
differences between neighboring pixel values.
26
Conditions for the derivative function
1st Order Derivative
− must be zero in flat segments (areas of constant gray levels)
− must be nonzero at the onset of a gray level step or ramp
− must be nonzero along ramp
2nd Order Derivative
− must be zero in flat segments
− must be nonzero at the onset and end of gray level step or ramp
− must be zero along ramps of constant slope
27
Derivative of Digital Image
❖ For 1-D digital signal
1st Order Derivative
f
= f ( x − 1) − f ( x)
x
2nd Order Derivative
2 f
= f ( x + 1) + f ( x − 1) − 2 f ( x)
x 2
28
Derivative of Digital Image contd…
Ref. :- R. C. Gonzalez, and R. E. Woods, Digital Image Processing, Pearson
29
First Order Derivative : Gradient
For a function f(x,y)
f
f x = G x = = f ( x − 1, y ) − f ( x, y )
x
f
f y = G y = = f ( x, y − 1) − f ( x, y )
y
The Gradient of f at coordinates (x,y) is defined as 2-D column vector
f
Gx x
f = =
G y f
y
30
First Order Derivative : Gradient contd…
The Magnitude of this vector is
f = mag ( f )
= Gx 2 + G y 2
2
f f
2
= +
x y
To simplify the implementation, it is approximated as
f G x + G y Find the difference function first in x-direction
and then in y-direction.
31
Various Gradient Operators
Gradient Operator
-1 -1 1
Robert Cross Gradient Operator
-1 0 0 -1
0 1 1 0
32
Mask and their effects
Gx
-1
1 Gx + G y
Gy
-1 1
33
Mask and their effects
Gx
0 -1
Gx + G y
1 0
Gy
-1 0
0 1
34
Various Gradient Operators contd…
Prewitt Operator
-1 -1 -1 -1 0 1
0 0 0 -1 0 1
1 1 1 -1 0 1
35
Various Gradient Operators contd…
Sobel Operator
-1 -2 -1 -1 0 1
0 0 0 -2 0 2
1 2 1 -1 0 1
36
Example of Gradient Operators
37
Second Order Derivative : Laplacian
➢ These are Isotropic Filters or Rotation Invariant .
For a function f(x,y)
2 f 2 f
f= 2 +
2
x y 2
For a digital image
2 f
= f ( x + 1, y ) + f ( x − 1, y ) − 2 f ( x, y )
x 2
2 f
= f ( x, y + 1) + f ( x, y − 1) − 2 f ( x, y )
y 2
2 f = f ( x + 1, y ) + f ( x − 1, y ) + f ( x, y + 1) + f ( x, y − 1) − 4 f ( x, y )
38
Second Order Derivative : Laplacian contd…
Laplacian Operator
0 1 0
1 -4 1
0 1 0
➢ It is isotropic for increments of 900 i.e. even if rotated by 900 doesn’t
change the result.
➢ Not effective on Diagonal Edges.
39
Mask and their effects
0 1 0
1 -4 1
0 1 0
40
Various Laplacian Operators
0 1 0 1 1 1
1 -4 1 1 -8 1
0 1 0 1 1 1
0 -1 0 -1 -1 -1
-1 4 -1 -1 8 -1
0 -1 0 -1 -1 -1
41
Example of Laplacian Filtering
0 0 0 0 0
0 -1 0 -15 -15 -15 0 0
-1 4 -1 15 15 30 -15 0
0 -1 0 0 0 15 -15 0
0 0 0 0 0
0 0 15 -15 0
0 0 0 0 0
15 15 15 0 0
15 15 15 0 0
0 0 0 0 0
15 15 15 0 0
-1 -1 -1 -45 -45 -30 -15 0
-1 8 -1 45 45 75 -30 0
-1 -1 -1 0 0 45 -45 0
0 0 45 -45 0
42
Image Sharpening using Laplacian Filter
➢ HPF highlights discontinuities at the cost of slowly varying gray levels,
producing images with grayish edge lines on a dark featureless
background
How do we recover background while still preserving sharpening effect ?
➢ Add the original image to Laplacian image
(if center coefficient is negative then subtract)
43
Image Sharpening using Laplacian Filter contd…
44
Image Sharpening using Laplacian Filter contd…
f ( x, y ) − 2 f ( x, y ) if the center coefficient of the
Laplacian mask is negative
g ( x, y ) =
f ( x, y ) + f ( x, y )
2
if the center coefficient of the
Laplacian mask is positive
if 2 f = f ( x + 1, y ) + f ( x − 1, y ) + f ( x, y + 1) + f ( x, y − 1) − 4 f ( x, y )
then g ( x, y ) = f ( x, y ) − f ( x + 1, y ) + f ( x − 1, y ) + f ( x, y + 1) + f ( x, y − 1) + 4 f ( x, y )
= 5 f ( x, y ) − f ( x + 1, y ) + f ( x − 1, y ) + f ( x, y + 1) + f ( x, y − 1)
0 -1 0
-1 5 -1 Composite Laplacian Mask
0 -1 0
45
Image Sharpening using Laplacian Filter contd…
0 1 0
1 -4 1
0 1 0
0 -1 0
-1 5 -1
0 -1 0
46
Image Sharpening using Laplacian Filter contd…
47
1st Order versus 2nd Order
1st Order Operators 2nd Order Operators
• Nonzero at the onset of a gray • Nonzero at the onset and end of
level step or ramp a gray level step or ramp
• Non-zero along the ramp • Zero along the ramp
• Produces thick edges • Produces finer edges
• Response at & around isolated • Response at & around isolated
points is much weaker points is much stronger i.e. it
can enhance fine details
(including noise) much more
• Have stronger response to step • “Double edge” effect (thin
double line) at step changes
48
High-Boost Filtering
➢ HPF removes average brightness while enhancing edges & gives a
darker appearance to the image
➢ High Boost consists of adding a portion of the original image to the
HPF image
49
High-Boost Filtering contd…
f hb ( x, y ) = ( A − 1) f ( x, y ) + f s ( x, y ) where A 1
and f s ( x, y ) = sharpened image
If image is sharpened with Laplacian
then
A f ( x, y ) − 2 f ( x, y ) if the center coefficient of the
Laplacian mask is negative
f hb ( x, y ) =
A f ( x , y ) + 2
f ( x, y ) if the center coefficient of the
Laplacian mask is positive
50
High-Boost Filtering contd…
➢ If A = 1, then high-boost filtering is standard Laplacian sharpening
➢ As A increases, the contribution of the sharpening filter becomes less
and less important
➢ High-boost filtering helps in brightening the image
51
Various High-Boost Operators
0 -1 0 -1 -1 -1
-1 A+4 -1 -1 A+8 -1
0 -1 0 -1 -1 -1
52
Image Sharpening using High-Boost Filter contd…
53