Lecture4
Lecture4
Xiaoming Liu
Thank many researchers who have made their slides and course materials available
Let’s Take An Image
credit: D Fouhey
Let’s Fix Things
• We have noise in our image
• Let’s replace each pixel with a weighted
average of its neighborhood
• Weights are filter kernel
Pixel Pixel Pixel
1/9 1/9 1/9
1 2 3
Pixel Pixel Pixel
X 1/9 1/9 1/9 Out
4 5 6
Pixel Pixel Pixel
1/9 1/9 1/9
7 8 9
Input image patch Weight for each pixel Output
Slide Credit: D. Lowe
1D Case
Signal/ 10 12 9 11 10 11 12
Front Row
credit: D Fouhey
Applying a Linear Filter
I21 I22 I23 I24 I25 I26 F11 F12 F13 O11 O12 O13 O14
I31 I32 I33 I34 I35 I36 F21 F22 F23 O21 O22 O23 O24
I41 I42 I43 I44 I45 I46 F31 F32 F33 O31 O32 O33 O34
credit: D Fouhey
Applying a Linear Filter
F21
I21 F22
I22 F23
I23 I24 I25 I26 O11
F31
I31 F32
I32 F33
I33 I34 I35 I36
credit: D Fouhey
Applying a Linear Filter
I21 F21
I22 F22
I23 F23
I24 I25 I26 O11 O12
I31 F31
I32 F32
I33 F33
I34 I35 I36
credit: D Fouhey
Applying a Linear Filter
credit: D Fouhey
Applying a Linear Filter
I21 I22 I23 I24 I25 I26 F11 F12 F13 O11 O12 O13 O14
I31 I32 I33 I34 I35 I36 F21 F22 F23 O21 O22 O23 O24
I41 I42 I43 I44 I45 I46 F31 F32 F33 O31 O32 O33 O34
f f f
g g
g g g g
f/g Diagram Credit: D. Lowe
Painful Details – Edge Cases
What to about the “?” region?
0 0 0
0 1 0
?
0 0 0
Original
0 0 0
0 1 0
0 0 0
0 0 0
0 0 1 ?
0 0 0
Original
0 0 0
0 0 1
0 0 0
Original Shifted
LEFT
1 pixel
0 1 0
0 0 0 ?
0 0 0
Original
0 1 0
0 0 0
0 0 0
Original Shifted
DOWN
1 pixel
Original
Original Blur
(Box Filter)
-
?
1/9 1/9 1/9
Original
1/9 1/9 1/9
1/9 1/9 1/9
-
1/9 1/9 1/9
Original Sharpened
1/9 1/9 1/9 (Acccentuates
difference from
1/9 1/9 1/9 local average)
A( , + ) =A( , )=
A( , )+A( , )= + =
Note: I am showing filters un-normalized and blown up. They’re a smaller
box filter (i.e., each entry is 1/(size^2))
credit: D Fouhey
Properties – Shift-Invariant
A( , )=
A( , )=
credit: D Fouhey
Painful Details – Signal Processing
Cross-Correlation Convolution
(Original Orientation) (Flipped in x and y)
credit: D Fouhey
Properties of Convolution
• Any shift-invariant, linear operation is a convolution (⁎)
• Commutative: f ⁎ g = g ⁎ f
• Associative: (f ⁎ g) ⁎ h = f ⁎ (g ⁎ h)
• Distributes over +: f ⁎ (g + h) = f ⁎ g + f ⁎ h
• Scalars factor out: kf ⁎ g = f ⁎ kg = k (f ⁎ g)
• Identity (a single one with all zeros):
* =
Property List: K. Grauman
Questions?
credit: D Fouhey
Solution – Weighted Combination
𝐹𝑖𝑙𝑡𝑒𝑟𝑖𝑗 ∝ 1
What’s this?
𝑥2 + 𝑦2
𝐹𝑖𝑙𝑡𝑒𝑟𝑖𝑗 ∝ exp −
2𝜎 2
credit: D Fouhey
Recognize the Filter?
It’s a Gaussian!
1 𝑥2 + 𝑦2
𝐹𝑖𝑙𝑡𝑒𝑟𝑖𝑗 ∝ 2
exp −
2𝜋𝜎 2𝜎 2
credit: D Fouhey
Smoothing With A Box & Gauss
Still have some speckles, but it’s not a big box
Input Box Filter Gauss. Filter
credit: D Fouhey
Gaussian Filters
credit: D Fouhey
Applying Gaussian Filters
Input Image
(no filter)
credit: D Fouhey
Applying Gaussian Filters
σ=1
credit: D Fouhey
Applying Gaussian Filters
σ=2
credit: D Fouhey
Applying Gaussian Filters
σ=4
credit: D Fouhey
Applying Gaussian Filters
σ=8
credit: D Fouhey
Picking a Filter Size
Too small filter → bad approximation
Want size ≈ 6σ (99.7% of energy)
Left far too small; right slightly too small!
σ = 8, size = 21 σ = 8, size = 43
credit: D Fouhey
Runtime Complexity
Image size = NxN = 6x6
Filter size = MxM = 3x3
I11 I12 I13 I14 I15 I16
for ImageY in range(N):
I21 F11
I22 F12
I23 F13
I24 I25 I26 for ImageX in range(N):
I31 I32
F21 I33
F22 I34
F23 I35 I36 for FilterY in range(M):
I41 F31
I42 F32
I43 F33
I44 I45 I46
for FilterX in range(M):
…
I51 I52 I53 I54 I55 I56
credit: D Fouhey
Separability
credit: D Fouhey
Separability
1 𝑥2 + 𝑦2
𝐹𝑖𝑙𝑡𝑒𝑟𝑖𝑗 ∝ 2
exp −
2𝜋𝜎 2𝜎 2
→
𝑥21 1 𝑦2
𝐹𝑖𝑙𝑡𝑒𝑟𝑖𝑗 ∝ exp − 2 exp − 2
2𝜋𝜎 2𝜎 2𝜋𝜎 2𝜎
credit: D Fouhey
Separability
1D Gaussian ⁎ 1D Gaussian = 2D Gaussian
Image ⁎ 2D Gauss = Image ⁎ (1D Gauss ⁎ 1D Gauss )
= (Image ⁎ 1D Gauss) ⁎ 1D Gauss
⁎ =
credit: D Fouhey
Runtime Complexity
Image size = NxN = 6x6
Filter size = Mx1 = 3x1
I11 I12 I13 I14 I15 I16
for ImageY in range(N):
I21 F1
I22 I23 I24 I25 I26 for ImageX in range(N):
I31 I32
F2 I33 I34 I35 I36 for FilterY in range(M):
I41 F3
I42 I43 I44 I45 I46
…
for ImageY in range(N):
I51 I52 I53 I54 I55 I56
for ImageX in range(N):
I61 I62 I63 I64 I65 I66
for FilterX in range(M):
What are my compute …
savings for a 13x13 filter?
credit: D Fouhey
Time: O(N2M) old: N^2*13*13 new: N^2*13*2
Why Gaussian?
credit: D Fouhey
Where Gaussian Fails
credit: D Fouhey
Applying Gaussian Filters
σ=1
credit: D Fouhey
Why Does This Fail?
Means can be arbitrarily distorted by outliers
Signal 10 12 9 8 1000 11 10 12
Sort
40 81 13 22
[013, 040, 076, 081, 092, 108, 125, 144, 830]
125 830 76 80
144 92 108 95 92
132 102 106 87
[830, 076, 080, 092, 108, 095, 102, 106, 087]
Sort
[076, 080, 087, 092, 095, 102, 106, 108, 830]
95
credit: D Fouhey
Applying Median Filter
Median
Filter
(size=3)
credit: D Fouhey
Applying Median Filter
Median
Filter
(size = 7)
credit: D Fouhey
Is Median Filtering Linear?
1 1 1 0 0 0 1 1 1
1 1 2 + 0 1 0 = 1 2 2
2 2 2 0 0 0 2 2 2
Median Filter
1 + 0 = 2
-
Details
=
credit: D Fouhey
Filtering – Sharpening
Image Details
+α
“Sharpened” α=1
=
credit: D Fouhey
Filtering – Sharpening
Image Details
+α
“Sharpened” α=0
=
credit: D Fouhey
Filtering – Sharpening
Image Details
+α
“Sharpened” α=2
=
credit: D Fouhey
Filtering – Extreme Sharpening
Image Details
+α
“Sharpened” α=10
=
credit: D Fouhey
Filtering
Dx Dy
credit: D Fouhey
Filtering – Derivatives
credit: D Fouhey
Filtering – Counting
⁎ r=10
=
credit: D Fouhey
Filtering – Counting
x =
credit: D Fouhey
Filtering – Missing Data
Oh no! Missing data!
(and we know where)
Image ⁎
Per-element /
Binary
Mask ⁎
Filtering – Missing Data
Image
Per-element /
Binary
Mask
Filtering – Missing Data
Before
Filtering – Missing Data
After
Filtering – Missing Data