Edge Detection Using OpenCV _ LearnOpenCv
Edge Detection Using OpenCV _ LearnOpenCv
Edge detection is an image-processing technique that is used to identify the boundaries (edges) of objects or regions within an image. Edges are among the
most important features associated with images. We know the underlying structure of an image through its edges. Computer vision processing pipelines,
therefore, extensively use edge detection in applications.
https://learnopencv.com/edge-detection-using-opencv/ 1/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
First, take a look at the code that will demonstrate edge detection. Each line of code will be discussed in detail so that you understand it fully.
Python:
1 import cv2
2
3 # Read the original image
4 img = cv2.imread('test.jpg')
5 # Display original image
6 cv2.imshow('Original', img)
7 cv2.waitKey(0)
8
9 # Convert to graycsale
10 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
11 # Blur the image for better edge detection
12 img_blur = cv2.GaussianBlur(img_gray, (3,3), 0)
13
14 # Sobel Edge Detection
15 sobelx = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge Detection on the X axis
16 sobely = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge Detection on the Y axis
17 sobelxy = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5) # Combined X and Y Sobel Edge Detection
18 # Display Sobel Edge Detection Images
19 cv2.imshow('Sobel X', sobelx)
20 cv2.waitKey(0)
21 cv2.imshow('Sobel Y', sobely)
22 cv2.waitKey(0)
23 cv2.imshow('Sobel X Y using Sobel() function', sobelxy)
24 cv2.waitKey(0)
25
26 # Canny Edge Detection
27 edges = cv2.Canny(image=img_blur, threshold1=100, threshold2=200) # Canny Edge Detection
28 # Display Canny Edge Detection Image
29 cv2.imshow('Canny Edge Detection', edges)
30 cv2.waitKey(0)
31
32 cv2.destroyAllWindows()
C++:
1 #include <opencv2/opencv.hpp>
2 #include <iostream>
3 // i t llif f f ti () t d td f ti ()
https://learnopencv.com/edge-detection-using-opencv/ 2/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
We assume you already have OpenCV in your system. If you need to install OpenCV, please visit the relevant link below.
1. Install OpenCV on Windows (https://learnopencv.com/install-opencv-on-windows/)
2. Install OpenCV on MacOS (https://learnopencv.com/install-opencv-4-on-macos/)
3. Install OpenCV on Ubuntu (https://learnopencv.com/install-opencv-4-on-ubuntu-18-04/)
This tiger image will be used for all the examples here.
https://learnopencv.com/edge-detection-using-opencv/ 3/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
(https://learnopencv.com/wp-content/uploads/2021/06/input_image-1.jpg)
Before going into each algorithm in detail, let’s complete some preliminary steps for edge detection. Start by importing the OpenCV library, as shown in the code
below.
Python:
1 import cv2
Download Code To easily follow along this tutorial, please download code by clicking on the button below. It's FREE!
Download Code
C++:
1 #include<opencv2/opencv.hpp>
2 #include<iostream>
3
4 // Namespace nullifies the use of cv::function();
5 using namespace std;
6 using namespace cv;
The first step is to read in the image, using the imread() function in OpenCV.
Here, we read in the color image as a grayscale image because you do not need color information to detect edges. To learn more refer: Read, Display and Write
an Image using OpenCV (https://learnopencv.com/read-display-and-write-an-image-using-opencv/).
After reading the image, we also blur it, using the GaussianBlur() function. This is done to reduce the noise in the image. In edge detection, numerical
derivatives of the pixel intensities have to be computed, and this typically results in ‘noisy’ edges. In other words, the intensity of neighboring pixels in an image
(especially near edges) can fluctuate quite a bit, giving rise to edges that don’t represent the predominant edge structure we are looking for.
https://learnopencv.com/edge-detection-using-opencv/ 4/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
Python:
C++:
1 // Reading image
2 Mat img = imread("test.jpg",0);
3 // Blur the image for better edge detection
4 Mat img_blur;
5 GaussianBlur(img, img_blur, Size(3,3), SigmaX=0, SigmaY=0);
(https://learnopencv.com/wp-content/uploads/2021/06/pixel_intensity.png)
The rise in intensity is even more evident when we plot the first derivative of the intensity function.
(https://learnopencv.com/wp-content/uploads/2021/06/first_derivative.png)
The above plot demonstrates that edges can be detected in areas where the gradient is higher than a particular threshold value. In addition, a sudden change in
the derivative will also reveal a change in the pixel intensity. With this in mind, we can approximate the derivative using a 3×3 kernel. We use one kernel to detect
sudden changes in pixel intensity in the X direction and another in the Y direction. (To learn more, check this post on image filtering and convolution kernels
(https://learnopencv.com/image-filtering-using-convolution-in-opencv/).
https://learnopencv.com/edge-detection-using-opencv/ 5/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
(1)
X-Direction Kernel
(2)
Y-Direction Kernel
When these kernels are convolved with the original image, you get a ‘Sobel edge image’.
If we use only the Vertical Kernel, the convolution yields a Sobel image, with edges enhanced in the X-direction
Using the Horizontal Kernel yields a Sobel image, with edges enhanced in the Y-direction.
Let and represent the intensity gradient in the and directions respectively. If and denote the X and Y kernels defined above:
(3)
(4)
where denotes the convolution operator, and represents the input image.
(5)
(6)
The following is the syntax for applying Sobel edge detection using OpenCV:
The parameter ddepth specifies the precision of the output image, while dx and dy specify the order of the derivative in each direction. For example:
If dx=1 and dy=0, we compute the 1st derivative Sobel image in the x-direction.
If both dx=1 and dy=1, we compute the 1st derivative Sobel image in both directions
Python:
https://learnopencv.com/edge-detection-using-opencv/ 6/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
1 # Sobel Edge Detection Click here to download the source code to this post
2 sobelx = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge Detection on the X axis
3 sobely = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge Detection on the Y axis
4 sobelxy = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5) # Combined X and Y Sobel Edge Detection
5
6 # Display Sobel Edge Detection Images
7 cv2.imshow('Sobel X', sobelx)
8 cv2.waitKey(0)
9
10 cv2.imshow('Sobel Y', sobely)
11 cv2.waitKey(0)
12
13 cv2.imshow('Sobel X Y using Sobel() function', sobelxy)
14 cv2.waitKey(0)
C++:
The results are shown in the figures below. See how the Sobel image in the x-direction predominantly identifies vertical edges (i.e. those whose gradient is
largest in the x-direction, horizontal). And that in the y-direction identifies horizontal edges (i.e. those whose gradient is largest in the y-direction, vertical). Look
closely at the tiger’s stripes in both images. Note how the strong vertical edges of the stripes are more evident in the Sobel image in the x-direction.
https://learnopencv.com/edge-detection-using-opencv/ 7/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
(https://learnopencv.com/wp-content/uploads/2021/06/sobel_x.jpg)
(https://learnopencv.com/wp-content/uploads/2021/06/sobel_y.jpg)
The figure below shows the Sobel image for the gradient in both directions, which distills the original image into an edge structure representation, such that its
https://learnopencv.com/edge-detection-using-opencv/ 8/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
(https://learnopencv.com/wp-content/uploads/2021/06/sobel_xy.jpg)
1. Noise Reduction
2. Calculating the Intensity Gradient of the Image
3. Suppression of False Edges
4. Hysteresis Thresholding
Noise Reduction
Raw image pixels can often lead to noisy edges, so it is essential to reduce noise before computing edges In Canny Edge Detection, a Gaussian blur filter is used
to essentially remove or minimize unnecessary detail that could lead to undesirable edges. Have a look at the tiger in the two images below; Gaussian blur has
been applied to the image to the right. As you can see, it appears slightly blurred but still retains a significant amount of detail from which edges can be
computed. Click on this link to learn more about blurring (https://learnopencv.com/image-filtering-using-convolution-in-opencv/).
https://learnopencv.com/edge-detection-using-opencv/ 9/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
(https://learnopencv.com/wp-content/uploads/2021/06/orig_gauss_blur.jpg)
(7)
(8)
The gradient direction is then rounded to the nearest 45-degree angle. The figure below (right) shows the result of this combined processing step.
(https://learnopencv.com/wp-content/uploads/2021/06/original_after_sobel.jpg)
https://learnopencv.com/edge-detection-using-opencv/ 10/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
(https://learnopencv.com/wp-content/uploads/2021/06/sobel_non_max.jpg)
If the gradient magnitude value is higher than the larger threshold value, those pixels are associated with solid edges and are included in
the final edge map.
If the gradient magnitude values are lower than the smaller threshold value, the pixels are suppressed and excluded from the final edge
map.
All the other pixels, whose gradient magnitudes fall between these two thresholds, are marked as ‘weak’ edges (i.e. they become
candidates for being included in the final edge map).
If the ‘weak’ pixels are connected to those associated with solid edges, they are also included in the final edge map.
The following is the syntax for applying Canny edge detection using OpenCV:
In the code example below, the Canny() function implements the methodology described above. We supply the two thresholds used by the Canny Edge
Detection algorithm, and OpenCV handles all the implementation details. Don’t forget to blur the image before calling the Canny() function. It is a highly-
recommended preprocessing step. To learn more about the optional arguments, please refer to the OpenCV documentation
(https://docs.opencv.org/3.4/dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de) page.
Python:
C++:
https://learnopencv.com/edge-detection-using-opencv/ 11/21
9/11/23, 9:20 AM Edge Detection Using OpenCV | LearnOpenCV #
Here are the final results: We use a lower threshold of 100, and an upper threshold of 200. As you can see, the algorithm has identified the predominant edges in
the image, eliminating in the process those less important to the overall structure. However, the results can easily be adjusted, so experiment with different
images, vary the amount of blurring, and try different threshold values to get a feel for things yourself.
(https://learnopencv.com/wp-content/uploads/2021/06/canny_edge.png)
Regarding performance, Canny Edge Detection produces the best results because it uses not only Sobel Edge Detection but also Non-Maximum Suppression
and Hysteresis Thresholding. This provides more flexibility in how edges are identified and connected in the final stages of the algorithm.
Summary
We discussed what makes edge detection an important image-processing technique and focused on knowing its two most important algorithms (Sobel Edge
Detection and Canny Edge Detection). While demonstrating their use in OpenCV, we highlighted why blurring is an important preprocessing step.
You also saw how Canny Edge Detection actually uses the Sobel operator to compute numerical derivatives. And is robust and flexible, using even Non-
Maximum Suppression and Hysteresis Thresholding to maximum advantage. Finally, you understood why Canny Edge Detection is the preferred and most
widely used method for performing edge detection.
You can find all the codes discussed in this post at this link →Edge Detection Using OpenCV Colab Notebook
(https://colab.research.google.com/drive/16_QE16GQgCZTzfWmjnH49sadoKkcMoE9?usp=sharing).
https://learnopencv.com/edge-detection-using-opencv/ 12/21