Spatial Filtering
Spatial Filtering
output image
Spatial Filtering (cont’d)
output image
Spatial Filtering – Neighborhood (or Mask)
• Typically, the neighborhood is rectangular and its size
is much smaller than that of f(x,y)
- e.g., 3x3 or 5x5
Spatial filtering - Operation
• Manipulate the pixel values, e.g.,
z’5 = 5z1 -3z2+z3-z4-2z5-3z6+z8-z9-9z7
z’5 = max(z1,z2,z3,z4,z5,z6,z7,z8,z9)
output image
Linear vs Non-Linear filters
• A filter is called linear when its output is a linear combination
of the inputs, e.g.,
output image
Linear vs Non-Linear filters (cont’d)
z’5 = max(z1,z2,z3,z4,z5,z6,z7,z8,z9)
output image
Correlation (linear operator)
• The output of correlation is a weighted sum of input pixels.
Need to
define mask
weights!
output image
Correlation (cont’d)
w(i,j)
g(i,j)
Filtered
Image
f(i,j)
Filtered image is
generated by moving
the center of the mask
at every location of
the input image.
K /2 K /2
g (i, j ) w(i, j ) f (i, j )
s K /2 t K /2
w( s, t ) f (i s, j t )
Handling Pixels Close to Boundaries
0 0 0 ……………………….0
Correlation (cont’d)
K /2 K /2
g (i, j ) w(i, j ) f (i, j )
s K /2 t K /2
w( s, t ) f (i s, j t )
Correlation:
Convolution:
Filter Categories
• Averaging
• Gaussian
• Median filtering (non-linear)
Smoothing Filters: Averaging
Smoothing Filters: Averaging (cont’d)
• Mask size determines degree of smoothing (i.e., loss of detail).
15x15 25x25
Smoothing Filters: Averaging (cont’d)
Example: extract largest, brightest objects
σ=3
σ = 1.4
Smoothing filters: Gaussian (cont’d)
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)
• Replace each pixel by the median in a neighborhood
around the pixel.
• The size of the neighborhood controls the amount of
smoothing.
Common Sharpening Filters
• Unsharp masking
• High Boost filter
• Gradient (1st derivative)
• Laplacian (2nd derivative)
Sharpening Filters: Unsharp Masking
• Obtain a sharp image by subtracting a lowpass filtered
(i.e., smoothed) image from the original image:
- =
(with contrast
enhancement)
Sharpening Filters: High Boost
• Image sharpening emphasizes edges but details are lost.
• Idea: amplify input image, then subtract a lowpass image.
(A-1) + =
Sharpening Filters: High Boost (cont’d)
• If A=1, the result is unsharp masking.
• If A>1, part of the original image is added back to the high
pass filtered image.
High boost
One way to
implement high boost
filtering is using the
masks below
Sharpening Filters: High Boost (cont’d)
A=1.4 A=1.9
Sharpening Filters: Derivatives
• The derivative of an image results in a sharpened image.
(approximation)
f f
or | || |
x y
Gradient (cont’d)
Δx
Gradient Computation (cont’d)
f(x3,y3)-f(x3,y2)
sensitive to horizontal edges
y3-y2
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)
Implement Gradient Using Masks (cont’d)
Prewitt
Sobel
Example: Gradient Magnitude Image
Gradient Magnitude
f
x
f
y
(isotropic)
Laplacian
The Laplacian (2nd derivative) is defined as:
(dot product)
Approximate
2nd derivatives:
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
Example: Laplacian vs Gradient
Laplacian Sobel