Image Filtering
(Spatial Domain)
Elsayed Hemayed
Overview
• Convolution and Correlation
• Box Filter and Gaussian Filter
• Template Matching and CNN
Convolution
• Convolution of two functions is defined as
– In the discrete case is:
( )* ( ) ( ) ( )f x h x f h x d  


 
[ ]* [ ] [ ] [ ]
m
f n h n f m h n m


 
Convolution – 1D
• In the 2D discrete case Convolution is
defined as:
is called a linear filter1 2[ , ]h n n
Convolution – 2D
Image filtering• Image filtering:
– Compute function of local neighborhood at each position
],[],[],[
,
lnkmIlkfnmh
lk
 
I=imagef=filterh=output
2d coords=m,n2d coords=k,l
[ ] [ ]
[ ]
Correlation and Convolution
• 2d correlation
],[],[],[
,
lnkmIlkfnmh
lk
 
James Hays
e.g., h = scipy.signal.correlate2d(f,I)
Correlation and Convolution
• 2d correlation
• 2d convolution
],[],[],[
,
lnkmIlkfnmh
lk
 
],[],[],[
,
lnkmIlkfnmh
lk
 
Convolution is the same as correlation with a 180° rotated filter kernel.
Correlation and convolution are identical when the filter kernel is symmetric
James Hays
e.g., h = scipy.signal.correlate2d(f,I)
e.g., h = scipy.signal.convolve2d(f,I)
– Step1:
filter image
f f x h
Image Filtering
– Step2:
filter image
f x hf
Image Filtering
– Step3:
filter image
f x hf
Image Filtering
– Step4:
filter image
f x hf
Image Filtering
– Step5:
filter image
f x hf
etc. . . . .
Image Filtering
Image filtering
• Image filtering:
– Compute function of local neighborhood at each
position
• Really important!
– Enhance images
• Denoise, resize, increase contrast, etc.
– Extract information from images
• Texture, edges, distinctive points, etc.
– Detect patterns
• Template matching
],[],[],[
,
lnkmIlkfnmh
lk
 
James Hays
What does it do?
• Replaces each pixel with
an average of its
neighborhood
• Achieve smoothing effect
(remove sharp features)
111
111
111
],[g 
Box Filter
15
1 1 1
1
1 1 1
9
1 1 1
 
 
 
  
* =
Smoothing with box filter
17
Practice with linear filters
000
010
000
Original
?
18
Practice with linear filters
000
010
000
Original Filtered
(no change)
19
Practice with linear filters
000
100
000
Original
?
20
Practice with linear filters
000
100
000
Original Shifted left
By 1 pixel
21
Practice with linear filters
Original
111
111
111
000
020
000
- ?
22
Practice with linear filters
Original
111
111
111
000
020
000
-
Sharpening filter
- Accentuates differences with local
average
23
Sharpening
24
Edges
-101
-202
-101
Vertical Edge
(absolute value)
Sobel
David Lowe
Edges
-1-2-1
000
121
Horizontal Edge
(absolute value)
Sobel
David Lowe
• Spatially-weighted average
0.003 0.013 0.022 0.013 0.003
0.013 0.059 0.097 0.059 0.013
0.022 0.097 0.159 0.097 0.022
0.013 0.059 0.097 0.059 0.013
0.003 0.013 0.022 0.013 0.003
5 x 5,  = 1
Slide credit: Christopher Rasmussen
Important filter: Gaussian
27
Smoothing with Gaussian filter
28
Smoothing with box filter
29
Gaussian vs. mean filters
What does real blur look like?
Gaussian filters
• What parameters matter here?
• Variance of Gaussian: determines extent of
smoothing
Source: K. Grauman
31
Smoothing with a Gaussian
…
Parameter σ is the “scale” / “width” / “spread” of the Gaussian
kernel, and controls the amount of smoothing.
Source: K. Grauman
32
Gaussian filters
• Remove “high-frequency” components from the
image (low-pass filter)
– Images become more smooth
• Gaussian convolved with Gaussian…
…is another Gaussian
– So can smooth with small-width kernel, repeat, and
get same result as larger-width kernel would have
– Convolving two times with Gaussian kernel of width σ
is same as convolving once with kernel of width σ √2
• Separable kernel
– Factors into product of two 1D Gaussians
Source: K. Grauman
Separability of the Gaussian filter
Source: D. Lowe
Separability example
*
*
=
=
2D convolution
(center location only)
Source: K. Grauman
The filter factors
into a product of 1D
filters:
Perform convolution
along rows:
Followed by convolution
along the remaining column:
=
Separable filters
* =
Compute Gaussian in
horizontal direction, followed
by the vertical direction.
Not all filters are separable.
Freeman and Adelson, 1991
Much faster!
Separability
Why is separability useful in practice?
MxN image, PxQ filter
• 2D convolution: ~MNPQ multiply-adds
• Separable 2D: ~MN(P+Q) multiply-adds
Speed up = PQ/(P+Q)
9x9 filter = ~4.5x faster
Practical matters
• How big should the filter be?
• Values at edges should be near zero
• Gaussians have infinite extent…
• Rule of thumb for Gaussian: set filter half-
width to about 3 σ
James Hays
Practical matters
• 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
Source: S. Marschner
mode=‘full’ (Default; pad with zeros)
mode=‘same’ (Return same size as D)
mode=‘valid’ (No padding)
Practical matters
I = correlate2d( D, f, ‘same’ )
I = convolve2d( D, f, ‘same’ )
# Normalize for visualization
>> I_norm = (I – np.min(I)) / (np.max(I) – np.min(I))
>> plt.imshow( I_norm )
Convolution in Convolutional
Neural Networks
• Convolution is the basic operation in
CNNs
• Learning convolution kernels allows us to
learn which `features’ provide useful
information in images.
Input Image
Feature Map
.
.
.
Convolution (Filtering) Example
Source: Rob Fergus, “Deep Learning & Feature Learning Methods for Vision” , CVPR 2012
Tutorial
Filters
>> f = D[ 57:117, 107:167 ]
Expect response ‘peak’ in middle of I
>> I = correlate2d( D, f, ‘same’ )
f
61 x 61
D (275 x 175 pixels)
I
Response
peak
Hmm…
That didn’t work – why not?
+
Correct
location
[Thanks to Robert Collins @ Penn State]
Template Matching
Correlation
],[],[],[
,
lnkmIlkfnmh
lk
 
e.g., h = scipy.signal.correlate2d(f,I)
As brightness in I increases, the response
in h will increase, as long as f is positive.
Overall brighter regions will give higher
correlation response -> not useful!
OK, so let’s subtract the mean
>> f = D[ 57:117, 107:167 ]
>> f2 = f – np.mean(f)
>> D2 = D – np.mean(D)
Now zero centered.
Score is higher only when dark parts
match and when light parts match.
>> I2 = correlate2d( D2, f2, ‘same’ )
f2
61 x 61
D2 (275 x 175 pixels)
I2
Or even
>> I3 = correlate2d( D2, f2, ‘full’ )
I3
D2 (275 x 175 pixels)
What happens with convolution?
>> f = D[ 57:117, 107:167 ]
>> f2 = f – np.mean(f)
>> D2 = D – np.mean(D)
>> I2 = convolve2d( D2, f2, ‘same’ )
f2
61 x 61
D2 (275 x 175 pixels)
I2
Summary
• Convolution and Correlation
• Box Filter and Gaussian Filter
• Template Matching and CNN

03 cie552 image_filtering_spatial

  • 1.
  • 2.
    Overview • Convolution andCorrelation • Box Filter and Gaussian Filter • Template Matching and CNN
  • 3.
    Convolution • Convolution oftwo functions is defined as – In the discrete case is: ( )* ( ) ( ) ( )f x h x f h x d       [ ]* [ ] [ ] [ ] m f n h n f m h n m    
  • 4.
  • 5.
    • In the2D discrete case Convolution is defined as: is called a linear filter1 2[ , ]h n n Convolution – 2D
  • 6.
    Image filtering• Imagefiltering: – Compute function of local neighborhood at each position ],[],[],[ , lnkmIlkfnmh lk   I=imagef=filterh=output 2d coords=m,n2d coords=k,l [ ] [ ] [ ]
  • 7.
    Correlation and Convolution •2d correlation ],[],[],[ , lnkmIlkfnmh lk   James Hays e.g., h = scipy.signal.correlate2d(f,I)
  • 8.
    Correlation and Convolution •2d correlation • 2d convolution ],[],[],[ , lnkmIlkfnmh lk   ],[],[],[ , lnkmIlkfnmh lk   Convolution is the same as correlation with a 180° rotated filter kernel. Correlation and convolution are identical when the filter kernel is symmetric James Hays e.g., h = scipy.signal.correlate2d(f,I) e.g., h = scipy.signal.convolve2d(f,I)
  • 9.
    – Step1: filter image ff x h Image Filtering
  • 10.
    – Step2: filter image fx hf Image Filtering
  • 11.
    – Step3: filter image fx hf Image Filtering
  • 12.
    – Step4: filter image fx hf Image Filtering
  • 13.
    – Step5: filter image fx hf etc. . . . . Image Filtering
  • 14.
    Image filtering • Imagefiltering: – Compute function of local neighborhood at each position • Really important! – Enhance images • Denoise, resize, increase contrast, etc. – Extract information from images • Texture, edges, distinctive points, etc. – Detect patterns • Template matching ],[],[],[ , lnkmIlkfnmh lk   James Hays
  • 15.
    What does itdo? • Replaces each pixel with an average of its neighborhood • Achieve smoothing effect (remove sharp features) 111 111 111 ],[g  Box Filter 15
  • 16.
    1 1 1 1 11 1 9 1 1 1          * =
  • 17.
  • 18.
    Practice with linearfilters 000 010 000 Original ? 18
  • 19.
    Practice with linearfilters 000 010 000 Original Filtered (no change) 19
  • 20.
    Practice with linearfilters 000 100 000 Original ? 20
  • 21.
    Practice with linearfilters 000 100 000 Original Shifted left By 1 pixel 21
  • 22.
    Practice with linearfilters Original 111 111 111 000 020 000 - ? 22
  • 23.
    Practice with linearfilters Original 111 111 111 000 020 000 - Sharpening filter - Accentuates differences with local average 23
  • 24.
  • 25.
  • 26.
  • 27.
    • Spatially-weighted average 0.0030.013 0.022 0.013 0.003 0.013 0.059 0.097 0.059 0.013 0.022 0.097 0.159 0.097 0.022 0.013 0.059 0.097 0.059 0.013 0.003 0.013 0.022 0.013 0.003 5 x 5,  = 1 Slide credit: Christopher Rasmussen Important filter: Gaussian 27
  • 28.
  • 29.
  • 30.
    Gaussian vs. meanfilters What does real blur look like?
  • 31.
    Gaussian filters • Whatparameters matter here? • Variance of Gaussian: determines extent of smoothing Source: K. Grauman 31
  • 32.
    Smoothing with aGaussian … Parameter σ is the “scale” / “width” / “spread” of the Gaussian kernel, and controls the amount of smoothing. Source: K. Grauman 32
  • 33.
    Gaussian filters • Remove“high-frequency” components from the image (low-pass filter) – Images become more smooth • Gaussian convolved with Gaussian… …is another Gaussian – So can smooth with small-width kernel, repeat, and get same result as larger-width kernel would have – Convolving two times with Gaussian kernel of width σ is same as convolving once with kernel of width σ √2 • Separable kernel – Factors into product of two 1D Gaussians Source: K. Grauman
  • 34.
    Separability of theGaussian filter Source: D. Lowe
  • 35.
    Separability example * * = = 2D convolution (centerlocation only) Source: K. Grauman The filter factors into a product of 1D filters: Perform convolution along rows: Followed by convolution along the remaining column: =
  • 36.
    Separable filters * = ComputeGaussian in horizontal direction, followed by the vertical direction. Not all filters are separable. Freeman and Adelson, 1991 Much faster!
  • 37.
    Separability Why is separabilityuseful in practice? MxN image, PxQ filter • 2D convolution: ~MNPQ multiply-adds • Separable 2D: ~MN(P+Q) multiply-adds Speed up = PQ/(P+Q) 9x9 filter = ~4.5x faster
  • 38.
    Practical matters • Howbig should the filter be? • Values at edges should be near zero • Gaussians have infinite extent… • Rule of thumb for Gaussian: set filter half- width to about 3 σ James Hays
  • 39.
    Practical matters • Whatabout 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 Source: S. Marschner
  • 40.
    mode=‘full’ (Default; padwith zeros) mode=‘same’ (Return same size as D) mode=‘valid’ (No padding) Practical matters I = correlate2d( D, f, ‘same’ ) I = convolve2d( D, f, ‘same’ ) # Normalize for visualization >> I_norm = (I – np.min(I)) / (np.max(I) – np.min(I)) >> plt.imshow( I_norm )
  • 41.
    Convolution in Convolutional NeuralNetworks • Convolution is the basic operation in CNNs • Learning convolution kernels allows us to learn which `features’ provide useful information in images.
  • 42.
    Input Image Feature Map . . . Convolution(Filtering) Example Source: Rob Fergus, “Deep Learning & Feature Learning Methods for Vision” , CVPR 2012 Tutorial Filters
  • 43.
    >> f =D[ 57:117, 107:167 ] Expect response ‘peak’ in middle of I >> I = correlate2d( D, f, ‘same’ ) f 61 x 61 D (275 x 175 pixels) I Response peak Hmm… That didn’t work – why not? + Correct location [Thanks to Robert Collins @ Penn State] Template Matching
  • 44.
    Correlation ],[],[],[ , lnkmIlkfnmh lk   e.g., h= scipy.signal.correlate2d(f,I) As brightness in I increases, the response in h will increase, as long as f is positive. Overall brighter regions will give higher correlation response -> not useful!
  • 45.
    OK, so let’ssubtract the mean >> f = D[ 57:117, 107:167 ] >> f2 = f – np.mean(f) >> D2 = D – np.mean(D) Now zero centered. Score is higher only when dark parts match and when light parts match. >> I2 = correlate2d( D2, f2, ‘same’ ) f2 61 x 61 D2 (275 x 175 pixels) I2
  • 46.
    Or even >> I3= correlate2d( D2, f2, ‘full’ ) I3 D2 (275 x 175 pixels)
  • 47.
    What happens withconvolution? >> f = D[ 57:117, 107:167 ] >> f2 = f – np.mean(f) >> D2 = D – np.mean(D) >> I2 = convolve2d( D2, f2, ‘same’ ) f2 61 x 61 D2 (275 x 175 pixels) I2
  • 48.
    Summary • Convolution andCorrelation • Box Filter and Gaussian Filter • Template Matching and CNN