What is Canny Edge Detection?

Simarpreet Singh
Simply Dev
Published in
7 min readApr 8, 2020

--

Hola people. While utilising this quarantine time, I am exploring a very famous python package and library : ‘ OpenCV ’, a library for doing operations related to or on the images, and learning about various function this library contains. Canny Edge algorithm is one of it.

Theory

Canny Edge Detection

It is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images . It was developed by John F. Canny ,an Australian computer scientist, back in 1986.

The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images. It was developed by John F. Canny in 1986. Canny also produced a computational theory of edge detection explaining why the technique works. (Wikipedia)

  • The Canny edge detection algorithm is composed of 5 steps:
  1. Noise reduction;
  2. Gradient calculation;
  3. Non-maximum suppression;
  4. Double threshold;
  5. Edge Tracking by Hysteresis.

lets us discuss the idea about how it works

1). Input image

We give the image to the method in RGB, before starting the main processing of methods; we need to have some pre-processing to prepare the given image to the methods. These will be explained in the next steps clearly.

2). converting the image to grayscale

In this step we convert the RGB image to grayscale, we can use the built-in function for passing this step.

3). Smoothing the image

Smoothing of image is the next step of the image for noise reduction;Gradient is the first order derivatives of image for each direction. It is cause of edges that seems more and the edges look thick. The gradient can be computed using central difference: it is preprocessing work to prepare the image for edge detection. we use the blur filtering of Gaussian to smoothing the image. Convolve the input image with Gaussian kernel(filter) in order to remove high-freq noises of the image.

4). Image gradient

Gradient is the function of the partial derivatives. I applied to the image convolution process with Sobel filters to obtain this partial derivative. The code for convolution has been written in the ‘convolution.m’. I have applied Sobel filters to convolution function in vertical and horizontal axes of image.

5). non-maximum suppression

This step is deciding whether a point is a local maximum of the interpolated gradient magnitude in the direction of the gradient or not, this step has a significant effect on the performance of edge. In this step the pixel is compared with its two neighbors of the pixel, if the compared pixel is larger than neighbor we do not change the pixel, otherwise, this pixel is not maximum value hence, we set the zero to that pixel.

6). Tracking the edge by hysteresis

In this step we choose two type of threshold, high and low threshold value. Afterward, each pixel of image is compared with two different threshold value. If the pixel is larger than the high threshold, this pixel mark with 255 in the final image. If the pixel between high threshold and low threshold. If the pixel is smaller than low-threshold image, mark as black with 0 (black) value in the resulting image.

7). Final Results

After passing all of the mentioned steps, we will give the final result from the method. after comparing the results with the built-in function of Matlab for edge detection, we can reach an agreement that this result is very similar to that function, all in all built-in function has higher performance due to choose the good threshold of the algorithm.

this shows all the results of function we are using, from grayscale image, SobelX, SobelY, Sobel’s combination, Laplacian.

These are all the results of functions we used and discussed above.

<script src=”https://gist.github.com/simarpreetsingh-019/cc90a76bba7989c27a5b522607cc419a.js"></script>

link for above code :point_right:

Let us see some mathematics behind this.

1). Noise Reduction

Since the mathematics involved behind the scene are mainly based on derivatives (cf. Step 2: Gradient calculation), edge detection results are highly sensitive to image noise.

One way to get rid of the noise on the image, is by applying Gaussian blur to smooth it. To do so, image convolution technique is applied with a Gaussian Kernel (3x3, 5x5, 7x7 etc…). The kernel size depends on the expected blurring effect. Basically, the smallest the kernel, the less visible is the blur. In our example, we will use a 5 by 5 Gaussian kernel.

The equation for a Gaussian filter kernel of size (2k+1)×(2k+1) is given by:

Gaussian filter kernel equation

2). Finding Intensity Gradient of the Image

Smoothened image is then filtered with a Sobel kernel in both horizontal and vertical direction to get first derivative in horizontal direction (

) and vertical direction (

).

From these two images, we can find edge gradient and direction for each pixel as follows:

Gradient direction is always perpendicular to edges. It is rounded to one of four angles representing vertical, horizontal and two diagonal directions.

3). Non-maximum Suppression

After getting gradient magnitude and direction, a full scan of image is done to remove any unwanted pixels which may not constitute the edge. For this, at every pixel, pixel is checked if it is a local maximum in its neighborhood in the direction of gradient.

Point A is on the edge ( in vertical direction). Gradient direction is normal to the edge. Point B and C are in gradient directions. So point A is checked with point B and C to see if it forms a local maximum. If so, it is considered for next stage, otherwise, it is suppressed ( put to zero).

In short, the result you get is a binary image with “thin edges”.

4). Hysteresis Thresholding

This stage decides which are all edges are really edges and which are not. For this, we need two threshold values, minVal and maxVal. Any edges with intensity gradient more than maxVal are sure to be edges and those below minVal are sure to be non-edges, so discarded. Those who lie between these two thresholds are classified edges or non-edges based on their connectivity. If they are connected to “sure-edge” pixels, they are considered to be part of edges. Otherwise, they are also discarded.

The edge A is above the maxVal, so considered as “sure-edge”. Although edge C is below maxVal, it is connected to edge A, so that also considered as valid edge and we get that full curve. But edge B, although it is above minVal and is in 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 minVal and maxVal accordingly to get the correct result.

This stage also removes small pixels noises on the assumption that edges are long lines.

So what we finally get is strong edges in the image.

// Code using in built function is provided above.

code using mathematics: :point_right:

code using built-in functions : :point_right:

That’s all for this time.

Simarpreet Singh , signing off.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

References:

  1. https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_canny.html

2. Github

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

#Python3 #CannyEdgeDetction #ImageProcessing #OpenCV

--

--

Simarpreet Singh
Simply Dev

Tech enthusiast, Psychology lover , Pogonophile, Ambidextrous