0% found this document useful (0 votes)
1K views

Spatial Filters in Image Processing

Spatial filters are used for image processing tasks like smoothing and sharpening by operating directly on pixel values, and are classified based on whether they preserve low, high, or specific frequency bands. Smoothing spatial filters like mean and order statistics filters are used for noise reduction and blurring, while sharpening filters like the Laplacian emphasize edges by using derivatives. Non-linear filters like the median filter better preserve edges compared to linear filters.

Uploaded by

Mudassir Rafiq
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Spatial Filters in Image Processing

Spatial filters are used for image processing tasks like smoothing and sharpening by operating directly on pixel values, and are classified based on whether they preserve low, high, or specific frequency bands. Smoothing spatial filters like mean and order statistics filters are used for noise reduction and blurring, while sharpening filters like the Laplacian emphasize edges by using derivatives. Non-linear filters like the median filter better preserve edges compared to linear filters.

Uploaded by

Mudassir Rafiq
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

Spatial Filters in

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

The correlation 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 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

Blurring is used in removal of small details and bridging of


small gaps in lines or curves

Smoothing spatial filters include linear filters and nonlinear


filters.

08/05/2021 11
Spatial Smoothing Linear Filters

The general implementation for filtering an M  N image


with a weighted averaging filter of size m  n is given
a b

  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

— Based on ordering (ranking) the pixels contained in the


filter mask

— Replacing the value of the center pixel with the value


determined by the ranking result

E.g., median filter, max filter, min filter

08/05/2021 16
Example: Use of Median Filtering for Noise
Reduction

08/05/2021 17
Sharpening Spatial Filters

► Foundation

► Laplacian Operator

► Unsharp Masking and Highboost Filtering

► Using First-Order Derivatives for Nonlinear Image


Sharpening — The Gradient

08/05/2021 18
Sharpening Spatial Filters: Foundation

► The first-order derivative of a one-dimensional function f(x)


is the difference

f
 f ( x  1)  f ( x)
x

► The second-order derivative of f(x) as the difference

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

The second-order isotropic derivative operator is the


Laplacian for a function (image) f(x,y)
 2
f  2
f
 f  2  2
2

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

Image sharpening in the way of using the Laplacian:

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

Let f ( x, y ) denote the blurred image, unsharp masking is


g mask ( x, y)  f ( x, y )  f ( x, y )
Then add a weighted portion of the mask back to the original
g ( x, y )  f ( x, y )  k * g mask ( x, y ) k 0

when k  1, the process is referred to as 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

For function f ( x, y ), the gradient of f at coordinates ( x, y )


is defined as
 f 
 g x   x 
f  grad( f )      
 g y   f 
 y 

The magnitude of vector f , denoted as M ( x, y )


Gradient Image M ( x, y)  mag(f )  g x  g y
2 2

08/05/2021 29
Image Sharpening based on First-Order Derivatives

The magnitude of vector f , denoted as M ( x, y )


M ( x, y)  mag(f )  g x  g y 2 2

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

Roberts Cross-gradient Operators


M ( x, y ) | z9  z5 |  | z8  z6 |

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:

 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)
Spatial Filtering (cont’d)
 Need to define:
 A neighborhood (or mask)
 An Mask Operation

• Typically, the neighborhood


is rectangular and its size is
much smaller than that of
f(x,y)

- e.g., 3x3 or 5x5


Operation:
w(i,j)

g(i,j)
Filtered
Image

f(i,j)

A filtered image is generated as the center of the mask moves to


every pixel in the input image.
Spatial filtering - Operation
Example: weighted sum of input pixels.
mask
weights:

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.

 Methods that do not satisfy the above property are called


non-linear.
 e.g.,
Spatial Filters
 We will mainly focus on two types of filters:
 Smoothing (low-pass)
 Sharpening (high-pass)
Smoothing Filters (low-pass)
 Useful for reducing noise and eliminating
small details.
 The elements of the mask must be positive.
 Sum of mask elements is 1 (after
normalization).

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])

Input Image Sharpened Image

(for better visualization, the original


image is added to the sharpened image)
Common Smoothing Filters

 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

 Obtain a sharp image by subtracting a low


pass filtered (i.e., smoothed) image from the
original image:

- =
(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

One way to implement


high boost filtering is using
the masks below
Sharpening Filters: High
Boost (cont’d)

A=1.4 A=1.9
Sharpening Filters: Derivatives
 Taking the derivative of an image results
in sharpening the image.

 The derivative of an image (i.e., 2D function)


can be computed using the gradient.
How do we choose the mask
weights?
 Typically, by sampling certain functions:

2nd derivative
1 derivative
st
of
Gaussian of Gaussian
Gaussian
First and Second Derivative

65
Gradient (cont’d)

 Gradient magnitude: provides


information about edge strength.
 Gradient direction: perpendicular to
the direction of the edge.
Derivative Results and Laplacian:
Any Questions?

Thank You

68

You might also like