Canny Edge Detection.

Vin_itall
Bleep-Bloop
Published in
5 min readNov 14, 2019

A multi-stage algorithm that filters out edges in a picture.

Edge detection finds it’s role in a lot of important applications in image and video processing, like whenever you’re trying to get your self-driving agent to drive on the right side of the road(detecting lanes) and not be penalized for drunk driving or you’re trying to detect it’s license plate to penalize it for drinking and driving.

In any image an edge can be found wherever there is a sharp change in the intensity or color. In order to understand how Canny Edge Detection works we first need to understand how the Sobel Operator works.

Sobel Operator.

Applying a sobel operator or a sobel filter to an image is convolving the image with the two 3x3 kernels one in X direction and the other in Y direction to determine it’s gradients in both vertical and horizontal directions.

The kernels in the X and the Y directions are

Sobel Kernels For X and Y direction.

These two kernels are convolved with the target image and the magnitude of the gradient at each point in the image combining the Gradients in the X and the Y directions is computed as

Using the Gradient values in the X and the Y directions we can also compute the gradient’s direction using.

Direction Of The Gradient.

Let’s take a look at the implementation of whatever we have learned.

Applying Sobel Kernel To An Image Using OpenCV.

We get the output as follows.

As you can observe applying Sobel in X on this pretty checkered shorts of mine, it gives us the edges in the X direction and in Y direction it gives us the edges in the Y direction.

You must thinking “Yo! this blog’s a scam where are my edges?! These aren’t edges!!”. So, let’s quickly get to Canny Edge Detection before you start commenting in capitals.

Canny Edge Detection

Canny Edge Detection is a 5 step algorithm that is used to detect edges(duh!).

  1. Reducing Noise By Applying Blur.
  2. Calculating Gradient.
  3. Non-Maximum Suppression.
  4. Double Threshold.
  5. Edge Linking.

Step 1. Applying Blur.

We applying a blur to make the image smoother and remove out the noise. You can learn more about it on this article https://medium.com/bleep-bloop/image-convolution-f6c243574557 .

Step 2. Calculating Gradient.

Calculating the Sobel gradient for the image using Sobel operator in both directions.(See I told you needed to understand Sobel operator first)

Step 3. Non-Maximum Suppression.

Non-maximum suppression is applied in order to get thinner edges. So, to get the thinnest possible edges the algorithm tries to get a pixel with the most intensity in a given direction.

Non-Maximum Suppression Example.

So the image will look like

Image After Non-Maximum Suppression.

So, the edge is now sharper than it was before applying Non-Maximum Suppression to the pixel.

Step 4. Double Threshold.

Double thresholding helps us to focus on the edges that we really need and filters out all the additional noise that non-maximum suppression couldn’t.

  • High Threshold is used to detect the pixels that we’re are sure that contributes to an edge and isn’t noise, by setting high threshold(high threshold is nothing but a value of intensity ranging from 0 to 255) we take all the pixels whose intensity is higher than the high threshold as definitely contributing to the edge.
  • Low Threshold is used to filter out all the pixels that are irrelevant to us i.e, by setting low threshold(again a value of intensity ranging from 0 to 255) we ignore all the pixels having intensity lower than this threshold as noise.
  • All the pixels that lie between the high and the low threshold are only taken into consideration if and only if they are in some way connected to or a part of the edge that has pixels above the high threshold, the pixels having no such connection are ignored as noise, this is done by edge linking.
Double Threshold Example.

In the above given scenario, anything below the low threshold won’t be taken into consideration and will be treated as noise.

Since A is above high threshold it will be taken into consideration in the output image.

Now, Since B is in between high and low threshold and has no link to any pixels above high threshold, will be neglected and treated as noise by the algorithm.

However, C being in between high and low thresholds but having a link or being in continuation of the edge A which is well above high threshold, C will be considered in the output image.

Step 5. Edge Linking.

The above process of linking C to A, even though the intensity of C is lesser than the high threshold is called edge linking, where the algorithm changes the value of intensities of pixels(between High and Low thresholds) to make them brighter in the presence of pixels(above High threshold) that have high values of intensity to form a neat and sharp edge.

Now let’s take a look at the implementation.

Original Image Vs Image After Canny Edge Detection.

Following is an application of edge detection where I am detecting the lanes of a street in GTA San Andreas…

You can find the code used for detecting lanes in Video Games in the following GitHub repository.

So that’s how developers keep self driving agents sober and save lives. That’s all for this blog.

Stay tuned for more…Bleep-Bloop.

References

--

--