Machine_Learning_Course_Shashi_Lecture_04
Machine_Learning_Course_Shashi_Lecture_04
Shashikant Dugad,
IISER Mohali
● Types of filters:
○ Mean Filter, Median Filter, Gaussian Filter, Convolution Filter ….
● Purpose: Reduce noise, extract features, create images that are rich
in feature of interest, reduce size etc.
○ Gaussian noise contains variations in intensity that are drawn from gaussian or
normal distribution and is a very good model for many kinds of sensor noise, such
as the noise due to camera electronics
(c) (d)
Images
Figure corrupted by salt of
4.5: Examples and pepper,
images impulse,byand
corrupted saltGaussian noise. impulse,
and pepper, (a) & (b)
Original images. (c) Salt and pepper noise. (d) Impulse noise. (e) Gaussian noise.
and Gaussian noise. (a) & (b) Original images. (c) Salt and pepper noise.
Shashikantnoise.
R Dugad, IISER Mohali 5
(d) Impulse noise. (e) Gaussian
Mean Filter
● The mean filter is implemented by a local averaging operation where the value of
each pixel is replaced by the average of all the values in the local neighbourhood
where M is the total number of pixels in the neighbourhood.
● If f(i,j) is an initial image and h(i, j) is the final image after applying mean filter of
say 3 x 3 neighbourhood then, h(i, j) can be obtained as,
#&% (&%
1
ℎ 𝑖, 𝑗 = ( ( 𝑓(𝑘, 𝑙)
9
!"#$% '"($%
Mean Filter Smoothening Filter
1/9 1/9 1/9 1/16 1/8 1/16
1/9 1/9 1/9 1/8 1/4 1/8
1/9 1/9 1/9 1/16 1/8 1/16
r r
t
I I
L Mean
L Filter IResult
r
4.3. LINEAR FILTERS 121
I L L
I ___ .._ ___.._ 1 1
L L
I ___.._ 1
Original Image
I
MF: 3 x 3 L MF: 7 x 7
L
I ___.._ 1
I I
I
, ,
,
J J J
MF: 5 x 5
Figure Figure
4.7: The Figure
4.7:results
The of4.7:
a 3 The
results xof3,aresults3, of
53 xx 5, 5 xa 35,
and 7 xxand
3,7 5mean
7x x5,7and 7 on
filter
mean xfilter
7the
mean filter
noisy
on on the noisy
the noisy
Shashikant R Dugad, IISER Mohali 7
imagesimages fromimages
from Figure 4.5. from
Figure 4.5. Figure 4.5.
Median Filter
● An alternative approach is to replace each pixel value with the median of the gray
values in the local neighbourhood.
● Median filters effective in removing salt and pepper and impulse noise while
retaining image details because they do not depend on values which are
significantly different from typical values in the neighbourhood.
\ \
'" 75
around [i, j] f\
\ 98
38 99
● Sort the pixels into ascending order \
● Select the value of the middle pixel Figure 4.9: An example illustrating the median filter using a 3 x 3 neighbor-
hood.
as the new value for pixel [i,j].
where the Gaussian spread parameter (J determines the width of the Gaus-
sian. For image processing, the two-dimensional zero-mean discrete Gaussian
function,
(4.12)
Shashikant R Dugad, 9
is used asIISER Mohalifilter. A plot of this function is shown in Figure 4.11.
a smoothing
--. --. --Median Filter Results
.,-.,-.,-
4.3. LINEAR FILTERS 121
I
MF: 3 x 3 L MF: 7 x 7
L
I ___.._ 1
MF: 5 x 5
Figure
Figure Figure
4.10: 4.10:
The The
4.10:results
The of aresults
results 3,a of
53 xa5,
3 xofShashikant3 xand
3, 5 3, 57 xxand
x 5, 5, and
7 x 77 median
7 median x filter
7 median filter
filter
on the on on the
the
noisy noisy
noisy 10
R Dugad, IISER Mohali
images
imagesimages
from fromfrom
Figure 4.5.Figure
Figure 4.5. 4.5.
Gaussian Filter
● Gaussian filters are a class of smoothening filters where, the weights are chosen according
to the shape of a Gaussian function. The Gaussian smoothing filter is a very good filter for
removing noise drawn from a normal distribution.!
𝒌𝟐 𝒍𝟐
( (
= ∑𝒎
𝒌#𝟏 𝒆 𝟐𝝈𝟐 ∑𝒏𝒍#𝟏 𝒆 𝟐𝝈𝟐 ∗ 𝒇 𝒊 − 𝒌, 𝒋 − 𝒍
● Seperatibility property can be used to apply two dimensional filter using 1-dimensional
choosing a value 134 CHAPTER 4. IMAGE FILTERIN
filter for (72,we can evaluate it over an n x n window to
n a kernel, or mask, for which the value at [0,0] equals 1. For example,
ng (72 =●2 and
Fornlarge
= 7, n,
thegaussian function yields
above expression can
Now,bebyused:
the n=7, σthe
array:
multiplying
2=2
rest of the weights by k, we obtain
[i,j] -3 -2 -1 o 1 2 3 [i,j] -3 -2 -1 0 1 2 3
-3 .011 .039 .082 .105 .082 .039 .011 -3 1 4 7 10 7 4 1
-2 .039 .135 .287 .368 .287 .135 .039 -2 4 12 26 33 26 12 4
-1 .082 .287 .606 .779 .606 .287 .082 -1 7 26 55 71 55 26 7
o .105 .368 .779 1.000 .779 .368 .105 0 10 33 71 91 71 33 10
1 .082 .287 .606 .779 .606 .287 .082 1 7 26 55 71 55 26 7
2 .039 .135 .287 .368 .287 .135 .039 2 4 12 26 33 26 12 4
3 .011 .039 .082 .105 .082 .039 .011 3 1 4 7 10 7 4 1
Shashikant R Dugad, IISER Mohali 12
ever, we desire the filter weights to be integerThis is the
values forresulting convolution mask for the Gaussian filter (also shown
ease in compu-
Result of Gaussian Filter
~~J'
I ~
~
!
~~
....
t
I
(a) (b)
The results
Figure 4.16: of
Thesmoothing
results ofusing the 7x7 using
smoothing Gaussian
the mask.
7 x 7 (a) Originalmask.
Gaussian image(a)
corruptedimage
Original by Gaussian noise.by
corrupted (b)Gaussian
Smoothed noise.
image. (b) Smoothed image.
Shashikant R Dugad, IISER Mohali 13
Linear linear
• One simple version: Filtering
filtering
(cross-correlation, convolution)
● Linear Filtering: Replace each pixel by a linear combination of its neighbours and
self – Replace each pixel by a linear combination of its
neighborsconvolution
○ cross-correlation,
● • The prescription
The prescription for the linear
for the linear combination combination
is called the “kernel” (oris“mask”,
“filter”) called the “kernel” (or “mask”, “filter”)
10 5 3 0 0 0
4 6 1 0 0.5 0 8
1 1 8 0 1 0.5
● Same Kernel applied across entire image are referred as Space Invariant Filters
Source: L. Zhang
Shashikant R Dugad, IISER Mohali 14
Cross-correlation
Cross-correlation
●
Let be the image, be the kernel (of
Let F be the input image and H be the kernel (of size 2k+1 x 2k+1), and G
Cross-correlation
be thesize 2k+1
output x 2k+1),
image and be the output
image
Let be the image, be the kernel (of
size 2k+1 x 2k+1), and be the output
image
0 0 0
*
Linear filters: =
0 1 0 examples
0 0 0
0 0 0
* 1
0
0
0
0
0
=
Source: D. Lowe
1 1 1
*
Linear filters:
1 1 1 =
examples
1 1 1
-
0 0 0 1 1 1
* 0
0
2
0
0
0
1
1
1
1
1
1
=
Source: D. Lowe
Sharpening filter
Original (accentuates edges)
● Usefulness of associativity
○ Often apply several filters one after another: (((a ★ b1) ★ b2) ★ b3)
○ This is equivalent to applying one filter: a * (b1 ★ b2 ★ b3)
● The kernel size, stride value can substantially reduce the size of output im the
output size and computational efficiency of the network, influencing feature
extraction and spatial dimensions of image.
Shashikant R Dugad, IISER Mohali 20
Movement of Filter
Stride = 1
Stride = 2
𝑵𝒊𝒏
𝒓𝒐𝒘 4 𝟐 𝑿 𝑷 9 𝑫𝒓𝒐𝒘 𝑿 (𝑲𝒓𝒐𝒘 9𝟏) 9𝟏
𝑵𝒐𝒖𝒕
𝒓𝒐𝒘 = 𝑺𝒓𝒐𝒘
+1
𝑴𝒊𝒏
𝒄𝒐𝒍 4 𝟐 𝑿 𝑷 9 𝑫𝒄𝒐𝒍 𝑿 (𝑲𝒄𝒐𝒍 9𝟏) 9𝟏
𝑴𝒐𝒖𝒕
𝒄𝒐𝒍 = 𝑺𝒄𝒐𝒍
+1
padding=1, stride=2