Learn Canny Edge Detection with OpenCV canny() function

Introduction

Edge detection is a fundamental problem in image processing. Opencv offers a function Canny() that helps to detect edges of the image. Canny edge detection is a multi-step algorithm that can detect edges. This algorithm consists of four stages –

  1. Noise Reduction
  2. Gradient Computation
  3. Non-Max Suppression
  4. Hysteresis Thresholding
Let us see these steps in more detail.

1. Noise Reduction

Edge detection is sensitive towards image with noise. If the noise is not removed from the image then the edge detection results will not be good. So the first step is the preprocessing of the image to eliminate noise. This can be done by applying Gaussian filters.
We have already seen that Opencv provides functions for Gaussian filters. You can check that out at the below link –

2. Gradient Computation

Edges correspond to a change of pixels’ intensity. To detect it, the easiest way is to apply filters that highlight this intensity change in both directions: horizontal (x) and vertical (y)

So in this step, 2-D first derivative operator like Sobel operator is applied to the smoothened image in both horizontal as well as vertical directions. This will help to get the first derivative in the horizontal direction (G_x) and vertical direction (G_y) to highlight regions of the image with high first spatial derivatives.

Edges give rise to ridges in the gradient magnitude image. The Gradient Computation detects the edge intensity and direction by calculating the gradient.

Screenshot%20from%202020-03-26%2008-34-46.png

3. Non-Max Suppression

After doing the above gradient computation steps the thickness of the edge is not uniform. Some of the edges are thick while others are thin and also there are unwanted pixels that may not constitute images. So to overcome this issue non-max suppression is used.

In this step, for each pixel, it is checked if it is a local maximum in its neighborhood in the direction of the gradient.

Canny Edge Detection - Non Max Suppression
Canny Edge Detection – Non Max Suppression
As we see here in the image, point A is on the edge, and points B and C are on the gradient direction. Since gradient direction is always perpendicular to the edge, so point A is checked with points B and C. If it forms a local maximum, it is considered for the next stage, otherwise, it is suppressed i.e the pixel value put to zero.
In simple words, let us assume I(m,n) is an image –

then if I(m,n) is greater than its two neighbors along the gradient direction theta(m,n) then keep I(m,n) unchanged, otherwise, set it to 0.

4. Hysteresis Thresholding

This step is used to identify pixels that are all contributing to the edges are really edges and which are not.
To do this we need to have two threshold T1(High Threshold) and T2(Low Threshold).
  1. Any edges with intensity gradient more than High threshold are sure to be edges.
  2. Any edges below minVal are sure to be non-edges, so discarded.
  3. Those who lie between these two thresholds are classified edges or non-edges based on their connectivity. i.e if they are connected to “sure-edge” pixels, they are considered to be part of edges. Otherwise, they are also discarded.
Canny Edge Detection - Hysteresis Thresholding
Canny Edge Detection – Hysteresis Thresholding
Let us take the above example to understand it clearly.
  1. Edge A is above the High Threshold(T1). so it is considered as “sure-edge”.
  2. Although edge C is below High THreshold, it is connected to edge A. So that is also considered as a valid edge and we get that full curve.
  3. Although Edge B is above Low Threshold and is in the same region as that of edge C, it is not connected to any “sure-edge”, so that is discarded.

So it is very important that we have to select Low Threshold and High Threshold accordingly to get the correct result.

Note: The canny edge detector algorithm is based on grayscale pictures.

[adrotate banner=”3″]

Some important points

The effect of the Canny operator is determined by three parameters – i) Width of the Gaussian kernel used in the smoothing phase, ii) the upper threshold and iii) lower thresholds used by the tracker.

So if we increase the width of the Gaussian kernel then it reduces the detector’s sensitivity to noise, at the expense of losing some of the finer detail in the image. The localization error in the detected edges also increases slightly as the Gaussian width is increased.

The upper tracking threshold can be set quite high, and the lower threshold quite low for good results. Setting the lower threshold too high will cause noisy edges to break up. On the other hand, setting the upper threshold too low increases the number of spurious and undesirable edge fragments appearing in the output.

OpenCV Canny Edge Detection: cv2.Canny()

OpenCV provides a function Canny() to detect the edges of the image.

Syntax

cv2.Canny(image, edges, Low_Threshold,High_Threhold, apertureSize,L2gradient )

 

  • image −input image for this operation.
  • edges −It represents the destination (edges) for this operation.
  • Low_Threshold − It represents the Low threshold for the hysteresis procedure.
  • High_Threshold − It represents the High threshold for the hysteresis procedure.
  • apertureSize – aperture size for the Sobel() operator.It is the size of Sobel kernel used for find image gradients. By default it is 3.
  • L2gradient :L2gradient which specifies the equation for finding gradient magnitude. If it is True, it uses the equation mentioned above which is more accurate, otherwise it uses this function: Edge_Gradient(G) = |G_x| + |G_y|. By default, it is False.

Importing OpenCV library

In [1]:
import cv2
In [2]:
#read image
img=cv2.imread("lena.png")
In [3]:
#Display image
window_name='imagefirst'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Opencv Canny Edge Detection - Sample Image
Opencv Canny Edge Detection – Sample Image

Opencv canny() function does not do Gaussian blurring to remove noise from the image. Hence we will have to carry out this step as a prerequisite by using another opencv function GaussianBlur()

In [4]:
#Gaussian Blurring
gaussian_blur = cv2.GaussianBlur(img,(5,5),sigmaX=0)
In [5]:
#Display image
window_name='Gaussian_Blur'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,gaussian_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
Canny Edge Detection - Gaussian Blur
Canny Edge Detection – Gaussian Blur
In [6]:
#Canny Edge Detector
edges = cv2.Canny(gaussian_blur,100,200)
In [7]:
#Display image
window_name='Gaussian_Blur'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

cv2.Canny() - Edge Detection Output
cv2.Canny() – Edge Detection Output

Conclusion

In this tutorial, we understood how the canny edge detection technique works and how opencv canny() function can be used to achieve the same.

Click here to download the code

Reference – https://docs.opencv.org/master/d6/d00/tutorial_py_root.html

  • Sachin Mohan

    My name is Sachin Mohan, an undergraduate student of Computer Science and Engineering. My area of interest is ‘Artificial intelligence’ specifically Deep learning and Machine learning. I have attended various online and offline courses on Machine learning and Deep Learning from different national and international institutes My interest toward Machine Learning and deep Learning made me intern at ISRO and also I become the 1st Runner up in TCS EngiNX 2019 contest. I always love to share my knowledge and experience and my philosophy toward learning is "Learning by doing". So thats why I believe in education which have include both theoretical as well as practical knowledge.

    View all posts

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *