05 Spatial Filtering
05 Spatial Filtering
Fundamentals of
Image Processing
Erkut Erdem
Dept. of Computer Engineering
Hacettepe University
Spatial Filtering
Image Filtering
• Image filtering: computes a function of a local neighborhood at
each pixel position
• Called “Local operator,” “Neighborhood operator,” or
“Window operator”
• f: image è image
• Uses:
– Enhance images
• Noise reduction, smooth, resize, increase contrast,
recolor, artistic effects, etc.
– Extract features from images
• Texture, edges, distinctive points, etc.
– Detect patterns
• Template matching, e.g., eye template
Slide credit: D. Hoiem
Filtering
• The name “filter” is borrowed from frequency domain
processing (next week’s topic)
• Accept or reject certain frequency components
• Fourier (1807):
Periodic functions
could be represented
as a weighted sum of
sines and cosines
– each sample gets its own weight (normally zero far away)
• This is all convolution is: it is a moving weighted average
– now the filter is a rectangle you slide around over a grid of numbers
• Usefulness of associativity
– often apply several filters one after another: (((a * b1) * b2) * b3)
– this is equivalent to applying one filter: a * (b1 * b2 * b3)
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 20 10
0 0 0 90 90 90 90 90 0 0 0 20 40 60 60 60 40 20
0 0 0 90 90 90 90 90 0 0 0 30 60 90 90 90 60 30
0 0 0 90 90 90 90 90 0 0 0 30 50 80 80 90 60 30
0 0 0 90 0 90 90 90 0 0 0 30 50 80 80 90 60 30
0 0 0 90 90 90 90 90 0 0 0 20 30 50 50 60 40 20
0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 30 20 10
0 0 90 0 0 0 0 0 0 0 10 10 10 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Slide credit: C.
Dyer
Correlation filtering
Say the averaging window size is 2k+1 x 2k+1:
Non-uniform weights
Slide credit: K. Grauman
Correlation filtering
scanline
Norm. corr
0 0 0 0 0 0 0 0 0 0
0
0
0
0
0
0
90
0
90
0
90
0
90
0
90
0
0
0
0
1 1 1 0 10 20 30 30
0
0
0
0
0
90
90
90
90
90
90
90
90
90
90
0
0
0
0
1 ?1 1
0 0 0 90 0 90 90 90 0 0
1 1 1
0 0 0 90 90 90 90 90 0 0
“box filter”
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
original filtered
What if the filter size was 5 x 5 instead of 3 x 3?
Slide credit: K. Grauman
Boundary issues
• What is the size of the output?
• MATLAB: output size / “shape” options
– shape = ‘full’: output size is sum of sizes of f and g
– shape = ‘same’: output size is same as f
– shape = ‘valid’: output size is difference of sizes of f and g
full same valid
g g g g
g g
f f f
g g g
g g g
Slide credit: S. Lazebnik
Boundary issues
• What about near the edge?
– the filter window falls off the edge of the image
– need to extrapolate
– methods:
• clip filter (black)
• wrap around
• copy edge
• reflect across edge
0 0 0 0 0 0 0 0 0 0
This kernel is an
0 0 0 0 0 0 0 0 0 0
approximation of a 2d
0 0 0 90 90 90 90 90 0 0
Gaussian function:
0 0 0 90 90 90 90 90 0 0 1 2 1
0 0 0 90 90 90 90 90 0 0
2 4 2
0 0 0 90 0 90 90 90 0 0
1 2 1
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
σ = 5 with σ = 5 with
10 x 10 kernel 30 x 30 kernel
σ = 2 with σ = 5 with
30 x 30 kernel 30 x 30 kernel
>> mesh(h);
>> imagesc(h);
outim
Slide credit: K. Grauman
Smoothing with a Gaussian
Parameter σ is the “scale” / “width” / “spread” of the Gaussian
kernel, and controls the amount of smoothing.
for sigma=1:3:10
h = fspecial('gaussian‘, fsize, sigma);
out = imfilter(im, h);
imshow(out);
pause;
end
Slide credit: K. Grauman
Gaussian Filters
Slide credit: C.
Dyer
Spatial Resolution and Color
B
original
Slide credit: C.
Dyer
Blurring the G Component
B
original processed
Slide credit: C.
Dyer
Blurring the R Component
B
original processed
Slide credit: C.
Dyer
Blurring the B Component
B
original processed
Slide credit: C.
Dyer
“Lab” Color Representation
L A transformation
of the colors into
a color space that
is more
a perceptually
meaningful:
L: luminance,
a: red-green,
b: blue-yellow
b
Slide credit: C.
Dyer
Blurring L
b
original processed
Slide credit: C.
Dyer
Blurring a
b
original processed
Slide credit: C.
Dyer
Blurring b
b
original processed
Slide credit: C.
Dyer
Separability
• In some cases, filter is separable, and we can factor into two
steps:
– Convolve all rows
– Convolve all columns
2D convolution
(center location only)
Perform convolution =
*
along rows:
Followed by convolution =
*
along the remaining column:
Slide credit: K. Grauman
Why is separability useful?
• What is the complexity of filtering an n n image with an m m
kernel?
– O(n2 m2)
• What if the kernel is separable?
– O(n2 m)
• Convolution:
– Flip the filter in both dimensions (bottom to top, right to left)
– Then apply cross-correlation
Notation for
H
convolution
operator
Slide credit: K. Grauman
Convolution vs. Correlation
• A convolution is an integral that expresses the amount of
overlap of one function as it is shifted over another function.
– convolution is a filtering operation
• Correlation compares the similarity of two sets of data.
Correlation computes a measure of similarity of two input
signals as they are shifted by one another. The correlation result
reaches a maximum at the time when the two signals match best
.
– correlation is a measure of relatedness of two signals
Cross-correlation
0 0 0 0 0 0
* 0 1 0 =? * 0 0 1 =?
0 0 0 0 0 0
0 0 0 1 1 1
* 0 2 0
0 0 0
- 1 1 1
1 1 1
=?
0 0 0
0 1 0 ?
0 0 0
Original
0 0 0
0 1 0
0 0 0
Original Filtered
(no change)
0 0 0
0 0 1 ?
0 0 0
Original
0 0 0
0 0 1
0 0 0
1 1 1
1 1 1 ?
1 1 1
Original
1 1 1
1 1 1
1 1 1
0 0 0 1 1 1
0 2 0
0 0 0
- 1 1 1
1 1 1
?
Original
0 0 0 1 1 1
0 2 0
0 0 0
- 1 1 1
1 1 1
– =
+ =
unit impulse
Gaussian Laplacian of Gaussian
Slide credit: C.
Dyer
Unsharp Masking
Slide credit: C.
Dyer
Other filters
1 0 -1
2 0 -2
1 0 -1
Sobel
Vertical Edge
(absolute value)
Slide credit: J. Hays
Other filters
1 2 1
0 0 0
-1 -2 -1
Sobel
Horizontal Edge
(absolute value)
Slide credit: J. Hays
Median filters