0% found this document useful (0 votes)
13 views

Lecture 1.2.4-Spatial Filtering

Its about the spatial filtering used to enhance an image and its quality moreover reduce noise.

Uploaded by

nandinisharda06
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture 1.2.4-Spatial Filtering

Its about the spatial filtering used to enhance an image and its quality moreover reduce noise.

Uploaded by

nandinisharda06
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

University Institute of

Engineering
DEPARTMENT OF COMPUTER
SCIENCE & ENGINEERING
Bachelor of Engineering (Computer Science &
Engineering)
Subject Name : Computer Vision
Subject Code: 21CSH/ITH-422
Topic: Lecture-1.2.4
Lecture- Spatial Filtering
By :Er. Abha Agrawal
DISCOVER . LEARN . EMPOWER
1
Syllabus
• UNIT-1: Introduction to Image Processing

• CHAPTER-1: Introduction to Image Processing:


• Digital Image representation, Sampling & Quantization, Steps in image Processing,
Image acquisition, colour image representation.

• CHAPTER-2: Image Transformation, Filtering & Restoration:


• Intensity transforms functions, histogram processing, Spatial filtering, fourier transforms
and its properties, frequency domain filters, Image Noise and Restorations.

2
CONTENTS
• Neighbourhood operations
• What is spatial filtering?
• Smoothing operations
• What happens at the edges?
• Correlation and convolution
• Sharpening filters
• Combining filtering techniques

3
Neighbourhood Operations
Neighbourhood operations simply operate on a larger neighbourhood of pixels than
point operations
Origin x
Neighbourhoods are
mostly a rectangle
around a central pixel
Any size rectangle
and any shape filter
are possible (x, y)
Neighbourhood

y Image f (x, y)
Simple Neighbourhood Operations
Some simple neighbourhood operations include:
• Min: Set the pixel value to the minimum in the neighbourhood
• Max: Set the pixel value to the maximum in the neighbourhood
• Median: The median value of a set of numbers is the midpoint value in that set
(e.g. from the set [1, 7, 15, 18, 24] 15 is the median). Sometimes the median
works better than the average
Simple Neighbourhood Operations Example

Original Image Enhanced Image x


x
123 127 128 119 115 130

140 145 148 153 167 172

133 154 183 192 194 191

194 199 207 210 198 195

164 170 175 162 173 151

y
y
The Spatial Filtering Process
Origin x
a b c r s t
d
g
e
h
f
i
* u
x
v
y
w
z
Original Image Filter
Simple 3*3 Pixels
e 3*3 Filter
Neighbourhood
eprocessed = v*e +
r*a + s*b + t*c +
u*d + w*f +
y Image f (x, y) x*g + y*h + z*i
The above is repeated for every pixel in the
original image to generate the filtered image
Smoothing Spatial Filters
One of the simplest spatial filtering operations we can perform is a smoothing
operation
• Simply average all of the pixels in a neighbourhood around a central value
• Especially useful
in removing noise
from images
• Also useful for 1
/9 1
/9 1
/9
highlighting gross Simple
detail 1
/9 /9
1
/9
1
averaging
filter
1
/9 /9
1 1
/9
Smoothing Spatial Filtering
Origin x
104 100 108 1
/9 1
/9 1
/9

* /9 /9 /9
1 1 1
99 106 98

95 90 85 1
/9 1
/9 1
/9
1
/9 100
104 1
/9 108
1
/9
Original Image Filter
Simple 3*3 /9 106
1
99 1
/9 198
/9
3*3 Smoothing Pixels
Neighbourhood /9 190
1
95 /9 185
/9 Filter
e = 1/9*106 +
1
/9*104 + 1/9*100 + 1/9*108 +
1
/9*99 + 1/9*98 +
y Image f (x, y) 1
/9*95 + 1/9*90 + 1/9*85
= 98.3333
The above is repeated for every pixel in the original image to
generate the smoothed image.
Image Smoothing Example
The image at the top left
is an original image of
size 500*500 pixels
The subsequent images
show the image after
filtering with an averaging
filter of increasing sizes
• 3, 5, 9, 15 and 35
Notice how detail begins
to disappear
Weighted Smoothing Filters
More effective smoothing filters can be generated by allowing different pixels in the
neighbourhood different weights in the averaging function
• Pixels closer to the
central pixel are more
important 1
/16 2/16 1/16
• Often referred to as a
weighted averaging 2
/ 4
/ 2
/
16 16 16

1
/16 /16
2
/16
1

Weighted
averaging filter
Another Smoothing Example
By smoothing the original image we get rid of lots of the finer
detail which leaves only the gross features for thresholding
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Original Image Smoothed Image Thresholded Image


Averaging Filter Vs. Median Filter Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Original Image Image After Image After


With Noise Averaging Filter Median Filter
Filtering is often used to remove noise from images
Sometimes a median filter works better than an averaging filter
Spatial smoothing and image approximation
Spatial smoothing may be viewed as a process for estimating the
value of a pixel from its neighbours.
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

What is the value that “best” approximates the intensity of a


given pixel given the intensities of its neighbours?

We have to define “best” by establishing a criterion.


Spatial smoothing and image approximation (cont...)
Images taken from Gonzalez & Woods, Digital Image Processing (2002) Example x[n] 1 1 1 1 1 2 2 2 2 2

edge
Impulse x[n] 1 3 1 1 1 2 3 2 2 3
noise

Median
(N=3) x[n] - 1 1 1 1 2 2 2 2 -

Average x[n] - 1.7 1.7 1 1.3 2 2.3 2.3 2.2 -


(N=3)
The edge is smoothed
Strange Things Happen At The Edges!
At the edges of an image we are missing
pixels to form a neighbourhood
Origin x
e e

e e e
y Image f (x, y)
Strange Things Happen At The Edges! (cont…)

There are a few approaches to dealing with missing edge pixels:


• Omit missing pixels
• Only works with some filters
• Can add extra code and slow down processing
• Pad the image
• Typically with either all white or all black pixels
• Replicate border pixels
• Truncate the image
• Allow pixels wrap around the image
• Can cause some strange image artefacts
Correlation & Convolution
The filtering we have been talking about so far is referred to as
correlation with the filter itself referred to as the correlation
kernel
Convolution is a similar operation, with just one subtle difference

a b c r s eprocessed = v*e +
t
d e
f g h
e
* u v
x y z
w
For symmetric filters it makes no difference.
z*a + y*b + x*c +
w*d + u*e +
t*f + s*g + r*h
Original Image Filter
Pixels
Sharpening Spatial Filters
Previously we have looked at smoothing filters which remove fine detail
Sharpening spatial filters seek to highlight fine detail
• Remove blurring from images
• Highlight edges
Sharpening filters are based on spatial differentiation
Spatial Differentiation
Images taken from Gonzalez & Woods, Digital Image Processing (2002) Differentiation measures the rate of change of a function
Let’s consider a simple 1 dimensional example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

A
B
Spatial Differentiation
Derivative Filters Requirements
First derivative filter output
• Zero at constant intensities
• Non zero at the onset of a step or ramp
• Non zero along ramps

•Second derivative filter output


• Zero at constant intensities
• Non zero at the onset and end of a step or ramp
• Zero along ramps of constant slope
23
1st Derivative (cont.)
• The gradient of an image:

The gradient points in the direction of most rapid increase


in intensity.
Gradient direction

The edge strength is given by the gradient magnitude

Source: Steve Seitz


25
26
27
28
Using Second Derivatives For Image Enhancement
Edges in images are often ramp-like transitions
• 1st derivative is constant and produces thick edges
• 2nd derivative zero crosses the edge (double response at the onset
and end with opposite signs)

A common sharpening filter is the Laplacian


• Isotropic
• One of the simplest sharpening filters
• We will look at a digital implementation
30
31
The Laplacian (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Applying the Laplacian to an image we get a new image that highlights edges and
other discontinuities

Original Laplacian Laplacian


Image Filtered Image Filtered Image
Scaled for Display
33
Laplacian Image Enhancement
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

- =
Original Laplacian Sharpened
Image Filtered Image Image

In the final sharpened image edges and fine detail are much more
obvious
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Laplacian Image Enhancement
36
Simplified Image Enhancement (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

This gives us a new filter which does the whole job for us in one step

0 -1 0

-1 5 -1

0 -1 0
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Simplified Image Enhancement (cont…)
Variants On The Simple Laplacian
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

There are lots of slightly different versions of the Laplacian that can be used:

0 1 0 1 1 1
Simple Variant of
1 -4 1 1 -8 1
Laplacian Laplacian
0 1 0 1 1 1

-1 -1 -1

-1 9 -1

-1 -1 -1
Unsharp masking
Images taken from Gonzalez & Woods, Digital Image Processing (2002) Used by the printing industry
Subtracts an unsharped (smooth) image from the original image
f(x,y).
•Blur the image
b(x,y)=Blur{f(x,y)}
•Subtract the blurred image from the original (the result is
called the mask)
gmask(x,y)=f(x,y)-b(x,y)
•Add the mask to the original
g(x,y)=f(x,y)+k gmask(x,y) with k non negative
Unsharp masking (cont...)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Sharping mechanism

When k>1 the process is


referred to as highboost filtering
Unsharp masking (cont...)
Original image
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Blurred image

Mask

Unsharp masking

Highboost filtering (k=4.5)


43
44
45
Sobel Operators
Based on the previous equations we can derive the Sobel Operators

-1 -2 -1 -1 0 1

0 0 0 -2 0 2
To filter an image it is filtered using both operators the results of
which are added 1 together
2 1 -1 0 1
Sobel Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
An image of a
contact lens which
is enhanced in
order to make
defects (at four
and five o’clock in
the image) more
Sobel filters are typically used for edge detection obvious
1st & 2nd Derivatives
Comparing the 1st and 2nd derivatives we can conclude the following:
• 1st order derivatives generally produce thicker edges (if thresholded at ramp
edges)
• 2nd order derivatives have a stronger response to fine detail e.g. thin lines
• 1st order derivatives have stronger response to grey level step
• 2nd order derivatives produce a double response at step changes in grey level
(which helps in detecting zero crossings)
Combining Spatial Enhancement Methods
Successful image enhancement is
typically not achieved using a single
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

operation
Rather we combine a range of techniques
in order to achieve a final result
This example will focus on enhancing the
bone scan to the right
Combining Spatial Enhancement Methods (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

(a)
Laplacian filter of
bone scan (a)
(b)
Sharpened version of
bone scan achieved (c)
by subtracting (a)
and (b) Sobel filter of bone
scan (a) (d)
Combining Spatial Enhancement Methods (cont…)
Result of applying a (h)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
power-law trans. to
Sharpened image (g)
which is sum of (a)
and (f) (g)
The product of (c)
and (e) which will be (f)
used as a mask
(e)

Image (d) smoothed with


a 5*5 averaging filter
Combining Spatial Enhancement Methods (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Compare the original and final images


Summary
In this lecture we have looked at the idea of spatial filtering and in particular:
• Neighbourhood operations
• The filtering process
• Smoothing filters
• Dealing with problems at image edges when using filtering
• Correlation and convolution
• Sharpening filters
• Combining filtering techniques
References
• Books and Journals
• Gonzalez and Woods: Digital Image Processing ISDN 0-201-600- 781, Addison Wesley 1992.
• Forsyth and Ponce: Computer Vision A Modern Approach Pearson Education Latest Edition.

• Video Link-
• https://www.youtube.com/watch?v=0uhs117VZj4

• Web Link-
• https://www.geeksforgeeks.org/spatial-filtering-and-its-types/

54
55

You might also like