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.

Sobel image edge detection

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

laplacian edge detection

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.

Blueprint

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