Spatial Filters in Image Processing
Spatial Filters in Image Processing
Image Processing
Filters
Filters are classified as:
Low-pass (i.e., preserve low frequencies)
High-pass (i.e., preserve high frequencies)
Band-pass (i.e., preserve frequencies within a band)
Band-reject (i.e., reject frequencies within a band)
Low-pass (Smoothening)
HIgh-pass (Sharpening)
Spatial Filters
Spatial Filters used directly on pixels of an image. Mask is usually
considered to be added in size so that it has specific center pixel.
This mask is moved on the image such that the center of the
mask traverses all image pixels.
Classification on the basis of linearity:
1. Linear Spatial Filter
2. Non-linear Spatial Filter
General Classification:
Smoothing Spatial Filter: Smoothing filter is used for blurring and
noise reduction in the image. Blurring is pre-processing steps for
removal of small details and Noise Reduction is accomplished by
blurring.
Spatial Filters
Types of Smoothing Spatial Filter:
1. Linear Filter (Mean Filter)
2. Order Statistics (Non-linear) filter
1. Mean Filter:
Linear spatial filter is simply the average of the pixels contained in
the neighborhood of the filter mask. The idea is replacing the
value of every pixel in an image by the average of the grey levels
in the neighborhood define by the filter mask.
Types of Mean filter:
(i) Averaging filter: It is used in reduction of the detail in image.
All coefficients are equal.
(ii) Weighted averaging filter: In this, pixels are multiplied by
different coefficients. Center pixel is multiplied by a higher value
than average filter.
Spatial Filters
2. Order Statistics Filter:
It is based on the ordering the pixels contained in the image area
encompassed by the filter. It replaces the value of the center pixel
with the value determined by the ranking result. Edges are better
preserved in this filtering.
Types of Order statistics filter:
(i) Minimum filter: 0th percentile filter is the minimum filter. The value of
the center is replaced by the smallest value in the window.
(ii) Maximum filter: 100th percentile filter is the maximum filter. The
value of the center is replaced by the largest value in the window.
(iii) Median filter: Each pixel in the image is considered. First neighboring
pixels are sorted and original values of the pixel is replaced by the median
of the list.
Sharpening Spatial
Filters
Sharpening Spatial Filter:
It is also known as derivative filter. The purpose of the sharpening spatial filter is
just the opposite of the smoothing spatial filter. Its main focus in on the removal of
blurring and highlight the edges. It is based on the first and second order derivative.
First order derivative:
Must be zero in flat segments.
Must be non zero at the onset of a grey level step.
Must be non zero along ramps.
First order derivative in 1-D is given by:
f' = f(x+1) - f(x)
Second order derivative:
Must be zero in flat areas.
Must be zero at the onset and end of a ramp.
Must be zero along ramps.
Second order derivative in 1-D is given by:
f'' = f(x+1) + f(x-1) - 2f(x)
Spatial Correlation
• Correlation is the process of moving a filter mask (kernel) over the image and
computing the sum of products at each location.
• Correlation is the function of displacement of the filter. The first value of the
correlation corresponds to zero displacement of the filter, the second value
corresponds to one unit of displacement, and so on.
• Correlation is used to check the similarity between two images
a b
w( x, y ) f ( x, y ) w( s, t ) f ( x s, y t )
s a t b
08/05/2021 7
Spatial Correlation
08/05/2021
Spatial Convolution
The convolution of a filter w( x, y) of size m n
with an image f ( x, y ), denoted as w( x, y) f ( x, y)
a b
w( x, y ) f ( x, y ) w(s, t ) f ( x s, y t )
s a t b
08/05/2021 9
08/05/2021 10
Smoothing Spatial Filters
Smoothing filters are used for blurring and for noise
reduction
08/05/2021 11
Spatial Smoothing Linear Filters
w(s, t ) f ( x s, y t )
g ( x, y ) s a t b
a b
w(s, t )
s a t b
where m 2a 1, n 2b 1.
08/05/2021 12
Two Smoothing Averaging Filter Masks
08/05/2021 13
08/05/2021 14
Example: Gross Representation of Objects
08/05/2021 15
Order-statistic (Nonlinear) Filters
— Nonlinear
08/05/2021 16
Example: Use of Median Filtering for Noise
Reduction
08/05/2021 17
Sharpening Spatial Filters
► Foundation
► Laplacian Operator
08/05/2021 18
Sharpening Spatial Filters: Foundation
f
f ( x 1) f ( x)
x
2 f
f ( x 1) f ( x 1) 2 f ( x)
x 2
08/05/2021 19
08/05/2021 20
Sharpening Spatial Filters: Laplace Operator
x y
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 )
08/05/2021 21
Sharpening Spatial Filters: Laplace Operator
08/05/2021 22
Sharpening Spatial Filters: Laplace Operator
g ( x, y ) f ( x, y ) c 2 f ( x, y )
where,
f ( x, y ) is input image,
g ( x, y ) is sharpenend images,
c -1 if 2 f ( x, y ) corresponding to Fig. 3.37(a) or (b)
and c 1 if either of the other two filters is used.
08/05/2021 23
08/05/2021 24
Unsharp Masking and Highboost Filtering
► Unsharp masking
Sharpen images consists of subtracting an unsharp (smoothed)
version of an image from the original image
e.g., printing and publishing industry
► Steps
1. Blur the original image
2. Subtract the blurred image from the original
3. Add the mask to the original
08/05/2021 25
Unsharp Masking and Highboost Filtering
08/05/2021 26
Unsharp Masking: Demo
08/05/2021 27
Unsharp Masking and Highboost Filtering: Example
08/05/2021 28
Image Sharpening based on First-Order Derivatives
08/05/2021 29
Image Sharpening based on First-Order Derivatives
M ( x, y ) | g x | | g y |
z1 z2 z3
M ( x, y ) | z8 z5 | | z6 z5 |
z4 z5 z6
z7 z8 z9
08/05/2021 30
Image Sharpening based on First-Order Derivatives
Sobel Operators
M ( x, y ) | ( z7 2 z8 z9 ) ( z1 2 z2 z3 ) |
z1 z2 z3 | ( z3 2 z6 z9 ) ( z1 2 z4 z7 ) |
z4 z5 z6
z7 z8 z9
08/05/2021 31
Image Sharpening based on First-Order Derivatives
08/05/2021 32
Example
08/05/2021 33
Example:
Combining
Spatial
Enhancement
Methods
Goal:
Enhance the
image by
sharpening it and
by bringing out
more of the
skeletal detail
08/05/2021 34
Example:
Combining
Spatial
Enhancement
Methods
Goal:
Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail
08/05/2021 35
Spatial Filtering Methods
(or Mask Processing Methods)
output image
Spatial Filtering (cont’d)
Filters are classified as:
g(i,j)
Filtered
Image
f(i,j)
output image
Handling Pixels Close to Boundaries
Pad With Zeroes
0 0 0 ……………………….0
0 0 0 ……………………….0
or
Linear vs Non-Linear Spatial
Filtering Methods
A filtering method is linear when the output is a weighted sum
of the input pixels.
Gaussian
Smoothing filters – Example
Input Image Smoothed Image
Sharpening Filters (high-pass)
Useful for highlighting fine details.
The elements of the mask contain both
positive
and negative weights.
Sum of mask elements is 0.
2nd derivative
1st derivative
of
of
Gaussian
Gaussian
Sharpening Filters - Example
Warning: the results of sharpening might contain negative
values (i.e., re-map them to [0, 255])
Averaging
Gaussian
Median filtering (non-linear)
Smoothing Filters: Averaging
Un-weighted 3 3 0 1 1
4 5 4 0 2 Mask
2 3 4 0 1
1 5 6 7 1 1 1 1
1 0 2 3 5 1 1 1
Input Image 4 5 6 7 0 1 1 1
Smoothing Filters: Averaging
(cont’d)
Mask size determines the degree of smoothing (loss of detail).
original 3x3 5x5 7x7
15x15 25x25
Image
Smoothing
Smoothing filters: Gaussian
The weights are samples of a 2D Gaussian
function:
σ = 1.4
mask size is
a function of σ:
Smoothing filters: Gaussian
(cont’d)
• σ controls the amount of smoothing
• As σ increases, more samples must be obtained to represent the
Gaussian
function accurately.
σ=3
Smoothing filters: Gaussian (cont’d)
Averaging vs. Gaussian Smoothing
Averaging
Gaussian
Smoothing Filters: Median Filtering
(cont’d)
Replace each pixel by the median in
a neighborhood around the pixel.
The size of the neighborhood controls
the amount of smoothing.
Smoothing Filters: Median Filtering
(non-linear)
Very effective for removing “salt and pepper” noise (i.e., random
occurrences of black and white pixels).
Median
Averaging
Original Image Image with Noise Filtering
Common Sharpening Filters
Unsharp masking
High Boost filter
Gradient (1st derivative)
Laplacian (2nd derivative)
Sharpening Filters: Un sharp Masking
- =
(after contrast
enhancement)
Sharpening Filters: High Boost
Image sharpening emphasizes edges but
details are lost.
High boost filter: Amplify input image, then
subtract a Low pass image.
(A-1) + =
Sharpening Filters: High
Boost (cont’d)
If A=1, we get unsharp masking.
If A>1, part of the original image is
added back to the high pass filtered image.
High boost
A=1.4 A=1.9
Sharpening Filters: Derivatives
Taking the derivative of an image results
in sharpening the image.
2nd derivative
1 derivative
st
of
Gaussian of Gaussian
Gaussian
First and Second Derivative
65
Gradient (cont’d)
Thank You
68