Introduction: Edge Detection Using Canny Edge Algorithm in Python

Hello! In this tutorial we will see how to detect edges in an image using canny edge detection algorithms in python using openCV library. If your new to this technique then don't worry, at the end of this guide you will be able to program and detect edges of an image.

Following are the functions of openCV which you will be learning in this tutorial:

1. How to read an image

2. How to create Track Bars

3. What is thresolding

3. How to apply edge detection alogorithm

4. And then your final output


  • Python 3
  • openCV library
  • numpy library

Step 1: Importing Libraries

First step will be importing our libraries

1. Including openCV library. It is called cv2 in python

Step 2: Canny Edge Detection Algorithm

For canny edge detection we need 3 things:

1. An image

2. Lower threshold value

3. Upper threshold value

What is threshold?

The thresholding stage in algorithm decides which are ages and which are not. So for this we need two values, one lower threshold value and one upper threshold value. Any value which is more than upper threshold value are sure to be edges and those which are less than lower threshold value are not edges as shown in figure where "A" is the upper value and "B" is the lower value. And those who lie between lower and upper threshold can be edges or non-edges depending on the connectivity with the upper and lower threshold values.

So the flow of the program would be as follow:

1. Load image

2. Resize image

3. Create Trackbars (To vary threshold values)

4. Getting Trackbars position

5. Applying edge detection algorithms

Step 3: Importing Image and Creating Trackbars

  • Defining a function named "nothing" which we will use later on
  • cv2.imread is used to load the image and storing it in variable name "img"
  • The image was too big so i am resizing that image using cv2.resize function in which 800 is width and 400 is height of an image (You can give any dimension you want)
  • cv2.namedWindow("canny") --> This line of code is used to create a new output window and name of the window is given as canny(You can give any name you want)
  • After that I am making a variable switch and inside that '0 : OFF \n1 : ON' this means if my trackbar will be on 1 then only I can adjust the values.
  • Now we will be creating 2 TrackBars, one for lower value and one for upper value. We will be using createTrackbar function of openCV. First we will see the syntax of this function.
    cv2.createTrackbar('WINDOWNAME','MAINWINDOWNAME','RANGE'). This might be confusing but don't worry we will go through each and every step.

  • Creating TrackBar for lower value:
    cv2.createTrackbar('lower','canny',0,255,nothing). In this lower is the trackbar name, canny is the main window, 0 is the position on which our slider will be and 255 is the range means the silder will move from 0-255

  • In similar way you can create other two trackbars

Step 4: Getting Trackbar Position

Ok so now we are going to read trackbar bar values so we can apply it to our image. We will get the values using cv2.getTrackbarPos() function.

lower = cv2.getTrackbarPos('lower', 'canny')

In the above statement I am creating a variable name lower in which I will store the value of lower value. So inside cv2.getTrackbarPos 1st argument would be "lower" because I want lower values (The spelling must be exactly same as it is createTrackbar function) and 2nd argument would be the name of the trackbar window to which it belongs.
Repeat the same process for rest of the functions as shown in the above image

Step 5: Applying Canny Edge Algorithm

Over here I am using and 'if-else' condition.

If the the value of switch is zero then I cannot adjust the values and if the value of switch is one then I can adjust the values.It simply works as a switch.

So if s == 0, keep it as it is and if s==1, apply algorithm. So the algorithm is,

edges = cv2.Canny(res, lower, upper)

The function is cv2.Canny() in which there are 3 arguments

1. Variable where the image is stored

2. Lower threshold value

3. Upper threshold value

And after that I am simply displaying the image using cv2.imshow() function

And at the end there is a delay. The output of the program is shown in the above video