Faculty of Information Technology
Ton Duc Thang University
August 2023
Edge Detection
Extracting The Edges From An Image
Objective
What is edge detection and how it can be helpful in image classification.
Learn how kernels are used to identify the edges in a given image.
https://www.analyticsvidhya.com/blog/2021/03/edge-detection-extracting-the-edges-from-an-image/
@2023 Pham Van Huy and Trinh Hung Cuong 2
Edge Detection: image classification
Can you differentiate between
the objects?
@2023 Pham Van Huy and Trinh Hung Cuong 3
Edge Detection: image classification
Can you still easily classify
the images?
@2023 Pham Van Huy and Trinh Hung Cuong 4
Edge Detection: image classification
removed the color, the background, and the other minute details from the
pictures
extract only the edges, we would still be able to classify the image.
@2023 Pham Van Huy and Trinh Hung Cuong 5
Edges
Edges are significant local changes of intensity in an image.
Edges typically occur on the boundary between two different regions in an
image (Trucco, Chapt 4 AND Jain et al., Chapt 5)
@2023 Pham Van Huy and Trinh Hung Cuong 6
Goal of edge detection
Produce a line drawing of a scene from an image of that scene
Important features can be extracted from the edges of an image (e.g.,
corners, lines, curves)
These features are used by higher-level computer vision algorithms (e.g.,
recognition)
@2023 Pham Van Huy and Trinh Hung Cuong 7
What causes intensity changes?
Geometric events
object boundary (discontinuity in depth and/or surface color and texture)
surface boundary (discontinuity in surface orientation and/or surface color and texture)
Non-geometric events
specularity (direct reflection of light, such as a mirror)
shadows (from other objects or from the same object)
inter-reflections
@2023 Pham Van Huy and Trinh Hung Cuong 8
Edge Detection
Identify the edges by looking at the numbers or the pixel values
there is a
significant
difference
between the pixel
values around the
edge
@2023 Pham Van Huy and Trinh Hung Cuong 9
Edge Detection (ct)
Edge detection is an image processing technique for finding the boundaries
of an object in the given image
Edges are the part of the image that represents the boundary or the shape of
the object in the image
the pixel values around the edge show a significant difference or a sudden change in the
pixel values
Based on this fact we can identify which pixels represent the edge or which
pixel lie on the edge.
@2023 Pham Van Huy and Trinh Hung Cuong 10
How to Extract the Edges
compare the pixel values with its surrounding pixels, to find out if a particular
pixel lies on the edge
use a matrix known as the kernel and perform the element-wise multiplication
@2023 Pham Van Huy and Trinh Hung Cuong 11
How to Extract the Edges (ct)
@2023 Pham Van Huy and Trinh Hung Cuong 12
How to Extract the Edges (ct)
@2023 Pham Van Huy and Trinh Hung Cuong 13
How to Extract the Edges (ct)
@2023 Pham Van Huy and Trinh Hung Cuong 14
How to Extract the Edges (ct)
@2023 Pham Van Huy and Trinh Hung Cuong 15
Prewitt & Sobel kernels
Sobel kernels, higher importance is
given to the pixel values right next
to the target pixel
@2023 Pham Van Huy and Trinh Hung Cuong 16
Prewitt kernels
Vertical direction -1 0 1
-1 0 1
-1 0 1
@2023 Pham Van Huy and Trinh Hung Cuong 17
Prewitt kernels (ct)
-1 -1 -1
Horizontal direction
0 0 0
1 1 1
@2023 Pham Van Huy and Trinh Hung Cuong 18
Prewitt kernel – both directions
@2023 Pham Van Huy and Trinh Hung Cuong 19
Ex. 1
Apply Prewitt/Sobel operators in X direction, Y direction, and both directions
into the following image:
@2023 Pham Van Huy and Trinh Hung Cuong 20
Edges again
An edge is a place of rapid change in the image intensity function
Points which lie on an edge can be detected by
• detecting local maxima or minima of the first
edges correspond to
derivative extrema of derivative
@2023 Pham Van Huy and Trinh Hung Cuong 21
Edges and derivate
derivatives only exists for continuous functions but the image is a discrete 2D
intensity function
approximated the image gradients using finite approximation as
https://theailearner.com/tag/prewitt-
operator/
prefer the central difference as
shown above
@2023 Pham Van Huy and Trinh Hung Cuong 22
Edges and derivate (ct)
obtain the derivative filter in x and y directions as shown below
• assumed that the x-coordinate is increasing in the “right”-direction, and y-coordinate in
the “down”-direction
• By weighting these x and y derivatives, we can obtain different edge detection filters
@2023 Pham Van Huy and Trinh Hung Cuong 23
Sobel Operator
multiplying the x, and y-derivative filters obtained above with some
smoothing filter(1D) in the other direction
For example, a 3×3 Sobel-x and Sobel-y filter can be obtained as
Gaussian filter is used for
blurring thus, the Sobel
operator computes the
gradient with smoothing
@2023 Pham Van Huy and Trinh Hung Cuong 24
Sobel operators (ct)
Convolve these Sobel operators with the image, they estimate the gradients
in the x, and y-directions(say Gx and Gy). For each point, we can calculate
the gradient magnitude and direction as
https://docs.opencv.org/3.4/d2/d2c/tutorial_sobel_derivatives.html
@2023 Pham Van Huy and Trinh Hung Cuong 25
Prewitt Operator
x, and y-derivative filters are weighted with the standard averaging filter as
shown below
@2023 Pham Van Huy and Trinh Hung Cuong 26
Prewitt operator example
1 0 1
1 0 1
1 0 1
Original
1024x710 1 1 1
0 0 0
1 1 1
Digital Image Processing: Bernd Girod, © 2013 Stanford
University -- Edge Detection 27
@2023 Pham Van Huy and Trinh Hung Cuong 27
Prewitt operator example (cont.)
Sum of squared threshold = 900 threshold = 4500 threshold = 7200
horizontal and
vertical gradients
(log display)
Digital Image Processing: Bernd Girod, © 2013 Stanford University -- Edge Detection 28
@2023 Pham Van Huy and Trinh Hung Cuong 28
Sobel operator example
Sum of squared threshold = 1600 threshold = 8000 threshold = 12800
horizontal and
vertical gradients
(log display)
Digital Image Processing: Bernd Girod, © 2013 Stanford University -- Edge Detection 29
@2023 Pham Van Huy and Trinh Hung Cuong 29
Canny Edge Detection
Canny Edge Detection is a popular edge detection algorithm. It was
developed by John F. Canny
It is a multi-stage algorithm:
Noise Reduction
Edge detection is susceptible to noise in the image, first step is to remove the noise in
the image with a Gaussian filter
https://docs.opencv.org/4.x/da/d22/tutorial_py_canny.html
@2023 Pham Van Huy and Trinh Hung Cuong 30
Canny Edge Detection (ct)
Finding Intensity Gradient of the Image
Smoothened image is then filtered with a Sobel kernel in both horizontal and vertical
direction
to get first derivative in horizontal direction (𝐺𝑥 ) and vertical direction (𝐺𝑦 )
find edge gradient and direction for each pixel (gradient magnitude and angle):
Gradient direction is always perpendicular to edges. It is rounded to one of four angles
representing vertical, horizontal and two diagonal directions.
@2023 Pham Van Huy and Trinh Hung Cuong 31
Canny Edge Detection (ct)
Non-maximum Suppression
remove any unwanted pixels which may not constitute the edge
at every pixel, pixel is checked if it is a local maximum in its neighborhood in the
direction of gradient
Its gradient magnitude is smaller than either of its neighbors?
Point A is on the edge ( in vertical direction).
Point B and C are in gradient directions. So
point A is checked with point B and C to see if it
forms a local maximum. If so, it is considered
for next stage, otherwise, it is suppressed ( put
to zero).
@2023 Pham Van Huy and Trinh Hung Cuong 32
Canny Edge Detection (ct)
Hysteresis Thresholding
This stage decides which are all edges are really edges and which are not
Use two threshold values, minVal and maxVal
Any edges with intensity gradient more than maxVal are sure to be edges and those
below minVal are sure to be non-edges
Those who lie between these two thresholds are classified edges or non-edges based
on their connectivity. If they are connected to "sure-edge«(strong edge) pixels, they
are considered to be part of edges. Otherwise, they are also discarded.
@2023 Pham Van Huy and Trinh Hung Cuong 33
Canny Edge Detection (ct)
The edge A is above the maxVal, so considered as "sure-edge". Although edge C is below
maxVal, it is connected to edge A, so that also considered as valid edge and we get that
full curve. But edge B, although it is above minVal and is in same region as that of edge
C, it is not connected to any "sure-edge", so that is discarded.
@2023 Pham Van Huy and Trinh Hung Cuong 34
Canny Edge Detection (ct)
Hysteresis Thresholding
This stage also removes small pixels noises on the assumption that edges are long lines
https://docs.opencv.org/4.x/da/d22/tutorial_py_canny.html
@2023 Pham Van Huy and Trinh Hung Cuong 35
Ex. 2
Apply Canny operator into the following image:
@2023 Pham Van Huy and Trinh Hung Cuong 36
Template Matching
a technique for finding areas of an image that match (are similar) to a
template image (patch)
compare the template image against the source image by sliding it
https://docs.opencv.org/3.4/de/da9/tutorial_template_matching.html
@2023 Pham Van Huy and Trinh Hung Cuong 37
Template Matching (ct)
moving the patch one pixel at a time (left to right, up to down)
At each location, a metric is calculated so it represents how "good" or "bad" the match at
that location is (or how similar the patch is to that particular area of the source image)
https://docs.opencv.org/3.4/d4/dc6/tutorial_py_template_matching.html
@2023 Pham Van Huy and Trinh Hung Cuong 38
OpenCV & Machine Learning
Machine Learning Library (MLL) is a set of classes and functions for
statistical classification, regression, and clustering of data
https://docs.opencv.org/4.x/dd/ded/group__ml.html
https://docs.opencv.org/4.x/d8/d4b/tutorial_py_knn_opencv.html
@2023 Pham Van Huy and Trinh Hung Cuong 39
OpenCV & Machine Learning (ct)
OCR of Hand-written Data using kNN
@2023 Pham Van Huy and Trinh Hung Cuong 40
References
1. https://docs.opencv.org/4.x/d4/d86/group__imgproc__filter.html
2. https://www.youtube.com/watch?app=desktop&v=kGHz-cEyjiE
3. https://docs.opencv.org/4.x/d4/dc6/tutorial_py_template_matching.html
4. https://www.youtube.com/watch?app=desktop&v=kGHz-cEyjiE
5. https://vincmazet.github.io/bip/filtering/convolution.html
@2023 Pham Van Huy and Trinh Hung Cuong 41