Spatial Filtering
Spatial Filtering
output image
Spatial Filtering (cont’d)
• Spatial filters are defined by:
(1) Neighborhood (i.e., which pixels to process)
(2) Operation (i.e., how to process the pixels)
output image
Spatial Filtering – Neighborhood
output image
Linear vs Non-Linear filters
• Depending on the operator used, a filter can be of two
types: linear or non-linear, for example:
z’5 = 5z1 -3z2+z3-z4-2z5-3z6+z8-z9-9z7 linear
z’5 = max(z1,z2,z3,z4,z5,z6,z7,z8,z9) non-linear
output image
Linear Operators
W1 W2 W3
output image
W4 W5 W6
W7 W8 W9
Correlation (cont’d)
w(s,t)
g(i,j)
Output
Image
f(i,j)
The output image is
generated by moving the
center of the mask at
each location in the input
image.
K /2 K /2
i,j=0,1,…,N-1
g (i, j ) w( s, t ) f (i, j )
s K /2 t K /2
w( s, t ) f (i s, j t )
Handling Locations Close to Boundaries
• Pad with zeroes.
0 0 0 ……………………….0
0 0 0 ……………………….0
Correlation (cont’d)
K /2 K /2
g (i, j ) w( s, t ) f (i, j )
s K /2 t K /2
w( s, t ) f (i s, j t )
i,j=0,1,…,N-1
• If w(i, j) is symmetric (i.e., w(i, j)=w(-i,-j)), then
convolution is equivalent to correlation!
Example
Correlation:
Convolution:
Filter Categories
x 1/(sum of weights)
Smoothing filters – Example
• Averaging (linear)
• Gaussian (linear)
15x15 25x25
Smoothing Filters: Averaging (cont’d)
Example: extract largest, brightest objects
σ=3
σ = 1.4
Smoothing filters: Gaussian (cont’d)
Example
Averaging vs Gaussian Smoothing
Averaging
Gaussian
Smoothing Filters: Median Filtering
(non-linear)
• Very effective for removing “salt and pepper” noise (i.e.,
random occurrences of black and white pixels).
median
averaging filtering
Smoothing Filters: Median Filtering (cont’d)
• Idea: replace each pixel by the median in a neighborhood
around the pixel.
• The size of the neighborhood controls the amount of
smoothing.
Sharpening Filters
g mask ( x, y ) f ( x, y ) f LP ( x, y )
f ( x, y ) f LP ( x, y ) g mask ( x, y )
- =
Note: contrast enhancement
has been applied for
better visualization
Sharpening Filters: Unsharp Masking (cont’d)
• Image sharpening emphasizes edges but other details are lost.
• Idea: add gmask back to the original image.
g ( x, y ) f ( x, y ) g mask ( x, y )
f ( x, y ) g mask ( x, y ) g ( x, y )
+ =
High Boost Filtering
g ( x, y ) f ( x, y ) kg mask ( x, y ), k 0
f ( x, y ) g mask ( x, y )
+k
High Boost Filtering - Example
original
Gaussian
smoothed
2D Example: gmask
Unsharp
Masking
(k=1)
Highboost
Filtering
(k>1)
Sharpening Filters: Derivatives
• The derivative of an image results in a sharpened image.
Δx
Convention:
x
232 177 82 7
y
241 18 152 140
156 221 67 3
100 45 1 103
Example: Image Gradient
f
x
f
y
Implement Gradient Using Masks
(x+1/2,y)
good approximation
at (x+1/2,y) (x,y+1/2)
*
*
good approximation
at (x,y+1/2)
Derivatives are
*
computed at the
same location!
Prewitt
Sobel
Example: Visualize Gradient Magnitude
• The gradient magnitude can be visualized
as an image by mapping the values to [0, 255]
f Gradient Magnitude
x
f
y
(isotropic, i.e., detects
edges in all directions)
Second Derivative – 1D case
f ( x) f ( x 1) f ( x)
f ( x) f ( x 1) f ( x) f ( x 1) f ( x 1) 2 f ( x)
max
1st derivative
(2) Zero-crossings
of the second derivative (i.e., where
2nd derivative
the second derivative changes sign).
Second Derivative – 1D case (cont’d)
Example:
Second Derivative – 2D case: Laplacian
The Laplacian is defined as:
(dot product)
Approximate
2nd partial
derivatives:
Second Derivative – 2D case: Laplacian
(cont’d)
Laplacian Mask
5 5 5 Edges can
5 -10 -5 -5
be found
5 -10
by detecting
10
the zero-
-10
crossings
Second Derivative – 2D case: Laplacian
(cont’d)
• Other realizations of the Laplacian mask found in practice.
How to best visualize the results
of the Laplacian?
• Typically, we add/subtract the results of the Laplacian
to/from the original image for better visualization!
g ( x, y ) f ( x, y ) c 2 f ( x, y )
f ( x, y ) is the input image, g ( x, y ) is the sharpenend images
f ( x, y )
g ( x, y ) 2 f ( x, y )
g ( x, y ) f ( x, y ) c 2 f ( x, y )
Laplacian vs Gradient
Laplacian Sobel