Cours 2
Cours 2
&
Edge Detection
P. Fua
(Taught by M. Salzmann)
IC-CVLab
EPFL
1
Reminder: Image Formation
In pixels
xc
u ∝ xi = f
zc
yc
v ∝ yi = f
zc
In meters
2D projection 3D point
x = PX
1000
[0 0 1 0]
P = K 0 1 0 0 Rt
αu s pu
[ 0 1]
R T
with K = 0 αv pv , Rt = , and RT R = I .
0 0 1
Intrinsics Extrinsics
Hartley, Chap 6. 4
Reminder: Camera Calibration
Internal Parameters:
• Horizontal and vertical scaling (2)
• Principal points (2)
• Skew of the axis (1)
External Parameters:
• Rotations (3)
• Translations (3)
aperture
iris diaphragm
Image Plane
6
Reminder: Distortions
10
De Vignetting
12
Sensing
! Quantization in
• Time
• Space 13
Sensors
CCD CMOS
•Charged Coupling Devices (CCD): Made through a special manufacturing process that
allows the conversion from light to signal to take place in the chip without distortion.
•Complimentary Metal Oxide Semiconductor (CMOS): Easier to produce and similar
quality. Now used in most cameras except when quantum efficient pixels are needed,
e.g. for astronomy.
14
In Short
15
Edge Detection
• What’s an edge
• Image gradients
• Edge operators
16
Line Drawings
17
From Edges To Cats
Demo https://affinelayer.com/pixsrv/ 18
Maps and Overlays
19
Corridor
20
Corridor
21
Edges and Regions
Edges:
• Boundary between bland image regions.
Regions:
• Homogenous areas between edges.
! Edge/Region Duality.
22
Discontinuities
B
A
C D
24
More Reality
27
Edge Properties
Original
Orientation
Magnitude
Contrast
28
Edge Descriptors
• Edge Normal:
• Unit vector in the direction of maximum intensity change
• Edge Direction:
• Unit vector perpendicular to the edge normal
• Edge position or center
• Image location at which edge is located
• Edge Strength
• Speed of intensity variation across the edge.
29
Images as 3-D Surfaces
30
Geometric Interpretation
31
Image Gradient
⎡ δI ⎤
∇I = ⎢ ,0⎥
⎣δx ⎦ ⎡ δ I δI ⎤
€ ∇I = ⎢ , ⎥
⎣δx δy ⎦
⎡ δI ⎤
€ ∇I = ⎢0, ⎥
⎣ δy ⎦
€
€
32
Magnitude And Orientation
⎛ ∂I ∂I ⎞
⎜⎜ , ⎟⎟
⎝ ∂x ∂y ⎠
2 2
∂I ∂I
Measure of contrast : G = +
∂x ∂y
∂I ∂I
Edge orientation : θ = arctan( , )
∂y ∂x
33
Gradient Images
∂I
I
∂x
∂I ∂I 2 ∂I 2
+
∂y ∂x ∂y
• Difference Operators
• Convolution Operators
• Trained Detectors
• Deep Nets
36
Gradient Methods
F(x)
x
F’(x)
x
37
1D Finite Differences
In one dimension:
x − dx x x + dx
• for i in range(n-1):
q[i]=(p[i+1]-p[i])
• for i in range(1,n-1):
q[i]=(p[i+1]-p[i-1])/2
• q=(p[2:]-p[:-2])/2
39
2D Finite Differences
40
Coding 2D Finite Differences
p
Python C
Image stored as a 2D array:
Image stored in raster format:
• dx = p[1:,:]-p[:-1,:] {
• dy = p[:,1:]-p[:,:-1] int i;
for(i=0;i<xdim;i++){
dx[i] = p[i+1] -p[i];
• dx = (p[2:,:]-p[:-2,:])/2 dy[i] = p[i+xdim]-p[i];
• dy = (p[:,2:]-p[:,:-2])/2 }
}
• Only 1D array accesses
• No multiplications
—> Can be exploited to increase speed. 41
Noise in 1D
42
Fourier Interpretation
f F
df
dx uF
Problem:
• High frequencies and differentiation do not
mix well.
Solution:
• Suppress high frequencies by
• using the Discrete Fourier Transform.
46
Discrete Fourier Transform
M−1 N−1
1
f(x, y)e −2iπ(μx/M+νy/N)
M*N ∑ ∑
F(μ, ν) =
x=0 y=0
M−1 N−1
1
F(μ, ν)e +2iπ(μx/M+νy/N)
M*N ∑ ∑
f(x, y) =
μ=0 ν=0
47
Fourier Basis Element
Real part of
e +2iπ(ux+vy)
where
• u 2 + v 2 represents the frequency,
• atan(v, u) represents the orientation.
48
Fourier Basis Element
Real part of
e +2iπ(ux+vy)
where
• u 2 + v 2 is larger than before.
49
Fourier Basis Element
Real part of
e +2iπ(ux+vy)
where
• u 2 + v 2 is larger still.
50
Truncated Inverse DFT
M−1 N−1
1
f(x, y)e −2iπ(μx/M+νy/N)
M*N ∑ ∑
F(μ, ν) =
x=0 y=0
M−1 N−1
1
F(μ, ν)e +2iπ(μx/M+νy/N)
M*N ∑ ∑
f(x, y) =
μ=0 ν=0
1
F(μ, ν)e +2iπ(μx/M+νy/N)
∑2
f(x, y) =
M*N 2
μ +ν <T T is a hand-specified threshold.
51
Smoothing by Truncating the IDFT
Rotated stripes:
• Dominant diagonal structures
• Discretization produces additional harmonics
52
Removing Noise
Problem:
• High frequencies and differentiation do not mix well.
Solution:
• Suppress high frequencies by
• using the Discrete Fourier Transform,
• convolving with a low-pass filter.
53
1D Convolution
Z
g ⇤ f (t) = g(t ⌧ )f (⌧ )d⌧
⌧ 54
Smooth Before Differentiating
g* f
∂
(g * f )
∂x
55
Simultaneously Smooth and Differentiate
∂g
∂x
∂ ∂g
(g * f ) = * f
∂x ∂x
Input
1 4 -1 0 2 -2 1 3 3 1
Mask
1 2 0 -1
Input
1 4 -1 0 2 -2 1 3 3 1
1 2 0 -1
Output
W −w+ 1
Input
1 4 -1 0 2 -2 1 3 3 1
1 2 0 -1
Output
9 0
W −w+ 1
Input
1 4 -1 0 2 -2 1 3 3 1
1 2 0 -1
Output
9 0 1
W −w+ 1
Input
1 4 -1 0 2 -2 1 3 3 1
1 2 0 -1
Output
9 0 1 3
W −w+ 1
Input
1 4 -1 0 2 -2 1 3 3 1
1 2 0 -1
Output
9 0 1 3 -5
W −w+ 1
Input
1 4 -1 0 2 -2 1 3 3 1
1 2 0 -1
Output
9 0 1 3 -5 -3
W −w+ 1
Input
1 4 -1 0 2 -2 1 3 3 1
1 2 0 -1
Output
9 0 1 3 -5 -3 6
W −w+ 1
Input
1 4 -1 0 2 -2 1 3 3 1
f
W
Mask
1 2 0 -1
m
w
Output
9 0 1 3 -5 -3 6
m*f
W −w+ 1
∑
m * f(x) = m(i)f(x − i)
i=0 65
Discrete 2D Convolution
Input image: f
Convolved image: m**f
66
Differentiation As Convolution
∂f f ( x + dx, y ) − f ( x, y )
[− 1,1]→ ≈
∂x dx
∂f f ( x + dx, y ) − f ( x − dx, y )
[− 0.5,0,0.5]→ ≈
∂x 2dx
⎡− 1⎤ ∂f f ( x, y + dy ) − f ( x, y )
⎢ 1 ⎥ → ∂y ≈ dy
⎣ ⎦
⎡− 0.5⎤
⎢ 0 ⎥ → ∂f ≈ f ( x, y + dy ) − f ( x, y − dy )
⎢ ⎥ ∂y 2dy
⎢⎣ 0.5 ⎥⎦
68
3X3 Masks
x derivative y derivative
⎡− 1 0 1⎤ ⎡− 1 − 1 − 1⎤ ⎡ − 1 0 1⎤ ⎡− 1 − 2 − 1⎤
⎢− 1 0 1⎥ and ⎢ 0 0 0 ⎥ ⎢− 2 0 2⎥ and ⎢ 0 0 0 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢⎣− 1 0 1⎥⎦ ⎢⎣ 1 1 1 ⎥⎦ ⎢⎣ − 1 0 1 ⎥⎦ ⎢⎣ 1 2 1 ⎥⎦
69
Prewitt Example
70
Sobel Example
71
Gaussian Smoothing
σ =1 σ =2
1 2 2 2
g 2 ( x, y ) = 2
exp(−( x + y ) / 2σ )
2πσ
• The integral is always 1.0
• The larger σ, the broader the Gaussian is.
• As σ approaches 0, the Gaussian approximates a Dirac function.
73
DFT of a Gaussian
74
Gaussians as Low-Pass Filters
75
Gaussian Smoothed Images
Blur σ =1
σ =1
σ =2 Blur σ =2 Blur σ =4
σ =4
77
Separability
1
g1 ( x) = exp(− x 2 / σ 2 )
πσ
g 2 ( x, y ) = g1 ( x) g1 ( y )
∫∫g
u v
2 (u , v) f ( x − u , y − v)dudv = ∫ g1 (u )( ∫ g1 (v) f ( x − u , y − v)dv)du
u v
= x
∂ '
∫ ∫ g 2 (u , v ) f ( x − u , y − v ) dudv = ∫ g1 (v )( ∫ g1 (u ) f ( x − u , y − v ) du ) dv
∂y v u
79
Discrete Gaussian Derivatives
g
Sigma=1: *
g’
g : 0.000070 0.010332 0.207532 0.564131 0.207532 0.010332 0.000070
g’: 0.000418 0.041330 0.415065 0.000000 -0.415065 -0.041330 -0.000418
Sigma=2:
g’: 0.010334 0.044602 0.103784 0.109856 0.000000 -0.109856 -0.103784 -0.044602 -0.010334
σ=1
No Noise
σ=2
σ=4
∂I
I
∂x
∂I ∂I 2 ∂I 2
+
∂y ∂x ∂y
82
Derivative Images
∂I ∂I ∂I 2 ∂I 2
I +
∂x ∂y ∂x ∂y
83
Gradient-Based Tracking
84
Gradient Maximization
85
Real-Time Tracking
86
Canny Edge Detector
∂I 2 ∂I 2
I + Thinned gradient image
∂x ∂y
87
Canny Edge Detector
Convolution
• Gradient strength
• Gradient direction
Thresholding
Non Maxima Suppression
Hysteresis Thresholding
88
Non-Maxima Suppression
89
Hysteresis Thresholding
Increasing scale (σ) removes details but never adds new ones:
• Edge position may shift.
• Two edges may merge.
• An edge may not split into two.
93
Multiple Scales
σ =1 σ =2 σ =4
Fine scale
High threshold
Coarse scale
High threshold
Coarse scale
Low threshold
95
Industrial Application
Image gradient
Given an initial pose estimate:
99
Space Cleaning
https://clearspace.today/ 100
Limitations of the Canny Algorithm
101
Steep Smooth Shading
103
Different Boundary Types
Non-boundaries Boundaries
Intensity
Brightness
Color
Texture
Benchmark
Human Segmentations
106
Comparative Results
Image Canny 2MM BG+CG+TG Human
Yes!
110
Deeper Learning
112
A Partial Explanation?
Canny, PAMI’86 —> Sironi et al. PAMI’15 Sironi et al. PAMI’15 —> Liu et al. , CVPR’17