07 Spatial Filtering - PDF
07 Spatial Filtering - PDF
Technology (NUST)
School of Electrical Engineering and Computer Science
(SEECS)
1 Khawar Khurshid
Spatial Filtering
2 Khawar Khurshid
Spatial Filtering - Basics
Origin x
Neighbourhood operations:
y
Image f (x, y)
3 Khawar Khurshid
Spatial Filtering - Basics
y y
4 Khawar Khurshid
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
e 3*3 Filter Pixels
Neighbourhood
eprocessed = v*e +
r*a + s*b + t*c +
y Image f (x, y) u*d + w*f +
x*g + y*h + z*i
The above is repeated for every pixel in the original image to generate the filtered image
5 Khawar Khurshid
Spatial Filtering - Basics
6 Khawar Khurshid
Spatial Filtering - Basics
7 Khawar Khurshid
Spatial Filtering - Basics
8 Khawar Khurshid
Spatial Filtering - Basics
9 Khawar Khurshid
Spatial Filtering - Basics
10 Khawar Khurshid
Spatial Filtering - Basics
11 Khawar Khurshid
Spatial Filtering - Basics
Mask operation near the image border
Problem arises when part of the mask is located outside the
image plane
Pixel replication:
Discard the problem pixels (e.g. 512x512 We normally use the gray levels
input 510x510 output if mask size is 3x3) of border pixels to fill up the
expanded region (for 3x3
mask). For larger masks a
border region equal to half of
Zero padding: Expand the input image by the mask size is mirrored on
padding zeros (512x512 original image, the expanded region.
514x514 padded image, 512x512 output)
Customized Processing:
Zero padding is not recommended as it
Using only those coefficients
creates artificial lines or edges on the of the mask which overlap with
border the image pixels on the borders
and ignoring the others.
12 Khawar Khurshid
Spatial Filtering - Basics
13 Khawar Khurshid
Spatial Filtering - Basics
14 Khawar Khurshid
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
15 Khawar Khurshid
Spatial Filtering - Basics
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.
16 Khawar Khurshid
Spatial Filtering - Basics
17 Khawar Khurshid
Spatial Filtering - Basics
1/ 1/ 1/
9 9 9
1/ 1/ 1/
9 9 9
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 +
y Image f (x, y) 9 9
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
19 Khawar Khurshid
Smoothing Filter
Consider the
output pixel is
positioned at
the center
20 Khawar Khurshid
Smoothing Filter - Example
21 Khawar Khurshid
Smoothing Filter - Example
22 Khawar Khurshid
Smoothing Filter - Example
23 Khawar Khurshid
Smoothing Filter - Example
24 Khawar Khurshid
Smoothing Filter - Example
Original
image Smooth by 3x3
Size: box filter
500x500
• Smoothing filters
– Implement a box filter
– Add noise yourself
26 Khawar Khurshid
MATLAB - Exercise
clear all;
II=imread('rain.jpg');
I=rgb2gray(II);
[x,y] = size(I)
I=double(I)/255;
I = imnoise(I,'salt & pepper',0.02);
w=(ones(3,3));
O=ones(x,y);
Or use the built in conv2 function
for i=2:x-2
for j=2:y-2
O(i,j)=1/9*(w(1,1)*I(i-1,j-1)+w(1,2)*I(i-1,j)+w(1,3)*I(i-1,j+1)+w(2,1)*I(i,j-1) +
w(2,2)*I(i,j)+w(2,3)*I(i,j+1)+w(3,1)*I(i+1,j-1)+w(3,2)*I(i+1,j) +w(3,3)*I(i+1,j+1));
end
end
27 Khawar Khurshid
Sharpening Filters
Highlight edges
28 Khawar Khurshid
Spatial Differentiation
A B
29 Khawar Khurshid
Spatial Differentiation
A B
30 Khawar Khurshid
Spatial Differentiation
f
f ( x 1) f ( x)
x
Its just the difference between subsequent values and
measures the rate of change of the function.
31 Khawar Khurshid
Spatial Differentiation
Image Strip
8
7
6
5
4
3
1st Derivative
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
32 -8
Khawar Khurshid
Spatial Differentiation
f
2
f ( x 1) f ( x 1) 2 f ( x)
x
2
33 Khawar Khurshid
Spatial Differentiation
Image Strip
8
7
6
5
4
3
2nd Derivative
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
34 -15
Khawar Khurshid
2nd Derivative – Image Enhancement
35 Khawar Khurshid
Laplacian Filter
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
36 Khawar Khurshid
Laplacian Filter
2 f [ f ( x 1, y) f ( x 1, y )
f ( x, y 1) f ( x, y 1)]
4 f ( x, y)
0 1 0
Can we implement
it using a mask? 1 -4 1
0 1 0
37 Khawar Khurshid
Laplacian Filter
38 Khawar Khurshid
Laplacian Filter – Image Enhancement
f ( x, y ) 2 f , w5 0
g ( x, y )
f ( x, y ) 2 f , w5 0
39 Khawar Khurshid
Laplacian Filter – Image Enhancement
g ( x, y) f ( x, y) 2 f
f ( x, y) [ f ( x 1, y) f ( x 1, y)
f ( x, y 1) f ( x, y 1)
0 1 0
4 f ( x, y)]
1 -4 1
0 1 0
40 Khawar Khurshid
Laplacian Filter – Image Enhancement
g ( x, y) f ( x, y) f 2
5 f ( x, y) f ( x 1, y) f ( x 1, y)
f ( x, y 1) f ( x, y 1)
0 -1 0
-1 5 -1
0 -1 0
41 Khawar Khurshid
Laplacian Filter – 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
42 Khawar Khurshid
Laplacian Filter – Image Enhancement
Original Laplacian
43 Image Filtered Image
Khawar Khurshid
Laplacian Filter – Image Enhancement
+ =
Original Laplacian Sharpened
Image Filtered Image Image
44 Khawar Khurshid
Laplacian Filter – Image Enhancement
45 Khawar Khurshid
Laplacian Filter – Image Enhancement
46 Khawar Khurshid
Laplacian Filter – Image Enhancement
47 Khawar Khurshid
Gradient Operators
Simplest operator
f f
( z8 z5 ), ( z6 z5 )
x y
f ( z8 z5 ) 2 ( z6 z5 ) 2
f ( z8 z5 ) ( z6 z5 )
48 Khawar Khurshid
Gradient Operators
Prewitt operator
f ( z7 z8 z9 ) ( z1 z2 z3 )
( z3 z6 z9 ) ( z1 z4 z7 )
-1 -1 -1 -1 0 1
f f
0 0 0 -1 0 1
y 1 1 1
x -1 0 1
Extract horizontal edges Extract vertical edges
49 Khawar Khurshid
Gradient Operators
Sobel operator
-1 0 1
f -1 -2 -1
f
0 0 0 -2 0 2
y x -1 0 1
1 2 1
Extract horizontal edges Extract vertical edges
f ( z7 2 z8 z9 ) ( z1 2 z2 z3 )
( z3 2 z6 z9 ) ( z1 2 z4 z7 )
An image of a
contact lens which
is enhanced in
order to make
defects more
obvious
51 Khawar Khurshid
Combining Spatial Enhancement Methods
52 Khawar Khurshid
Combining Spatial Enhancement Methods
(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)
53 Khawar Khurshid
Combining Spatial Enhancement Methods
Original Enhanced
55 Khawar Khurshid
Noise Models
56 Khawar Khurshid
Noise Models
57 Khawar Khurshid
Noise Models
58 Khawar Khurshid
Noise Filters
Frequency Domain
Band Reject/Notch Filtering
59 Khawar Khurshid
Noise Filters
Order filters
- Minimum
- Maximum
- Median
- Mid-Point
- Alpha-Trimmed
Mean filters
- Arithmetic Mean
- Geometric Mean
- Contra-Harmonic Mean
60 Khawar Khurshid
Order Filters
61 Khawar Khurshid
Order Filters
• Median filter
– Select the middle pixel value from the ordered set.
– Used to remove salt-and-pepper noise.
• Maximum filter
– Select the highest pixel value from the ordered set.
– Remove pepper-type noise.
• Minimum filter
– Select the lowest pixel value from the ordered set.
– Remove salt-type noise.
• As the size of the window gets bigger, the more information
loss occurs.
– With windows larger than about 5x5, the image acquires an
artificial, “painted”, effect.
62 Khawar Khurshid
Order Filters
Minimum Filter
Maximum Filter
67 Khawar Khurshid
Median Filtering
Median = ? 20
69 Khawar Khurshid
Mean-Median Filter - comparison
70 Khawar Khurshid
Mean-Median Filter - comparison
Noisy Original
71 Khawar Khurshid
Mean-Median Filter - comparison
Noisy Noisy
72 Khawar Khurshid
Mean-Median Filter - comparison
3x3-blur x 1 3x3-median x 1
73 Khawar Khurshid
Mean-Median Filter - comparison
3x3-blur x 2 3x3-median x 2
74 Khawar Khurshid
Mean-Median Filter - comparison
3x3-blur x 5 3x3-median x 5
75 Khawar Khurshid
Mean-Median Filter - comparison
3x3-blur x 10 3x3-median x 10
76 Khawar Khurshid
Order Filters
Midpoint filter
77 Khawar Khurshid
Order Filters
If T = 0, mean filter.
If T = (N2 – 1) / 2, median filter.
80 Khawar Khurshid
Order Filters
81 Khawar Khurshid
Order Filters
82 Khawar Khurshid
Mean Filters
83 Khawar Khurshid
Mean Filters
1
Arithmetic Mean =
N2
d ( r, c )
( r ,c )w
84 Khawar Khurshid
Mean Filters
85 Khawar Khurshid
Mean Filters
86 Khawar Khurshid
Mean Filters
g (r , c)
1
Geometric Mean = N2
( r ,c )w
87 Khawar Khurshid
Mean Filters
89 Khawar Khurshid
Mean Filters
Contra-Harmonic Mean =
g (r , c)
( r ,c )w
R
90 Khawar Khurshid
Mean Filters
91 Khawar Khurshid
Mean Filters
92 Khawar Khurshid
Mean Filters
93 Khawar Khurshid
Mean Filters
94 Khawar Khurshid
Mean Filters
95 Khawar Khurshid
Mean Filters
96 Khawar Khurshid
Mean Filters
97 Khawar Khurshid
End
Spatial Filtering
98 Khawar Khurshid