Professional Documents
Culture Documents
5:
Filtering in the spatial domain
Dr. Stephen Czarnuch
Memorial University of Newfoundland 1
Agenda
• Correlation vs convolution
• Smoothing filters
– Linear filters: Average filter
– Nonlinear filters: Median filters
• Sharpening filters
– First derivative: Sobel
– Second derivative: Laplace
Memorial University of Newfoundland 2
Spatial Filtering
• Intensity transformation: Changing the intensity level at
a location of an image without using the neighbouring
pixels
• Point operations
• Spatial filtering: Changing the intensity level at a location
of an image using the intensity of neighbouring pixels
• Neighbourhood operations
3X3 neighbourhood
Memorial University of Newfoundland 3
Spatial Filtering cont..
• A spatial filter consists of
– a neighborhood, and
– a predefined operation
• Linear spatial filtering of an image of size with a
filter of size is given by the following expression:
𝑔 𝑥, 𝑦 𝑤 𝑠, 𝑡 𝑓 𝑥 𝑠, 𝑦 𝑡
– Where:
𝑚 1 𝑛 1
𝑎 ;𝑏
2 2
𝑥 0,1,2 … 𝑀 1; 𝑦 0,1,2 … 𝑁 1
𝑤 𝑠, 𝑡 filter coefficients
Memorial University of Newfoundland 4
Spatial Filtering
Figure 3.34: Gonzalez and
Wood, Digital Image Processing
Memorial University of Newfoundland 5
https://towardsdatascience.com/intuitively‐understanding‐convolutions‐for‐deep‐learning‐1f6f42faee1
Memorial University of Newfoundland 6
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
• Each pixel in the image is the result of applying the filter on the
neighborhood and calculating the new value using this equation
Memorial University of Newfoundland 7
Spatial Convolution
• A closely related concept is called convolution
• Convolution is basically the same as
correlation except that the filter is first rotated
by 180 degrees before application
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
Memorial University of Newfoundland 8
Correlation
Convolution
Memorial University of Newfoundland 9
Notes:
• What happens if the filter is symmetric?
– Convolution and correlation are the same
• Using correlation or convolution to perform
spatial filtering can have very significant
differences:
– Convolution is multiplication in the frequency
domain
• Associative
– Correlation is multiplication by the complex
conjugate in the frequency domain
• Not associative
Memorial University of Newfoundland 10
Smoothing Spatial Filters
• Smoothing filters are used for blurring and for
random noise reduction
• Smoothing filters are also called:
– Averaging filters
– Low‐pass filters
• Smoothing essentially removes “irrelevant” data:
– Pixel regions that are small relative to the size of the
filtering kernel
• Smoothing spatial filters can include linear filters
and nonlinear filters
Memorial University of Newfoundland 11
Two Smoothing Averaging Filter Masks
Memorial University of Newfoundland 12
Implementation for filtering an image
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.
Memorial University of Newfoundland 13
Image smoothing with masks of
various sizes
Memorial University of Newfoundland 14
Example: Gross Representation of Objects
• Hubble space telescope
Intensity of small objects blend with background and large objects
become blob‐like and easy to detect
Memorial University of Newfoundland 15
Matlab Example
clear all
close all
% Synthetic image of a white square
I = zeros(200,200);
I(50:150, 50:150) = 1;
imshow(I,[]);
Memorial University of Newfoundland 16
Smoothing filters: Gaussian
• The weights are samples of the
Gaussian function
• For filtering out considerable
amount of high frequency
components from the image
Gaussian kernel and filtering
Memorial University of Newfoundland 18
σ and smoothing
• σ controls the amount of smoothing
• As σ increases, more samples must be obtained to represent
the Gaussian function accurately.
σ=3
Results of Gaussian filtering
Matlab command: imgaussfilt(A,sigma)
Filters image A with a 2‐D Gaussian smoothing kernel
with standard deviation specified by sigma
Original image
Memorial University of Newfoundland 20
Nonlinear (Order‐statistic) spatial
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
– Examples include: median filter, max filter, min
filter
Memorial University of Newfoundland 21
Median Filter
• Consider a m x n kernel:
– Order the pixels according to their values
– Select the centre, or median value
• E.g., a 3 x 3 filter
– Neighbours: (10, 20, 20, 20, 15, 20, 20, 25, 100)
– Sorted: (10, 15, 20, 20, 20, 20, 20, 25, 100)
• Properties:
– Excellent noise reduction for certain types of random noise
(e.g., impulse/salt and pepper)
• With considerably less blurring than linear filters of similar size
– Mean produces a value that does not necessary exist in the
image.
• Median always replaces with an existing pixel value
Memorial University of Newfoundland 22
Use of median filtering for noise
reduction
Matlab Example
I=imread('cameraman.tif');
I_noise=imnoise(I,'salt & pepper',0.09);
w1=fspecial('average',[5 5]);
I_Ave=imfilter(I_noise,w1);
I_Med=medfilt2(I_noise); % 2D median filtering
figure, imshow(I_noise); figure, imshow(I_Ave); figure, imshow(I_Med);