Spatial Filtering and Edge
Detection
Grey-scale image (8 bits/pixel)
binary decimal
0
255
2
256 shades of grey
Digital Image (8-bit) and Image Matrix (5x5)
0 255 0 0 255
0 255 0 0 255
0 255 0 0 255
0 255 0 0 255
0 255 0 0 255
Binary Image Image matrix
255 255 255 255 255
100 100 100 100 100
50 50 50 50 50
0 0 0 0 0
50 50 50 50 50
Grey Image Image matrix 3
Digital Color Image and Image Matrix
255 255 0 0 0
255 255 0 0 0
255 255 0 0 0
255 255 0 0 0 255 0 0 255 0
255 255 0 0 0 255 0 0 255 0
Red component 255 0 0 255 0
255 0 0 255 0
255 0 0 0 255
255 0 0 255 0
Image
255 0 0 0 255 Green component
255 0 0 0 255
255 0 0 0 255
255 0 0 0 255
4
Blue component
Image Filtering
54 100 208 1 1 1
49 106 198 ×1/9 1 1 1
45 90 185 1 1 1
23 42 86
3x3 part of Image 3*3 Smoothing Filter
54 100 208
49 106 198
45 90 185
e = 1/9[106 + 54 +100 + 208 +
49 + 198 +45 + 90 + 185]
= 115
Image f (x, y)
Smoothing Spatial Filtering
54 100 208 1 1 1
49 106 198 × 1/9 1 1 1
45 90 185 1 1 1
23 42 86
3x3 part of Image 3*3 Smoothing Filter
54 100 208
49 106 198
45 90 185
Image f (x, y) 23 42 86
54 100 208
49 115 198
45 90 185
After averaging
Smoothing Spatial Filtering
Origin x
23 42 86 1 1 1
54 100 208 ×1/9 1 1 1
49 106 185 1 1 1
23 42 86
3*3 Smoothing
54 100 208
49 106 198
Filter
45 90 185
e = 94.77 95
23 42 86
54 95 208
y Image f (x, y) 49 115 198
45 90 185
Smoothing Spatial Filtering
Origin x
23 42 86 1 1 1
54 100 208 ×1/9 1 1 1
49 106 185 1 1 1
23 42 86
3*3 Smoothing
54 100 208
49 106 198
Filter
45 90 185
e = 94.77 95
23 42 86
54 95 208
y Image f (x, y) 49 115 198
45 90 185
Smoothing Spatial Filtering
Original Image After averaging by 3x3 filter
Smoothing Spatial Filtering
After averaging by 3x3 filter After averaging by 5x5 filter
Smoothing Spatial Filtering
After averaging by 15x15 filter After averaging by 35x35 filter
Weighted Smoothing Filters
Provides more effective 2 1
1
smoothing
Pixels closer to the 2 4 2
A= (1/16)
central pixel are more
important 1 2 1
Often referred to as a
Gaussian filter Weighted
averaging filter
Application of average filter
Original Image
Application of average filter
Original Image Smoothed Image
5×5 mask
Application of average filter
Original Image Smoothed Image Thresholded Image
5×5 mask G(x,y) =255, if f(x,y)>120
=0, otherwise
• Averaging removes finer details
• Thresholding separates gross details
Example: Spatial filters
Image matrix is given below. Determine the effect of
1. 3x3 and 5x5 averaging filters
2. 3x3 weighted averaging filter
45 56 42 63 54
20 47 56 28 53
63 59 26 38 47
67 36 27 48 51
43 36 42 65 43
Example: Spatial filters
Image matrix is given below. Determine the effect of
1. 3x3 and 5x5 averaging filters
2. 3x3 weighted averaging filter
45 56 42 63 54 45 56 42 63 54 45 56 42 63 54
20 47 56 28 53 20 47 56 28 53 20 47 56 28 53
63 59 26 38 47 63 59 26 38 47 63 59 26 38 47
67 36 27 48 51 67 36 27 48 51 67 36 27 48 51
43 36 42 65 43 43 36 42 65 43 43 36 42 65 43
Apply filter on top Move filter to
left corner process each pixel
Example: Spatial filters
Image matrix is given below. Determine the effect of
1. 3x3 and 5x5 averaging filters
2. 3x3 weighted averaging filter
45 56 42 63 54 45 56 42 63 54
20 47 56 28 53 20 46 46 45 53
1 1 1
63 59 26 38 47 63 44 41 42 47
x (1/9) 1 1 1 =
67 36 27 48 51 67 44 42 43 51
1 1 1
43 36 42 65 43 43 36 42 65 43
45 56 42 63 54 1 1 1 1 1 45 56 42 63 54
20 47 56 28 53 1 1 1 1 1 20 47 56 28 53
x (1/25)
63 59 26 38 47 1 1 1 1 1= 63 59 46 38 47
67 36 27 48 51 1 1 1 1 1 67 36 27 48 51
43 36 42 65 43 1 1 1 1 1 43 36 42 65 43
Example: Spatial filters
Image matrix is given below. Determine the effect of
1. 3x3 weighted averaging filters
45 56 42 63 54 45 56 42 63 54
20 47 56 28 53 20 47 45 44 53
1 2 1
63 59 26 38 47 x(1/16) 2 4 2 = 63 47 39 40 47
67 36 27 48 51 67 43 38 44 51
1 2 1
43 36 42 65 43 43 36 42 65 43
Sharpening Spatial Filters
• Remove blurring in images
• Highlight transition in intensity (edges)
• Uses spatial differentiation
• Differentiation measures the rate of change of a pixel
intensities
• Image gradient is based on differentiation
Image Gradients
• Is fundamental building block of many computer vision and image
processing techniques
• Use gradients to
• detecting edges
• find contours
• outlines of objects
• Compute gradient magnitude and gradient orientation
• Image descriptors such as Histogram of Oriented Gradients and
SIFT are built upon image gradient representations
Detection of Edges
Computation of Image gradients is a pre-processing
step to detecting edges in images
Detection of Edges
Computation of Image gradients is a pre-processing
step to detecting edges in images
Gradient of image
• Image pixels
f(x-1,y)
• Gradient along vertical direction
gy = f(x+1,y) – f(x-1,y)
f(x,y-1) f(x,y) f(x,y+1)
• Gradient along horizontal direction
f(x+1,y)
gx = f(x,y+1) – f(x,y-1)
Mask/ filter for gx -1 0 1
-1
Mask/ filter for gy
0
1
Image Gradient/ Gradient Vector
gy
𝑀= 𝑔𝑥 2 + 𝑔𝑦 2 ( x, y) tan
1
gx
• M is magnitude and α is angle of gradient
• Size of matrix for magnitude and angle is same as image
• Magnitude is the value of rate of change in the direction of
gradient vector
• Magnitude of gradient is used to measure how strong the
change in image intensity is
Image Gradients
• The gradient orientation is used to determine in which direction
the change in intensity is pointing
• Direction of edge is normal to direction of gradient
• Direction of gradient is also called edge normal
Image Gradients
• The gradient orientation is used to determine in which direction
the change in intensity is pointing
• Direction of edge is normal to direction of gradient
• Direction of gradient is also called edge normal
Gradient of image
image gx gy
10 10 50 10 10 -1
-1 0 1 -1
10 10 50 10 10 0
0
10 10 50 10 10 1
1
10 10 50 10 10
gy 𝑀𝑎𝑔 = 𝑔𝑥 2 + 𝑔𝑦 2
gx
10 40 0 -40 10 10 10 50 10 10 10 10 50 10 10
10 40 0 -40 10 0 0 0 0 0 10 40 0 40 10
10 40 0 -40 10 0 0 0 0 0 10 40 0 40 10
10 40 0 -40 10 10 10 50 10 10 10 10 50 10 10
Sobel Filters for gradient
Filter for gx Filter for gy
image
10 10 50 10 10 -1 0 1 -1 -2 -1
10 10 50 10 10 -2 0 2 0 0 0
10 10 50 10 10 -1 0 1 1 2 1
10 10 50 10 10
gy 𝑀𝑎𝑔 = 𝑔𝑥 2 + 𝑔𝑦 2
gx
10 10 50 10 10 10 10 50 10 10 10 10 50 10 10
10 160 0 -160 10 10 0 0 0 10 10 160 0 160 10
10 160 0 -160 10 10 0 0 0 10 10 160 0 160 10
10 10 50 10 10 10 10 50 10 10 10 10 50 10 10
Gradient Magnitudes
Filter for gx Filter for gy Magnitude using Sobel
-1 0 1 -1 -2 -1 10 10 50 10 10
-2 0 2 0 0 0 10 160 0 160 10
image
-1 0 1 1 2 1 10 160 0 160 10
10 10 50 10 10 50
10 10 10 10
10 10 50 10 10
10 10 50 10 10 Filter for gx Filter for gy Magnitude
10 10 50 10 10 10 10 50 10 10
-1 0 1 -1
10 40 0 40 10
0
10 40 0 40 10
1
10 10 50 10 10
Image Gradients
Image
Image Gradients
Image
gx
Image Gradients
Image
gx gy
Image Gradients
Image
Magnitude, M
gx gy
Laplacian Filter
0 1 0 -1 -1 -1
1 -4 1 -1 8 -1
0 1 0 -1 -1 -1
Detects horizontal Detects horizontal,
and vertical edges vertical and diagonal
edges
Laplacian Filter
Image Laplacian filter
10 10 100 10 10 0 -1 0
10 10 100 10 10
100 100 100 100 100 -1 4 -1
10 10 100 10 10
*
10 10 100 10 10 0 -1 0
10 10 100 10 10
10 -180 180 -180 10
100 180 0 180 100
=
10 -180 180 -180 10
10 10 100 10 10
Edge Detection Filters and Noise
Edge detectors are sensitive to noise
Image
after
Noisy image edge
detection
37
Edge Detection Filters and Noise
Edge detectors are sensitive to noise
Image
after
Noisy image threshold
ing
Image
Image after
after threshold
averaging ing
38
Edge Detection Filters and Noise
Edge detectors are sensitive to noise
Image
after
Noisy image edge
detection
Image
Image after
after edge
averaging detection
Apply edge detection after image smoothing
39
Gaussian Filter (Smoothing filter)
• Gaussian filter with mean 0 and standard deviation, σ is
3×3 filter with σ = 1 5×5 filter with σ = 1 5×5 filter with σ = 2
1 2 1 1 4 7 4 1 1 1 1 1 1
4 20 33 20 4 1 2 2 2 1
(1/16) 2 4 2 (1/330) 7 33 54 33 7 (1/34) 1 2 2 2 1
4 20 33 20 4 1 2 2 2 1
1 2 1
1 4 7 4 1 1 1 1 1 1
• Blurring increases with the increase in σ
Gaussian Filter (Smoothing filter)
Original Image
100 100 100 100 100 100 100
100 100 100 100 100 100 100
100 100 100 100 100 100 100
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0 Filtered Image
100 100 100 100 100 100 100
3×3 filter with σ = 1
100 100 100
1 2 1
100 75 100
(1/16) 2 4 2 0 25 0
0 0 0
1 2 1
0 0
0 0 0 0 0 0 0
Gaussian Filter (Smoothing filter)
Filtered Image for σ = 1
100 100 100 100 100 100 100
5×5 filter with σ = 1
100 100 100 100 100 100 100
1 4 7 4 1 100 100 70 70 70 100 100
4 20 33 20 4 0 0 30 30 30 0 0
(1/330)
7 33 54 33 7 0 0 10 10 10 0 0
4 20 33 20 4 0 0 0 0 0 0 0
1 4 7 4 1 0 0 0 0 0 0 0
5×5 filter with σ = 2 Filtered Image for σ = 2
1 1 1 1 1 100 100 100 100 100 100 100
100 100 100 100 100 100 100
(1/34) 1 2 2 2 1
1 2 2 2 1 100 100 60 60 60 100 100
1 2 2 2 1 0 0 40 40 40 0 0
1 1 1 1 1 0 0 10 10 10 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Difference of Gaussian
Filtered Image for σ = 1 • Subtract filtered image with
high sigma from filtered image
100 100 100 100 100 100 100
with low sigma
100 100 100 100 100 100 100 • Called difference of Gaussian
100 100 70 70 70 100 100
0 0 30 30 30 0 0 Difference of Gaussian (DoG)
(Absolute Difference)
0 0 10 10 10 0 0
0 0 0 0 0 0 0 100 100 100 100 100 100 100
0 0 0 0 0 0 0 100 100 100 100 100 100 100
100 100 10 10 10 100 100
Filtered Image for σ = 2
0 0 10 10 10 0 0
100 100 100 100 100 100 100
0 0 0 0 0 0 0
100 100 100 100 100 100 100
0 0 0 0 0 0 0
100 100 60 60 60 100 100
0 0 0 0 0 0 0
0 0 40 40 40 0 0
0 0 10 10 10 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Laplacian of Gaussian
• Apply Gaussian filter to reduce noise in the image
• Apply Laplacian filter on the image to detect edges
Image Gaussian filter Laplacian filter
100 100 100 100 100 1 2 1 0 -1
0
100 100 100 100 100
(1/16) 2 4 2 -1 4
-1
10 10 10 10 10
1 2 1 0
10 10 20 10 10 -1 0
10 10 10 10 10
Laplacian of Gaussian
Gaussian filtered image
10 10 100 10 10 10 10 100 10 10
10 77.5 10 10 10
100 33.5 100 100 100
10 11.25 10 10 10
10 10 100 10 10 10 10 100 10 10