Digital Image Processing
Lecture # 4
Spatial Filtering
1
Spatial Filtering
Spatial Filtering
4
Spatial Filtering
5
Spatial Filtering
6
Spatial Filtering
7
Spatial Filtering
Spatial Filtering: Basics
The output intensity value at (x,y) depends not only on the input
intensity value at (x,y) but also on the specified number of
neighboring intensity values around (x,y)
Spatial masks (also called window, filter, kernel, template) are used
and convolved over the entire image for local enhancement (spatial
filtering)
The size of the masks determines the number of neighboring pixels
which influence the output value at (x,y)
The values (coefficients) of the mask determine the nature and
properties of enhancing technique
11
Spatial Filtering: Basics
a b
g ( x, y ) w(s, t ) f ( x s, y t )
s a t b
m 1 n 1
where a = , b=
2 2
x 0,1, 2,...., M 1, y 0,1, 2,..., N 1
Filtering can be given in equation
form as shown above
12
Spatial Filtering: Basics
Given the 3×3 mask with coefficients: w1, w2,…, w9
The mask cover the pixels with gray levels: z1, z2,…, z9
w1 w2 w3 z1 z2 z3
w4 w5 w6 z4 z5 z6
w7 w8 w9 z7 z8 z9
9
z
z1w1 z2 w2 z3 w3 z9 w9 zi wi
i 1
z gives the output intensity value for the processed image (to be
stored in a new array) at the location of z5 in the input image
13
Spatial Filtering: Basics
Origin x
Neighbourhood
operations: Operate on a
larger neighbourhood of
pixels than point
operations (x, y)
Neighbourhood
Neighbourhoods are mostly a
rectangle around a central
pixel
y Image f (x, y)
16
Spatial Filtering: Basics
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
17
Spatial Filtering: Basics
Original Image x Enhanced Image 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
18
Spatial Filtering: Basics
Moving window (kernel)
scans the 3x3
neighborhood of every
pixel in the image
Original Image
19
Spatial Filtering: Basics
20
Spatial Filtering: Basics
21
Spatial Filtering: Basics
22
Spatial Filtering: Basics
23
Spatial Filtering: Basics
24
Spatial Filtering: Basics
Mask operation near the image border: Problem arises when
part of the mask is located outside the image plane
Discard the problem pixels (e.g.
512x512 input 510x510 output if mask Pixel replication: We
size is 3x3) normally use the gray
Zero padding: Expand the input levels of border pixels to
image by padding zeros (512x512 fill up the expanded region
original image, 514x514 padded (for 3x3 mask). For larger
image, 512x512 output) masks a border region
Zero padding is not equal to half of the mask
recommended as it creates size is mirrored on the
artificial lines or edges on the expanded region.
border
25
Spatial Filtering: Basics
Mask operation near the border: Pixel
replication
27
Smoothing Spatial Filters
Simply average all of the pixels in a neighbourhood around
a central value
1/ 1/ 1/
9 9 9
Simple
1/ 1/ 1/
9 9 9 averaging
filter
1/ 1/ 1/
9 9 9
28
Smoothing Spatial Filters
For blurring/noise reduction
Blurring is usually used in preprocessing steps, e.g., to remove
small details from an image prior to object extraction, or to bridge
small gaps in lines or curves
Equivalent to Low-pass spatial filtering in frequency domain
because smaller (high frequency) details are removed based on
neighborhood averaging (averaging filters)
29
Smoothing Spatial Filters
Origin x
104 100 108 1/ 1/ 1/
9 9 9
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
30
Smoothing Filter: Example
original 3x3 average
31
Smoothing Filter: Example
original 3x3 average
32
Smoothing Filter: Example
original 3x3 average
33
Smoothing Filter: Example
original 3x3 average
34
Original image
Smooth by 3x3
Size: 500x500 box filter
Smooth by 5x5 Smooth by 9x9
box filter box filter
Smooth by
Smooth by
35x35 box filter
15x15 box filter
Notice how detail begins to disappear
35
Smoothing Spatial Filters
Consider the
output pixel is
positioned at
the center
Box Filter all Weighted Average give
coefficients are more (less) weight to near
equal (away from) the output
location
36
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
• Let’s consider a simple 1 dimensional
example
Spatial Differentiation
A B
1st Derivative
The 1st derivative of a function is given by:
f
f ( x 1) f ( x)
x
Its just the difference between subsequent
values and measures the rate of change of
the function
1st Derivative
Image Strip
8
7
6
5
4
3
2
1
0
5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7
-1 -1 -1 -1 -1 0 0 6 -6 01st 0Derivative
0 1 2 -2 -1 0 0 0 7 0 0 0
8
6
4
2
0
-2
-4
-6
-8
2nd Derivative
The 2nd derivative of a function is given by:
Simply takes into account the values both before and after the current value
f
2
f ( x 1) f ( x 1) 2 f ( x)
x
2
2nd Derivative
Image Strip
8
7
6
5
4
3
2
1
0
5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7
-1 0 0 0 0 1 0 6 -12 6 0 0 1 1 -4 1 1 0 0 7 -7 0 0
2nd Derivative
10
-5
-10
-15
2nd Derivative for Image Enhancement
The 2nd derivative is more useful for image enhancement
than the 1st derivative - Stronger response to fine detail
We will come back to the 1st order derivative later on
The first sharpening filter we will look at is the Laplacian
Laplacian Filter
The Laplacian is defined as follows:
f f 2 2
f 2 2
2
x y
2 f
f ( x 1, y) f ( x 1, y) 2 f ( x, y)
x 2
2 f
f ( x, y 1) f ( x, y 1) 2 f ( x, y )
y 2
Laplacian Filter
So, the Laplacian can be given as follows:
2 f [ f ( x 1, y) f ( x 1, y)
f ( x, y 1) f ( x, y 1)]
4 f ( x, y )
Can we implement it using a 0 1 0
filter/ mask?
1 -4 1
0 1 0
Laplacian Filter
Laplacian Filter
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
Laplacian Image Enhancement
The result of a Laplacian filtering is not an
enhanced image
To generate the final enhanced image
Laplacian
Filtered Image
Scaled for Display
f ( x, y ) f , w5 0
2
g ( x, y )
f ( x, y ) 2 f , w5 0
Laplacian Image Enhancement
- =
Original Laplacian Sharpened
Image Filtered Image Image
In the final sharpened image edges and fine
detail are much more obvious
Laplacian Image Enhancement
Simplified Image Enhancement
• The entire enhancement can be combined
into a single filtering operation
g ( x, y) f ( x, y) f
2
f ( x, y ) [ f ( x 1, y ) f ( x 1, y )
f ( x, y 1) f ( x, y 1)
4 f ( x, y )]
Simplified Image Enhancement
• The entire enhancement can be combined
into a single filtering operation
g ( x, y) f ( x, y) f 2
5 f ( x, y ) f ( x 1, y ) f ( x 1, y )
0 -1 0
f ( x, y 1) f ( x, y 1)
-1 5 -1
0 -1 0
Simplified Image Enhancement
• 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
Unsharp Masking
Use of first derivatives for image
enhancement: The Gradient
• The gradient of a function f(x,y) is defined as
f
Gx x
f f
G y
y
Gradient Operators
• Most common differentiation operator is the
gradient vector.
f ( x , y )
x G x
f ( x, y )
f ( x , y ) G y
y
Magnitude:
f ( x, y ) G x G y
2
2 1/ 2
Gx Gx
Direction:
Gy
1
f ( x , y ) tan
Gx
59
Gradient Operators
Sobel Operator
-1 -2 -1 -1 0 1
0 0 0 -2 0 2
1 2 1 -1 0 1
Extract horizontal edges Extract vertical edges
Emphasize more the current point
(y direction)
f ( z7 2 z8 z9 ) ( z1 2 z2 z3 )
( z3 2 z6 z9 ) ( z1 2 z4 z7 )
Emphasize more the current point (x
direction) Pixel Arrangement
Sobel Operator: Example
An image of a
contact lens
which is
enhanced in
order to make
defects more
obvious
Sobel filters are typically used for edge
detection
Order-Statistic Filtering
Output is based on order of gray levels in the masked area
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
62
Median Filter
• For an image, mask symmetric: 3x3, 5x5, etc.
Sorted: 0,0,1,1,1,2,2,2,4
Input Output
1 2 0 1 3
2 2 4 2 2 1
1 0 1 0 1
1 2 1 0 2
2 5 3 1 2
63
Median Filtering
Sort the values
Determine the median
Median = ? 20
Particularly effective when
The noise pattern consists of strong
impulse noise ( salt-and-pepper)
64
Salt and Pepper Noise
Median Filtering
66
Min/Max Filtering
Combining Spatial Enhancement
Methods
Successful image enhancement is
typically not achieved using a single
operation
Rather we combine a range of
techniques in order to achieve a final
result
This example will focus on enhancing
the bone scan
Readings from Book (3rd Edn.)
• 3.3 Histogram
• 3.4 Filtering
• 3.5 Smoothing Filters
• Sharpening Filters (Chapter – 3)
• Reading Assignment
• High Boost Filtering (Chap –
3.6.3)
Acknowledgements
Statistical Pattern Recognition: A Review – A.K Jain et al., PAMI (22) 2000
Pattern Recognition and Analysis Course – A.K. Jain, MSU
Material in these slides has been taken from, the following resources
Pattern Classification” by Duda et al., John Wiley & Sons.
Digital Image Processing”, Rafael C. Gonzalez & Richard E. Woods, Addison-Wesley, 2002
Machine Vision: Automated Visual Inspection and Robot Vision”, David Vernon, Prentice Hall,
1991
www.eu.aibo.com/
Advances in Human Computer Interaction, Shane Pinder, InTech, Austria, October 2008
71