4 - Spatial - Filtering
4 - Spatial - Filtering
O. V. Ramana Murthy
B206, AB2
Electrical and Electronics Engineering
Amrita Vishwa Vidyapeetham, Coimbatore
Contents
Spatial Filter
Filter Mask
Averaging(Linear) and Median (non-linear) filtering
Correlation vs convolution
Sharpening
Edge Detection
2
Spatial
Filter
Filtered image 𝑔 𝑥, 𝑦 =
𝑎 𝑏
𝑤 𝑠, 𝑡 𝑓(𝑥 + 𝑠, 𝑦 + 𝑡)
𝑠=−𝑎 𝑡=−𝑏
3
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Spatial Filter
Spatial Filter consists of
1. Neighbourhood (small rectangle/square)
2. Predefined operation on the image pixels encompassed
by the neighbourhood.
A (filtered) image is generated as the center of the filter
visits each pixel in the input image.
Linear or non-linear spatial filter!
For a mask of size m × n, m = 2a+1, n = 2b+1,
applied on image of size M × N
4
Filter Mask
Rewriting as sum of products
𝑅 = 𝑤1 𝑧1 + 𝑤2 𝑧2 +…𝑤𝑚𝑛 𝑧𝑚𝑛
𝑚𝑛
= 𝑤𝑘 𝑧𝑘
𝑘=1
For 3 × 3 mask, mn = 9;
If it is average operator, i.e. 𝑤𝑖 = 1,
1 9
𝑅= σ 𝑧
9 𝑘=1 𝑘
6
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Average Filtering
8
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Smoothing – Neighbourhood averaging
9
Smoothing – Neighbourhood averaging
10
Sharpening – Laplacian Filter
(a) was obtained using the Laplacians of the RGB component images. These results were combined to
produce the sharpened full-color result.
(b) shows a similarly sharpened image based on the HSI components. This result was generated by
combining the Laplacian of the intensity component with the unchanged hue and saturation
components.
(c) The difference between the RGB and HSI sharpened images.
11
Non-linear filter
Median filter (HW: show median operator is non-linear
operator)
Example. Suppose a 3 × 3 neighborhood has values
(10,20,20,20,15,20,20,25,100)
Sort in ascending order (10,15,20,20,20,20,20,25,100)
Median is center value i.e 20 (not average!)
Median thus forces points with distinct intensity levels to be
more like their neighbors.
Effective in the presence of salt-and-pepper noise
12
Average vs Median Filter
13
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Spatial Correlation vs Convolution
14
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Spatial Correlation vs Convolution
15
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
16
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Spatial Correlation vs Convolution
Correlation is the process of moving a filter mask over the
image and computing the sum of products at each location.
Convolution is similar to correlation except that the filter is
first rotated 1800.
Convolving a function with unit impulse yields a copy of the
function at the location of the impulse.
Correlation is used for finding matches between images
17
First and Second Derivatives
First derivative
1. Must be zero in the areas of constant intensity
2. Must be non-zero at the onset of an intensity step or ramp
3. Must be non-zero along ramps
Second derivative
1. Must be zero in the areas of constant intensity
2. Must be non-zero at the onset and end of an intensity step
or ramp
3. Must be zero along ramp of constant slope
18
First and Second Derivatives
These filters are based on First and Second derivatives
𝜕𝑓
=𝑓 𝑥+1 −𝑓 𝑥
𝜕𝑥
𝜕2 𝑓
= 𝑓 𝑥 + 1 + 𝑓 𝑥 − 1 − 2𝑓(𝑥)
𝜕𝑥 2
19
First and Second Derivatives
20
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
First and Second Derivatives
21
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Sharpening filter
Isotropic filter (rotation-invariant) – whose response is
independent of the direction of the discontinuities in the
image to which the filter is applied.
Example: Laplacian filter defined as
𝜕2 𝑓 𝜕2 𝑓
𝛻2𝑓 = +
𝜕𝑥 2 𝜕𝑦 2
𝜕2 𝑓
= 𝑓 𝑥 + 1, 𝑦 + 𝑓 𝑥 − 1, 𝑦 − 2𝑓(𝑥, 𝑦)
𝜕𝑥 2
𝜕2 𝑓
= 𝑓 𝑥, 𝑦 + 1 + 𝑓 𝑥, 𝑦 − 1 − 2𝑓(𝑥, 𝑦)
𝜕𝑦 2
𝛻 2 𝑓 = 𝑓 𝑥 + 1, 𝑦 + 𝑓 𝑥 − 1, 𝑦 + 𝑓 𝑥, 𝑦 + 1 +
𝑓 𝑥, 𝑦 − 1 − 4𝑓(𝑥, 𝑦)
22
Sharpening filter – Laplacian
23
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Image Sharpening
𝑔 𝑥, 𝑦 =
𝑓 𝑥, 𝑦 +
2
𝑐 𝛻 𝑓(𝑥, 𝑦)
c = -1 if Figure
3.37 (a) or (b)
is used. Else 1.
24
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Edge Detection – Image Gradient
𝜕𝑓
𝑔𝑥 𝜕𝑥
𝛻𝑓 ≡ 𝑔𝑟𝑎𝑑 𝑓 = 𝑔 = 𝜕𝑓
𝑦
𝜕𝑦
𝜕𝑓(𝑥, 𝑦)
𝑔𝑥 = = 𝑓 𝑥 + 1, 𝑦 − 𝑓(𝑥, 𝑦)
𝜕𝑥
𝜕𝑓(𝑥, 𝑦)
𝑔𝑦 = = 𝑓 𝑥, 𝑦 + 1 − 𝑓 𝑥, 𝑦
𝜕𝑦
𝑀 𝑥, 𝑦 = 𝑚𝑎𝑔 𝛻𝑓 = 𝑔𝑥2 + 𝑔𝑦2
𝑔𝑥
𝛼 𝑥, 𝑦 = 𝑡𝑎𝑛−1
𝑔𝑦
25
Image Gradient
𝜕𝑓(𝑥, 𝑦)
𝑔𝑥 = = 𝑓 𝑥 + 1, 𝑦 − 𝑓(𝑥, 𝑦)
𝜕𝑥
𝜕𝑓(𝑥, 𝑦)
𝑔𝑦 = = 𝑓 𝑥, 𝑦 + 1 − 𝑓(𝑥, 𝑦)
𝜕𝑦
26
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Image Gradient
27
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Image Gradient
28
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Spatial Enhancement
29
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
Spatial Enhancement
30
Source: Digital Image Processing, Gonzalez and Woods, 3rd edition.
References
Chapter 3, Digital Image Processing, Gonzalez and Woods,
3rd edition.
31
Suggested viewing
Digital image processing- p018 Introduction to local
neighborhood operations
https://www.youtube.com/watch?v=GWCB3pKi2ko
Digital image processing- p018 Introduction to local
neighborhood operations
https://www.youtube.com/watch?v=GWCB3pKi2ko
32