Lec08 Linear Filtering
Lec08 Linear Filtering
Lec08 Linear Filtering
Course Project:
• Proposals: Friday, March 15 (2.5 weeks away!)
• Use Piazza to nd partners (teams of size 2-3)
• Talk to us in OHs about project ideas
• Poster Presentation: Friday May 10th, 2-4pm, LGRC A112.
• Reports: Sunday, May 12th
• Details: https://cvl-umass.github.io/compsci670-spring-2024/project/
Applications
• Denoising
• Sharpening
• Edge detection
1 1 1
1 1 1
1 1 1
“box filter”
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: D. Lowe 5
Filtering
Let f be the image and g be the kernel. The output of ltering f with g denoted f *g is given by:
X
(f ⇤ g)[m, n] = f [m + k, n + l]g[k, l]
k,l
g g
multi-channel f
multi-channel
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: F. Durand 7
fi
Key properties
Linearity: lter(f1 + f2) = lter(f1) + lter(f2)
Shift invariance: same behavior regardless of pixel location: lter(shift(f)) = shift( lter(f))
Theoretical result: any linear shift-invariant operator can be represented as a convolution
Associative: a * (b * c) = (a * b) * c
• Often apply several lters one after another: (((a * b1) * b2) * b3)
f f f
g g
g g
g g
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 10
Annoying details
What about near the edge?
• the lter window falls off the edge of the image
• need to extrapolate
• methods:
• clip lter (black) — correlate(f, g, mode=‘constant’, cval=0.0)
• wrap around — correlate(f, g, mode=‘wrap’)
• copy edge — correlate(f, g, mode=‘nearest’)
• re ect across edge –– correlate (f, g, mode=‘re ect’)
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: S. Marschner 11
fl
fi
fi
fl
Practice with linear lters
0 0 0
0 1 0 ?
0 0 0
Original
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: D. Lowe 12
fi
Practice with linear lters
0 0 0
0 1 0
0 0 0
Original Filtered
(no change)
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: D. Lowe 13
fi
Practice with linear lters
0 0 0
0 0 1 ?
0 0 0
Original
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: D. Lowe 14
fi
Practice with linear lters
0 0 0
0 0 1
0 0 0
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: D. Lowe 15
fi
Practice with linear lters
1 1 1
1 1 1 ?
1 1 1
Original
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: D. Lowe 16
fi
Practice with linear lters
1 1 1
1 1 1
1 1 1
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: D. Lowe 17
fi
Practice with linear lters
0 0 0 1 1 1
0 2 0
0 0 0
- 1 1 1
1 1 1
?
(Note that filter sums to 1)
Original
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: D. Lowe 18
fi
Practice with linear lters
0 0 0 1 1 1
0 2 0
0 0 0
- 1 1 1
1 1 1
Original
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: D. Lowe 19
fi
Smoothing with box lter revisited
What’s wrong with this picture?
What’s the solution?
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: D. Forsyth 20
fi
Smoothing with box lter revisited
What’s wrong with this picture?
What’s the solution?
• To eliminate edge effects, weight contribution of neighborhood pixels according to their
closeness to the center
“fuzzy blob”
5 x 5, σ = 1
Source: C. Rasmussen
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 22
fi
Gaussian kernel
Standard deviation σ: determines extent of smoothing
σ = 2 with 30 x 30 σ = 5 with 30 x 30
kernel kernel
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: K. Grauman 23
Choosing kernel width
The Gaussian function has in nite support, but discrete lters use nite kernels
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: K. Grauman 24
fi
fi
fi
Choosing kernel width
Rule of thumb: set lter half-width (radius) to about 3σ
&1 2 1 # &1 #
$2 4 2! = $2![1 2 1]
$ ! $ !
$%1 2 1 !" $%1 !"
Source: K. Grauman
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 27
fi
Separability of the Gaussian lter
Source: D. Lowe
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24
fi
28
Why is separability useful?
Separability means that a 2D convolution can be reduced to two 1D convolutions (one among
rows and one among columns)
What is the complexity of ltering an n×n image with an m×m kernel?
• O(n2 m2)
What if the kernel is separable?
• O(n2 m)
Source: S. Seitz
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 30
Gaussian noise
Mathematical model: sum of many independent factors
Good for small standard deviations
Assumption: independent, zero-mean noise
Source: M. Hebert
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 31
Reducing Gaussian noise
noise
Robustness to outliers
Source: K. Grauman
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 35
fi
fi
fi
Median lter
Salt-and-pepper noise Median filtered
Source: D. Lowe
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 37
Sharpening
What does blurring take away?
– =
+α =
unit impulse
Gaussian Laplacian of Gaussian
Gaussian Filter
Laplacian Filter
“Adversarial examples”
+ =
Source: D. Lowe
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 47
Origin of edges
Edges are caused by a variety of factors:
depth discontinuity
illumination discontinuity
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 Source: Steve Seitz 48
Edge detection
An edge is a place of rapid change in the image intensity function
intensity function
image (along horizontal scanline) first derivative
edges correspond to
extrema of derivative
∂f ( x, y ) f ( x + ε , y ) − f ( x, y )
= lim
∂x ε → 0 ε
For discrete data, we can approximate using nite differences:
∂f ( x, y ) f ( x + 1, y ) − f ( x, y )
≈
∂x 1
To implement the above as convolution, what would be the associated lter?
Source: K. Grauman
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 50
fi
fi
Partial derivatives of an image
∂f ( x, y ) ∂f ( x, y )
∂x ∂y
-1 1
-1 1 1 or
-1
f*g
d
( f ∗ g)
dx
d
• To find edges, look for peaks in ( f ∗ g)
dx Source: S. Seitz
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 54
fi
Derivative theorem of convolution
Differentiation is convolution, and convolution is associative:
d d
( f ∗ g) = f ∗ g
dx dx
This saves us one operation:
d
g
dx
d
f∗ g
dx
Source: S. Seitz
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24 55
Derivative of Gaussian lters
x-direction y-direction
Smoothed derivative removes noise, but blurs edge. Also nds edges at different “scales”
Source: D. Forsyth
COMPSCI 670 Grant Van Horn — UMass Amherst, Spring 24
fi
57
fi
Smoothing and derivative lters
Smoothing lters
• Gaussian: remove “high-frequency” components;
“low-pass” lter
• Can the values of a smoothing lter be negative?
• What should the values sum to?
• One: constant regions are not affected by the lter
Derivative lters
• Derivatives of Gaussian
• Can the values of a derivative lter be negative?
• What should the values sum to?
• Zero: no response in constant regions
• High absolute value at points of high contrast