Edge detection – OpenCV 3.4 with python 3 Tutorial 18
What you will learn about edge detection
The opencv edge detection is the image processing procedure, that allows you to structure the image pixels in order to show and highlight their edges.
With opencv this operation can be done with three methods:
-Sobel edge detection
-Laplacian edge detection
-Canny edge detection
What is the best method for our purposes? We will find out step by step in this article.
Sobel edge detection
Find the changes in contrast through vertical and horizontal intensity so let’s proceed with a small example of edge detection.
img= cv2.GaussianBlur(img, (5,5) , 0 ) sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0) sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1)
As a first step of the processing we applied a blur to try to eliminate noise and sharpen the post-processing image. As can be seen from the example, however, this procedure does not make the definition of the lines optimal anyway.
Laplacian edge detection
Another method used for image edge detection is Laplacian edge detection. The method can be invoked by using the following code.
laplacian = cv2.Laplacian(img, cv2.CV_64F, Ksize=5)
I want to bring your attention to Ksize (kernel size), this way you adjust the precision to get.
Here is an example image
Canny edge detection
Last and perhaps among the best of the methods proposed in relation to our example image (Panda).
It is simply invoked by the code I indicated. Explaining its configurations very quickly:
- 100 is the value below which lines are not recognized
- 150 is the value beyond which they are recognized
canny = cv2.Canny(img, 100 , 150)
This below is the result, as you can see it is the best of the methods described.
Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Freelancers to easily and efficiently build Computer Vision Software.
Learn to build Computer Vision Software easily and efficiently.
This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.Sign UP for FREE
Detect and Track any Object (Full Videocourse)
You can Build Software to detect and track any Object even if you have a basic programming knowledge.