Skip to content

Beginners Opencv · Tutorials

Edge detection – OpenCV 3.4 with python 3 Tutorial 18

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

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.

One comment

  1. Hi.
    I want to recognize the Text from the video ..By Canny, I did the detection part but I want to print the text, and by the OCR I can’t do so .please help.

Join the discussion