lecture04
lecture04
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)
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
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
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
y y
18
Spatial Filtering: Basics
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
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
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
31
Smoothing Filter: Example
32
Smoothing Filter: Example
33
Smoothing Filter: Example
34
Original image
Smooth by 3x3
Size: 500x500 box filter
Smooth by
Smooth by
35x35 box filter
15x15 box filter
36
Sharpening Spatial Filters
Highlight edges
A B
1st Derivative
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
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
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
0 1 0
Laplacian Filter
Laplacian Filter
Applying the Laplacian to an image we get a
new image that highlights edges and other
discontinuities
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
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
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
0 -1 0
-1 5 -1
0 -1 0
Unsharp Masking
Use of first derivatives for image
enhancement: The Gradient
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
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
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
Median = ? 20
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
71