Image Operations-Gradients and Edge Detection

Nishank Sharma
the ML blog
Published in
4 min readAug 14, 2017

Hello guys,

It’s Nishank here, welcome to your second OpenCV wit Python tutorial. We will build upon many concepts of OpenCV from beginner to advanced level and we will end it with a project! You can read the first tutorial Intro to OpenCV here.

While processing a video feed, it is important to detect edges in order to recognise certain shapes in an image. Also when we work with depth analysis, it is very important that we understand the video feed, that is a stream of images. This is done by taking gradient in every frame and is the prime focus of this short post.

Let’s start by importing necessary headers.

Importing Headers

We will just be using cv2 library for this post for various gradient functions.

Now let’s get the video feed, by normal VideoCapture() function.

VideoCapture()

We will be using 3 gradients-

  1. Laplacian Derivatives
  2. Sobel-x
  3. Sobel-y

They are different gradient functions which use different mathematical operations to produce the required image. For example Laplacian calculates laplacian derivative whereas sobel is joint gaussian and differenciation operation. Don’t be overwhelmed by the details, just keep in mind that they are just different mathematical functions to analyse an image (you will see soon how). If you want to read more about them, you can read it here or here or here.

Let’s start by Laplacian Gradient.

Laplacian Gradient

This is a simple filter to our image frame and just produces the laplacian derivative of the same. Here cv2.CV_64F is just the datatype used to create lap. Let’s see the output of the filter on a Donald Trump image.

Original Image
After Laplacian Gradient

While this is not that helpful for this specific image, it can be used many other applications like object detection. Let’s move on to the the second gradient, that is sobel-x, here x specifies the direction of derivative as x direction. Similarly sobel-y is same as sobel-x but direction is y direction. Let’s observe the output.

Sobel-x
Sobel-y

As you can see, both images are different only in derivative direction and looks like some type of engravings. That is the reason why sobel is used for depth-analysis of images.

Now, let’s move on to edge detection part. Edge detection in OpenCV is done using a simple function called Canny Edge Detector, named after it’s creator John.F.Canny. It is a low error rate detector which provides good localization and minimal response. You can learn more about Canny here and here.

Canny Edge Detector

Here 100, 150 determines the size of the region we want to consider. We can change the size depending on our applications and the regions we are interested in. Let’s see how well it detect the edges.

Edges 100,150

Observing the image, we can say that it works pretty well. Now decreasing or increasing the area will give us a more unclear and clearer image respectively. Let’s try with a 150,200 area.

Edges 150,200

As you can see, they are more well defined and clear. This brings us to the end of the post. You can play around with the gradients and the edge-detector all you want and can also apply to various applications. All the code used in this post can be found by clicking the banner below. I will be posting the next post to the series soon. Stay tuned and subscribe to our newsletter for an awesome experience and never missing an update.

--

--

Nishank Sharma
the ML blog

Hello, I’m Nishank. I design beautiful, usable and enjoyable interfaces.