Lecture 02
Lecture 02
!
!
!
!
Acknowledgement: many slides from James Hays, Derek Hoiem, Svetlana Lazebnik, Esa Rahtu, Steve Seitz, David Lowe,
Kristen Grauman, Alexei Efros, and others.!
!
Three views of filtering!
Source: J. Hays
Image filtering!
Source: Lazebnik
Moving average!
1 1 1
1 1 1
1 1 1
“box filter”
Source: D. Lowe
Image filtering!
1 1 1
1
1
1
1
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
0 0
0 90
90 0
0 90
90 90
90 90
90 0
0 0
0
0
0 0
0 0
0 90
90 90
90 90
90 90
90 90
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 90
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
h[ m, n] = ∑ g[ k , l ] f [ m + k , n + l ]
k ,l Credit: S. Seitz
Image filtering!
1 1 1
1
1
1
1
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
h[ m, n] = ∑ g[ k , l ] f [ m + k , n + l ]
k ,l Credit: S. Seitz
Image filtering!
1 1 1
1
1
1
1
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
h[ m, n] = ∑ g[ k , l ] f [ m + k , n + l ]
k ,l Credit: S. Seitz
Image filtering!
1 1 1
1
1
1
1
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
h[ m, n] = ∑ g[ k , l ] f [ m + k , n + l ]
k ,l Credit: S. Seitz
Image filtering!
1 1 1
1
1
1
1
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
h[ m, n] = ∑ g[ k , l ] f [ m + k , n + l ]
k ,l Credit: S. Seitz
Image filtering!
1 1 1
1
1
1
1
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
h[ m, n] = ∑ g[ k , l ] f [ m + k , n + l ]
k ,l Credit: S. Seitz
Image filtering!
1 1 1
1
1
1
1
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 50
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
h[ m, n] = ∑ g[ k , l ] f [ m + k , n + l ]
k ,l Credit: S. Seitz
Image filtering!
1 1 1
1
1
1
1
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
h[ m, n] = ∑ g[ k , l ] f [ m + k , n + l ]
k ,l Credit: S. Seitz
Box filter - what does it do?!
1 1 1
1 1 1
Source: D. Lowe
Smoothing with box filter!
Practice with linear filters!
0 0 0
0
0
1
0
0
0
?
Original
Source: D. Lowe
Practice with linear filters!
0 0 0
0 1 0
0 0 0
Original Filtered
(no change)
Source: D. Lowe
Practice with linear filters!
0 0 0
0
0
0
0
1
0
?
Original
Source: D. Lowe
Practice with linear filters!
0 0 0
0 0 1
0 0 0
Source: D. Lowe
Practice with linear filters!
0 0 0 1 1 1
0
0
2
0
0
0
- 1
1
1
1
1
1
?
(Note that filter sums to 1)
Original
Source: D. Lowe
Practice with linear filters!
0 0 0 1 1 1
0
0
2
0
0
0
- 1
1
1
1
1
1
Source: D. Lowe
Sharpening!
Source: D. Lowe
Other filters!
1 0 -1
2 0 -2
1 0 -1
Sobel
Vertical Edge
(absolute value)
Other filters!
1 2 1
0 0 0
-1 -2 -1
Sobel
Horizontal Edge
(absolute value)
Key properties!
• Linearity: !
filter(shift(f)) = shift(filter(f))
-> same behavior regardless of pixel location!
Source: S. Lazebnik
Properties in more detail!
• Commutative: a * b = b * a!
• Conceptually no difference between filter and signal!
• Associative: a * (b * c) = (a * b) * c!
• Often apply several filters one after another: (((a * b1) * b2) * b3)!
• This is equivalent to applying one filter: a * (b1 * b2 * b3)!
! h[m, n] = ∑ f [k , l ] I [m + k , n + l ]
k ,l
• 2D convolution:!
!
h=conv2(f,I);
h[m, n] = ∑ f [k , l ] I [m − k , n − l ]
k ,l
Definition of convolution!
• Let f be the image and g be the kernel. The output of convolving f with g is denoted f * g!
( f ∗ g )[m, n] = ∑ f [m − k , n − l ] g[k , l ]
k ,l
Convention:!
kernel is “flipped”!
f
• See MATLAB functions: conv2, filter2, imfilter (the latter two don’t flip the kernel) !
Source: F. Durand
Important filter - Gaussian!
• Spatially-weighted average!
5 x 5, σ = 1
Credit: C. Rasmussen
Smoothing with Gaussian filter!
Smoothing with box filter!
Gaussian filters!
• Separable kernel!
• Factors into product of two 1D Gaussians!
Source: K. Grauman
Separability of the Gaussian filter!
Separability example!
2D filtering
(center location only)
Perform filtering =
along rows: *
Followed by filtering =
along the remaining column: *
Source: K. Grauman
Separability!
f f f
g g
g g
g g
Source: S. Lazebnik
Why Gaussian gives smooth output compared to box filter?!
Source: D. Hoiem
Why lower resolution image still make sense? What is lost?!
Source: D. Hoiem
Thinking in terms of frequency!
Jean Baptiste Joseph Fourier (1768-1830)!
...the manner in which the author arrives at these
equations is not exempt of difficulties and...his
• He had a crazy idea in 1807:! analysis to integrate them still leaves something to be
desired on the score of generality and even rigour.
Any univariate function can be rewritten !
as a weighted sum of sines and cosines !
of different frequencies. !
• Don’t believe it? !
• Neither did lagrange, Laplace, Poisson and other big wigs!
Laplace
• Not translated into English until 1878!!
Legendre
Lagrange
Slides: Efros
A sum of sines!
• Example:!
g(t) = sin(2πf t ) + (1/3)sin(2π( 3f ) t)!
= +
Example: Music!
Source: D. Hoiem
2D signals!
• We can also think of all kinds of other signals the same way!
Source: D. Hoiem
Other signals!
• We can also think of all kinds of other signals the same way!
Source: D. Hoiem
Fourier analysis in images!
Intensity Image
Fourier Image
+ =
http://sharp.bu.edu/~slehar/fourier/fourier.html#filtering
Source: D. Hoiem More: http://www.cs.unm.edu/~brayer/vision/fourier.html
Fourier Bases!
Log Magnitude
Low Frequencies
2 2 I (ω )
−1
Amplitude: A = ± R(ω ) + I (ω ) Phase: φ = tan
R(ω )
Euler’s formula:
Source: D. Hoiem
Source: L. Xie
Computing 2D-DFT!
DFT
IDFT
• The Fourier transform of the convolution of two functions is the product of their Fourier
transforms!
F[ g ∗ h] = F[ g ] F[ h]
• The inverse Fourier transform of the product of two Fourier transforms is the convolution of the
two inverse Fourier transforms!
!
−1 −1 −1
!
! F [ gh] = F [ g ] ∗ F [ h]
!
• Convolution in spatial domain is equivalent to multiplication in frequency domain!!
Source: D. Hoiem
Properties of Fourier Transforms!
• Linearity!
• The energy of the signal is the same as the energy of its Fourier transform!
A = “Aron” P = “Phyllis”
FA = fft2(A) FP = fft2(P)
log(abs(FA)) log(abs(FP))
angle(FA) angle(FP)
Source: L. Xie
What this all has to do with filtering?!
Filtering in spatial domain!
1 0 -1
2 0 -2
1 0 -1
* =
Source: D. Hoiem
Filtering in frequency domain!
FFT FFT
=
Inverse FFT
Source: D. Hoiem
Why Gaussian gives smooth output compared to box filter?!
Source: D. Hoiem
Gaussian filter!
Box filter!
Why lower resolution image still make sense? What is lost?!
Source: D. Hoiem
Subsampling by a factor of two!
Throw away every other row and column to create a ½ size image
Problem: Aliasing !
!
• Characteristic errors may appear ”checker board disintegrate”, “striped shirts look funny”,….!
Nyquist-Shannon sampling theorem !
• When sampling a signal at discrete intervals, the sampling frequency must be ≥ 2 × fmax!
• This allows to reconstruct the original perfectly from the sampled version!
good
bad
Solution: Anti-aliasing!
Credit: S. Seitz
Subsampling with pre-filtering!
Source: D. Hoiem
Hybrid Images!
• Early processing in humans filters for various orientations and scales of frequency!
• Perceptual cues in the mid-high frequencies dominate perception!
• When we see an image from far away, we are effectively subsampling it (and low pass filtering)!
Source: D. Hoiem
Hybrid Image in FFT!
Source: D. Hoiem
Thus, we get distance-dependent interpretation of a hybrid image!
Source: D. Hoiem
Matching with filters!
f = image
g = filter
True detections
False
detections
∑ ( g[k , l ] − g )( f [m + k , n + l ] − f
k ,l
m ,n )
h[ m, n] = 0.5
⎛ 2 2⎞
⎜⎜ ∑ ( g[ k , l ] − g ) ∑ ( f [ m + k , n + l ] − f m,n ) ⎟⎟
⎝ k ,l k ,l ⎠
!
A: Depends!
• Zero-mean filter: fast but not a great matcher!
• Normalized cross-correlation: slow but invariant to local average intensity and contrast!
Source: D. Hoiem
Q: What if we want to find larger or smaller eyes?!
• Compression!
• Object detection!
• Scale search!
• Features!
Source: D. Hoiem
Things to Remember!