Lecture 9-Edge Detection
Lecture 9-Edge Detection
IMAGE PROCESSING
INSTRUCTOR: DR. NGUYEN NGOC TRUONG MINH
SCHOOL OF ELECTRICAL ENGINEERING, INTERNATIONAL UNIVERSITY (VNU-HCMC)
LECTURE IX –
EDGE DETECTION
INSTRUCTOR: DR. NGUYEN NGOC TRUONG MINH
SCHOOL OF ELECTRICAL ENGINEERING, INTERNATIONAL UNIVERSITY (VNU-HCMC)
LECTURE CONTENT
• What is edge detection and why is it so important to computer vision?
• What are the main edge detection techniques and how well do they work?
• What is the Hough transform and how can it be used to postprocess the
results of an edge detection algorithm?
• Problems
Lecture 9 – Edge Detection 4
• The need for edge detection algorithms as part of a vision system also has its
roots in biological vision: there is compelling evidence that the very early
stages of the human visual system (HVS) contain edge-sensitive cells that
respond strongly (i.e., exhibit a higher firing rate) when presented with
edges of certain intensity and orientation.
• When the input image is corrupted by noise, the first and second derivatives
respond quite differently. Even modest noise levels—barely noticeable when
you look at the original image or its profile—can render the second
derivative results useless, whereas more pronounced noise levels will also
impact the first derivative results to a point that they cannot be used for
edge detection.
Lecture 9 – Edge Detection 12
In M ATLAB
The IPT has a function for edge
detection (edge) whose variants
and options will be explored
throughout the tutorial.
Lecture 9 – Edge Detection 14
−1 0 1 −1 − 2 − 1
ℎ𝑥 = −2 0 2 ℎ𝑦 = 0 0 0
−1 0 1 1 2 1
Lecture 9 – Edge Detection 17
• As you may have noticed, despite their differences, the 3x3 masks
presented so far share two properties:
➢ They have coefficients of opposite signs (across a row or column of
coefficients equal to zero) in order to obtain a high response in image
regions with variations in intensity (possibly due to the presence of an
edge).
➢ The sum of the coefficients is equal to zero, which means that when applied
to perfectly homogeneous regions in the image (i.e., a patch of the image
with constant gray level), the result will be 0 (black pixel).
Lecture 9 – Edge Detection 18
In M ATLAB
The IPT function edge has options for both Prewitt and Sobel operators.
Edge detection using Prewitt and Sobel operators can also be achieved by
using imfilter with the corresponding 3x3 masks (which can be created
using fspecial).
Lecture 9 – Edge Detection 19
EXAMPLE 9.1
• Figure 9.4 shows an example of using imfilter to apply the Prewitt edge
detector to a test image. Due to the fact that the Prewitt kernels have
both positive and negative coefficients, the resulting array contains negative
and positive values.
• Since negative values are usually truncated when displaying an image in
MATLAB, the partial results (Figure 9.4b and c) have been mapped to a
modified gray-level range (where the highest negative value becomes black,
the highest positive value is displayed as white, and all zero values are
shown with a midlevel gray).
Lecture 9 – Edge Detection 20
EXAMPLE 9.1
• The combined final result (Figure 9.4d) was obtained by computing the
magnitude of the gradient, originally defined as
𝑔= 𝑔𝑥2 + 𝑔𝑦2
which can be approximated by
𝑔 = 𝑔𝑥 + 𝑔𝑦
Lecture 9 – Edge Detection 21
EXAMPLE 9.1
Lecture 9 – Edge Detection 22
EXAMPLE 9.2
• Figure 9.5 shows examples of edge detection using imfilter to apply the
Sobel operator on a grayscale image.
• All results are displayed in their negative (using imcomplement) for better
viewing on paper.
• The idea of using horizontal and vertical masks used by the Prewitt and
Sobel operators can be extended to include all eight compass directions:
north, northeast, east, southeast, south, southwest, west, and northwest.
• The Kirsch (Figure 9.6) and the Robinson (Figure 9.7) kernels are two
examples of compass masks.
Lecture 9 – Edge Detection 23
EXAMPLE 9.2
Lecture 9 – Edge Detection 24
EXAMPLE 9.2
Lecture 9 – Edge Detection 25
EXAMPLE 9.2
Lecture 9 – Edge Detection 26
EXAMPLE 9.2
EXAMPLE 9.2
Lecture 9 – Edge Detection 28
In M ATLAB
• Edge detection using the Laplacian operator can be implemented using the
fspecial function (to generate the Laplacian 3x3 convolution mask) and the
zerocross option in function edge as follows:
h = fspecial(’laplacian’,0);
J = edge(I,’zerocross’,t,h);
where t is a user-provided sensitivity threshold.
Lecture 9 – Edge Detection 30
EXAMPLE 9.3
• Figure 9.9 shows the results of applying the zero-cross edge detector to
an image and the impact of varying the thresholds.
• Part (a) shows a clean input image; part (b) shows the results of edge
detection in (a) using default parameters, whereas part (c) shows the
effects of reducing the threshold to 0. Clearly the result in (b) is much
better than (c).
• Part (d) is a noisy version of (a) (with zero-mean Gaussian noise with 𝜎 =
0.0001). Parts (e) and (f) are the edge detection results using the noisy
image in part (d) as an input and the same options as in (b) and (c),
respectively. In this case, although the amount of noise is hardly noticeable
in the original image (d), both edge detection results are unacceptable.
Lecture 9 – Edge Detection 31
EXAMPLE 9.3
Lecture 9 – Edge Detection 32
EXAMPLE 9.4
• Figure 9.11 shows the results of applying the LoG edge detector to an
image, and the impact of varying σ.
• Part (a) shows the input image; part (b) shows the results of edge
detection in (a) using default parameters (i.e., σ = 2), whereas parts (c)
and (d) show the effects of reducing or increasing sigma (to 1 and 3,
respectively).
• Reducing σ causes the resulting image to contain more fine details,
whereas an increase in σ leads to a coarser edge representation, as
expected.
Lecture 9 – Edge Detection 35
EXAMPLE 9.4
Lecture 9 – Edge Detection 36
• The Canny edge detector is one of the most popular, powerful, and effective
edge detection operators available today. Its algorithm can be described as
follows:
1. The input image is smoothed using a Gaussian low-pass filter, with a specified value
of σ: large values of σ will suppress much of the noise at the expense of weakening
potentially relevant edges.
2. The local gradient (intensity and direction) is computed for each point in the
smoothed image.
3. The edge points at the output of step 2 result in wide ridges.The algorithm thins
those ridges, leaving only the pixels at the top of each ridge, in a process known as
nonmaximal suppression.
Lecture 9 – Edge Detection 37
• The Canny edge detector is one of the most popular, powerful, and effective
edge detection operators available today. Its algorithm can be described as
follows:
4. The ridge pixels are then thresholded using two thresholds Tlow and Thigh: ridge pixels
with values greater than Thigh are considered strong edge pixels; ridge pixels with
values between Tlow and Thigh are said to be weak pixels.This process is known as
hysteresis thresholding.
5. The algorithm performs edge linking, aggregating weak pixels that are 8-connected to
the strong pixels.
Lecture 9 – Edge Detection 38
In M ATLAB
• The edge function includes the Canny edge detector, which can be invoked
using the following syntax:
J = edge(I, ’canny’,T, sigma);
where I is the input image, T = [T_lowT_high] is a1x2 vector containing the
two thresholds explained in step 4 of the algorithm, sigma is the standard
deviation of the Gaussian smoothing filter, and J is the output image.
Lecture 9 – Edge Detection 39
EXAMPLE 9.5
• Figure 9.12 shows the results of applying the Canny detector to an image
(Figure 9.5a), and the impact of varying σ and the thresholds. Part (a) uses
the syntax BW = edge(J,’canny’);, which results in t = [0.0625 0.1563] and σ
= 1. In part (b), we change the value of σ (to 0.5) leaving everything else
unchanged.
Lecture 9 – Edge Detection 40
EXAMPLE 9.5
EXAMPLE 9.5
Lecture 9 – Edge Detection 42
• Since each point in the image will map to a line in the transform domain,
repeating the process for other points will result in many intersecting
lines, one per point (Figure 9.14).
IN M AT L A B
• The IPT contains a function for Hough transform calculations, hough,
which takes a binary image as an input parameter, and returns the
corresponding Hough transform matrix and the arrays of ρ and θ values
over which the Hough transform was calculated. Optionally, the
resolution of the discretized 2D array for both ρ and θ can be specified
as additional parameters.
Lecture 9 – Edge Detection 51
EXAMPLE 9.6
• In this example, we use the hough function to find the strongest lines in a
binary image obtained as a result of an edge detection operator (BW),
using the following steps:
[H,T,R] = hough(BW,’RhoResolution’,0.5,’ThetaResolution’,0.5);
• Figure 9.16 shows the original image and the results of the Hough
transform calculations.You will notice that some of the highest peaks in
the transform image (approximately at θ =−60◦ and θ = 60◦) correspond
to the main diagonal lines in the scissors shape.
Lecture 9 – Edge Detection 52
EXAMPLE 9.6
Lecture 9 – Edge Detection 53
EXAMPLE 9.6
IN MATLAB
• The IPT also includes two useful companion functions for exploring and
plotting the results of Hough transform calculations: houghpeaks (which
identifies the k most salient peaks in the Hough transform results, where
k is passed as a parameter) and houghlines, which draws the lines
associated with the highest peaks on top of the original image.
Lecture 9 – Edge Detection 54
EXAMPLE 9.7
EXAMPLE 9.7
Lecture 9 – Edge Detection 56
Goa l
• The goal of this tutorial is to learn how to implement edge detection and
associated techniques in MATLAB.
Objectives
• Learn how to use the IPT edge function.
• Explore the most popular first-derivative edge detectors: Roberts, Sobel,
and Prewitt.
• Explore the Marr–Hildreth Laplacian of Gaussian edge detector.
• Explore the Canny edge detector.
• Learn how to implement edge detection with compass masks (Kirsch and
Robinson).
Lecture 9 – Edge Detection 57
18. Use a threshold value higher than the one in variable thresh.
[I_can2,thresh] = edge(I, ’canny’, 0.4);
subplot(2,2,3), imshow(I_can2), title(’Canny, thresh = 0.4’);
19. Use a threshold value lower than the one in variable thresh.
[I_can2,thresh] = edge(I, ’canny’, 0.08);
subplot(2,2,4), imshow(I_can2), title(’Canny, thresh = 0.08’);
Question 13. How does the sensitivity of the Ca nny edge detector
cha nge when the threshold va lue is increa sed?
Lecture 9 – Edge Detection 70
Previously, when we were using the edge function, we did not need to
convert the image to class double because the function took care of this for
us automatically.
Since now we are implementing the remaining edge detectors on our own,
we must perform the class conversion to properly handle negative values
(preventing unwanted truncation).
Next, we will define the eight Kirsch masks. For ease of implementation, we
will store all eight masks in a 3 × 3 × 8 matrix. Figure 9.18 illustrates this
storage format.
Lecture 9 – Edge Detection 72
Question 17. Why a re we required to sca le the ima ge when displa ying it?
In the previous step, we scaled the result for display purposes. If we wish to
threshold the image (as we did with all previous edge detectors), we must first
scale the image so that its values are within the range [0, 255] as well as
convert to class uint8.To do so, we can create a linear transformation function
that maps all current values to values within the range we want.
26. Create a transformation function to map the image to the grayscale range
and perform the transformation.
m = 255 / (max(I_kir(:)) - min(I_kir(:)));
I_kir_adj = uint8(m * I_kir);
figure, imshow(I_kir_adj);
Lecture 9 – Edge Detection 76
Question 18. Why is it not necessa ry to sca le this ima ge (I_kir_a dj)
when displa ying it?
Question 19. M a ke a copy of the ma ndrill ima ge a nd a dd Ga ussia n
noise to it. Then perform the Kirsch edge detector on it. Comment on
its performa nce when noise is present.
E dge Detection with the Robinson Opera tor
The Robinson edge detector can be implemented in the same manner as
the Kirsch detector. The only difference is the masks.
Lecture 9 – Edge Detection 77
29. Calculate the max of all eight images and display the result.
I_rob = max(I_r,[],3);
figure, imshow(I_kir,[]);
Question 20. How does the Robinson edge detector compa re with the
Kirsch detector?
Lecture 9 – Edge Detection 80
PROBLEMS
Problem 3. Show that the LoG edge detector can be implemented using
fspecial and imfilter (instead of edge) and provide a reason why this
implementation may be preferred.
Lecture 9 – Edge Detection 84
END OF LECTURE 9