You are on page 1of 31

Spatial Filtering

A spatial filter consists of (a) a neighborhood, and (b) a


predefined operation

Linear (filter that implements a linear operation) spatial


filtering of an image of size MxN with a filter of size mxn is
given by the expression
a b
g ( x, y )    w(s, t ) f ( x  s, y  t )
s  a t  b

11/14/2022 1
Spatial Filtering

11/14/2022 2
Spatial Correlation

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

Sum of the product

11/14/2022 3
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

Same thing but convolution is associative !!

11/14/2022 4
11/14/2022 5
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.

11/14/2022 6
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.

11/14/2022 7
Two Smoothing Averaging Filter Masks

11/14/2022 8
11/14/2022 9
Example: Gross Representation of Objects

11/14/2022 10
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

11/14/2022 11
Example: Use of Median Filtering for Noise Reduction

11/14/2022 12
Sharpening Spatial Filters

► Foundation

► Laplacian Operator

► Unsharp Masking and Highboost Filtering

► Using First-Order Derivatives for Nonlinear Image


Sharpening — The Gradient

11/14/2022 13
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
2
 f ( x  1)  f ( x  1)  2 f ( x)
x

11/14/2022 14
11/14/2022 15
Sharpening Spatial Filters: Laplace Operator

The second-order isotropic derivative operator is the


Laplacian for a function (image) f(x,y)
2 2
2  f  f
 f  2  2
x y
2 f
2
 f ( x  1, y )  f ( x  1, y )  2 f ( x, y )
x
2 f
2
 f ( x, y  1)  f ( x, y  1)  2 f ( x, y )
y
 2 f  f ( x  1, y )  f ( x  1, y )  f ( x, y  1)  f ( x, y  1)
- 4 f ( x, y )
11/14/2022 16
Sharpening Spatial Filters: Laplace Operator

11/14/2022 17
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.

11/14/2022 18
11/14/2022 19
Image scaling (Ch2)

(OR) y= ((x-min)/(max-min))*255

11/14/2022 20
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 (creates a mask)
3. Add the mask to the original

11/14/2022 21
Unsharp Masking: Demo

11/14/2022 22
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.

11/14/2022 23
Unsharp Masking and Highboost Filtering: Example

11/14/2022 24
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 )


2 2
Gradient Image M ( x, y)  mag(f )  g x  g y
11/14/2022 25
Image Sharpening based on First-Order Derivatives

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


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

M ( x, y ) | g x |  | g y |

z1 z2 z3
M ( x, y ) | z8  z5 |  | z6  z5 |
z4 z5 z6
z7 z8 z9
11/14/2022 26
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
11/14/2022 27
Image Sharpening based on First-Order Derivatives

11/14/2022 28
Example

11/14/2022 29
Example:

Combining
Spatial
Enhancement
Methods

Goal:

Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail

11/14/2022 30
Example:

Combining
Spatial
Enhancement
Methods

Goal:

Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail

11/14/2022 31

You might also like