08 Edge-Detection
08 Edge-Detection
Computer Vision
Edge Detection
Virginia Tech
Fall 2023
1
What do you
see here?
2
What do you
see here?
3
original
regions edges
4
Feature extraction
5
Thresholding
• Compare every pixel value with a given constant T, called the threshold value, and
assign an output value based on the comparison
• Example:
1 if I ( r , c ) ≥ T
I NEW ( r , c ) =
0 if I ( r , c ) < T
6
7
T = 213
T = 137
Examples of thresholding
T = 159
T = 124
EDGE DETECTION
8
An edge is an abrupt change in image intensity
9
What can cause an intensity edge?
Sudden change
Depth in reflectance:
discontinuity here, paint on
object’s surface
11
Using the first derivative to detect edges
• Ideal case:
intensity function
𝑑𝑑𝑑𝑑 𝑓𝑓 𝑥𝑥 + ∆𝑥𝑥 − 𝑓𝑓 𝑥𝑥 image (along horizontal scanline) first derivative
= lim
𝑑𝑑𝑑𝑑 Δ𝑥𝑥→0 ∆𝑥𝑥
• Discrete approximation:
𝑑𝑑𝑑𝑑 𝑓𝑓 𝑥𝑥 + ∆𝑥𝑥 − 𝑓𝑓 𝑥𝑥
≈
𝑑𝑑𝑑𝑑 ∆𝑥𝑥
edges correspond to
extrema of derivative
12
Now extend this idea to a 2-dimensional image I(x, y)
• ∆𝑥𝑥 and ∆𝑦𝑦 may be defined to be one pixel, or some other value that we choose
13
Mandrill: “a large fierce gregarious baboon of western Africa”
14
Convert to gray-scale; compute 𝐼𝐼𝑥𝑥 𝑟𝑟, 𝑐𝑐 = 𝐼𝐼 𝑟𝑟, 𝑐𝑐 + 1 − 𝐼𝐼 𝑟𝑟, 𝑐𝑐 ;
take the absolute value; and then threshold using T = 20
15
The derivative approximation 𝐼𝐼𝑥𝑥 𝑟𝑟, 𝑐𝑐 = 𝐼𝐼 𝑟𝑟, 𝑐𝑐 + 1 − 𝐼𝐼 𝑟𝑟, 𝑐𝑐 can be
represented using a 2D kernel
• This isn’t much of a kernel – but it illustrates the fact that we can represent
derivative operations using our linear-filtering concepts
16
Consider some alternatives
0 0 0 0 0 0 -1 0 1 -1 0 1
1 1
0 -1 1 -1 0 1 -1 0 1 ×
3
-2 0 2 ×
4
0 0 0 0 0 0 -1 0 1 -1 0 1
17
Now consider the vertical direction
0 1 0 0 1 0 1 1 1 1 2 1
1 1
0 -1 0 0 0 0 0 0 0 ×
3
0 0 0 ×
4
0 0 0 0 -1 0 -1 -1 -1 -1 -2 -1
18
Some well-known templates for edge detection
− 1 0 1 1 2 1
− 2 0 2 0
Sobel 0 0
− 1 0 1 − 1 − 2 − 1
− 1 0 1 1 1 1
Prewitt − 1 0 1 0 0 0
− 1 0 1 − 1 − 1 − 1
Roberts 1 0 0 − 1
0 −1 1 0
img0 = ...
sobelx = cv2.Sobel(img0, cv2.CV_64F, 1, 0, ksize=3) # x
sobely = cv2.Sobel(img0, cv2.CV_64F, 0, 1, ksize=3) # y
https://www.bogotobogo.com/python/OpenCV_Python/python_opencv3_Image_Gradient_Sobel_Laplacian_Derivatives_Edge_Detection.php
19
-1 0 1 1 2 1
-2 0 2 0 0 0
Ix -1 0 1 Iy -1 -2 -1
20
We can combine
Original the 2 results:
image: I | Ix | + | I y |
21
Q: How should the partial derivatives be combined?
A: Consider the gradient vector:
I x
∇I =
I y
2 2
Magnitude of gradient: ∇I= Ix + I y
−1 Iy
Direction of gradient:
θ = tan
Ix
22
SOBEL
Magnitude
23
We can also examine the direction component of the Sobel gradient.
It naturally ranges from −𝜋𝜋 to 𝜋𝜋 radians, so we can scale it the range 0 to
255 for display; but remember, angle is a circular quantity!
24
We can also examine the direction component of the Sobel gradient.
It naturally ranges from −𝜋𝜋 to 𝜋𝜋 radians, so we can scale it the range 0 to
255 for display; but remember, angle is a circular quantity!
25
When is the angle component ever used?
Example: if you want an image consisting of strong edges that are
diagonal, then you could multiply the Sobel magnitude times the
difference of the angle from 45 degrees
26
CANNY EDGE DETECTION
27
A widely used method for edge detection
was developed by Canny in the mid-1980’s
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.420.3300&rep=rep1&type=pdf
28
Canny’s idea was to find edge pixels that satisfy a few criteria
on what a “good” representation of an edge would look like
– assuming a noisy image
29
The Canny procedure uses both an estimate of the gradient
and local neighborhood information to produce a skeleton representation
30
Here’s an example from
Canny’s paper
31
Some parameters must be set:
width of Gaussian, and upper and lower thresholds for the edge strength
32
First step: the image is smoothed using a Gaussian filter
– but what is the right 𝜎𝜎 to use?
33
Canny step 1: smooth the original image using a Gaussian kernel
– in this case, 𝜎𝜎 = 1.4
34
Canny step 2: approximate the gradient (usually with Sobel kernels);
both magnitude and direction are extracted
35
Canny step 3: compare the Sobel magnitude (edge strength) to each pixel
in the neighborhood in the direction of max gradient
and suppress (set to zero) all edge pixels that are not the maximum
36
Canny step 4: binarize the image, using a dual-threshold procedure
• First, threshold the “edge image” with an initial, higher threshold – any pixel with
edge intensity above this threshold is marked as an edge pixel
• Then, re-examine the edge image: any pixel with edge intensity above a second,
lower threshold that is a neighbor of a pixel that is above the first threshold is
also marked as an edge pixel
• What does it mean for 2 pixels to be “neighbors”?
For a 3x3 neighborhood, there are two kinds of connectivity (neighbors)
– usually, 8-connectivity is used
37
Canny step 5: optionally, perform edge tracking
to join shorter edge elements into longer curves
38
Example of Canny edge detection
39
40
Original image Canny example
41
Canny example
σ =1 σ =2
σ =3 σ =4
42
Some notes about the Canny edge detector
43
MORE EDGE DETECTORS
44
People have considered improvements to the Canny procedure
• Deriche (1987) replaced the Sobel with a different edge operator, designed to
optimize the Canny criteria
– He replaced a FIR (Finite Impulse Response) filter with an IIR (Infinite Impulse
Response) filter
– This has computational advantages but is less well-tempered on edges of all directions
45
“Compass” edge detection
− 1 0 1 0 1 2 1 2 1 2 1 0
− 2 0 2 − 1 0 1 0 0 0 1 0 − 1
− 1 0 1 − 2 − 1 0 − 1 − 2 − 1 0 − 1 − 2
0° 45° 90° 135°
• At each pixel location, compute responses to several
templates
• The largest magnitude determines the “winner”
(so, this is a nonlinear procedure)
• These are sometimes called “generalized Sobel masks”
46
Another “compass” example:
Nevatia-Babu masks
− 100 − 100 0 100 100 − 100 32 100 100 100 100 100 100 100 100
− 100 − 100 0 100 100 − 100 − 78 92 100 100
− 32
78 100 100 100
− 100 − 100 0 100 100 − 100 − 100 0 100 100 − 100 − 92 0 92 100
− 100 − 100 0 100 100 − 100 − 100 − 92 78 100 − 100 − 100 − 100 − 78 32
− 100 − 100 0 100 100 − 100 − 100 − 100 − 32 100 − 100 − 100 − 100 − 100 100
0° 30° 60°
100 100 100 100 100 100 100 100 100 100 100 100 100 32 − 100
100
100 100 100 100
100
100 100 78 − 32
100
100 92 − 78 − 100
0 0 0 0 0 100 92 0 − 92 − 100 100 100 0 − 100 − 100
− 100 − 100 − 100 − 100 − 100 32 − 78 − 100 − 100 − 100 100 78 − 92 − 100 − 100
− 100 − 100 − 100 − 100 − 100 − 100 − 100 − 100 − 100 − 100 100 − 32 − 100 − 100 − 100
47
USING 2ND DERIVATIVES
48
We have looked at 1st-derivative operators
What about using 2nd derivatives?
∂x ∂y
49
50
Result of Laplacian
51
The result of the Laplacian operator can be added back
to the original image to enhance the edges in the image
52
In 1979, Marr, Hildreth, and Poggio caused a lot of excitement when they
suggested the combination of Laplacian and Gaussian filtering
• Implement as:
𝐼𝐼𝑛𝑛𝑛𝑛𝑛𝑛 𝑥𝑥, 𝑦𝑦 = 𝛻𝛻 2 𝐼𝐼 𝑥𝑥, 𝑦𝑦 ∗ 𝐺𝐺 𝑥𝑥, 𝑦𝑦 = 𝛻𝛻 2 𝐼𝐼 𝑥𝑥, 𝑦𝑦 ∗ 𝐺𝐺 𝑥𝑥, 𝑦𝑦 = 𝐼𝐼 𝑥𝑥, 𝑦𝑦 ∗ 𝛻𝛻 2 𝐺𝐺 𝑥𝑥, 𝑦𝑦
53
2D edge detection filters
Laplacian of Gaussian
𝐺𝐺 𝜎𝜎 𝐺𝐺 𝜎𝜎 𝐺𝐺 𝜎𝜎
54
𝛻𝛻 2 𝐺𝐺, shown upside down
55
𝜕𝜕2
Laplacian of Gaussian: 𝐺𝐺 ∗ 𝑓𝑓
𝜕𝜕𝑥𝑥 2
Consider
𝐺𝐺 Laplacian of Gaussian
operator
𝐺𝐺 *
σ = 1 pixel σ = 3 pixels
σ =1 σ =2
σ =3 σ =4
58
59
Janusz Starzyk, Univ of Colorado
60
LGN: Lateral Geniculate Nucleus
61
62
Summary: Edge Detection
63