ImageProcessing5 SpatialFiltering1
ImageProcessing5 SpatialFiltering1
Image Enhancement
(Spatial Filtering 1)
• Example: sharpening
5
of
45
What Point Operations Can’t Do
- Neighbourhoods are
mostly a rectangle
(x, y)
around a central pixel. Neighbourhood
y Image f (x, y)
7
of
45
Filter Parameters
Many possible filter parameters (size,
weights, function, etc)
• Filter size (size of neighbourhood):
3x3, 5x5, 7x7, …,21x21,..
• Filter shape: not necessarily square. Can be
rectangle, circle, etc
• Filter weights: May apply unequal weighting
to different pixels
• Filters function: can be linear (a weighted
summation) or nonlinear
8
of
45
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
9
of
Simple Neighbourhood Operations
45 Example
y y
10
of
45
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
w(s, t ) f ( x s, y t )
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
g ( x, y )
s at b
99 106 98
95 90 85
* 1/
1/
9
1/
1/
9
1/
1/
9
9 9 9
1/ 100
104
9
1/ 108
9
1/
9
Original Image Filter
Simple 3*3 1/ 1/ 1/
3*3 Smoothing Pixels
999 106
9 989
Neighbourhood 195
/9 190
/9 185
/9
Filter
e = 1/9*106 +
1/ *104 + 1/ *100 + 1/ *108 +
9 9 9
1/ *99 + 1/ *98 +
9 9
y Image f (x, y) 1/ *95 + 1/ *90 + 1/ *85
9 9 9
= 98.3333
The above is repeated for every pixel in the
original image to generate the smoothed image
14
of
45
Image Smoothing Example
The image at the top left
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
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
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
15
Image Smoothing Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
16
Image Smoothing Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
17
Image Smoothing Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
18
Image Smoothing Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
19
Image Smoothing Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
20
Image Smoothing Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
21
Image Smoothing Example
22
of
45
Weighted Smoothing Filters
More effective smoothing filters can be
generated by allowing different pixels in the
neighbourhood different weights in the
averaging function
1/ 2/ 1/
– Pixels closer to the 16 16 16
central pixel are more
2/ 4/ 2/
important 16 16 16
– Often referred to as a 1/ 2/ 1/
weighted averaging 16 16 16
Weighted
averaging filter
23
of
45
Another Smoothing Example
By smoothing the original image we get rid
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
27
Mean Filters: Effect of Filter Size
28
of
Averaging Filter Vs. Median Filter
45 Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
29
Example
Averaging Filter Vs. Median Filter
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
30
Example
Averaging Filter Vs. Median Filter
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
31
Example
Averaging Filter Vs. Median Filter
32
of
Simple Neighbourhood Operations
45 Example
x
123 127 128 119 115 130
y
33
of
45
Strange Things Happen At The Edges!
e e e
y Image f (x, y)
34
of
Strange Things Happen At The Edges!
45 (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 (Extend)
– Truncate the image (crop)
– Allow pixels wrap around the image
• Can cause some strange image artefacts
35
of
45
What to do at image boundaries?
Crop
36
of
45
What to do at image boundaries?
Pad
37
of
45
What to do at image boundaries?
Extend
38
of
45
What to do at image boundaries?
Wrap
39
of
Simple Neighbourhood Operations
45 Example
x
123 127 128 119 115 130
y
40
of
Strange Things Happen At The Edges!
45 (cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Filtered Image:
Zero Padding
Filtered Image:
Wrap Around Edge Pixels
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
41
Strange Things Happen At The Edges!
(cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
42
Strange Things Happen At The Edges!
(cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
of
45
43
Strange Things Happen At The Edges!
(cont…)
44
of
45
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 t eprocessed = v*e +
z*a + y*b + x*c +
d
f
e
g h
e
* u
x
v
y
w
z
w*d + u*e +
t*f + s*g + r*h
Original Image Filter
Pixels