Discovering OpenCV using Python: Edge detection
Tags: python, edgedetection, opencv, imageprocessing
Published: 2020-07-19 18:15:07

During my learning of the ways of the force in OpenCV, it was a matter of time before encountering edge detection. This concept, that also falls under image processing, uses the same principle of convolution that [I wrote about before](https://dev.to/tinazhouhui/discovering-open-cv-using-python-2iak). The aim of the edge detection is to highlight the outline of the most important information of an image. This is one of the essential steps in object detection and directly affects machines’ understanding of the world. 

# So what is an edge?
First of all, it is important to understand that the real world is continuous whereas a picture of the real world is made up of discrete (individual) pixels. Now let’s zoom in on the pixels for one second:

![Edge identification](https://dev-to-uploads.s3.amazonaws.com/i/mbn1y4f1jvzv2o0pd63q.jpg)

It is obvious to all of us, that the edge is where the red line is indicating; however, it is not possible to draw the edge between the smallest two units (not entirely true, there are methods, like super-sampling). Therefore, the computer checks whether a pixel is on an edge by comparing the pixel’s adjacent neighbors. Here, if we were to go from left to right, pixel number 2 is an edge as there is a significant difference between 1 and 3. Same applies for pixel number 3. The result would look something like this:

![Edge identification result](https://dev-to-uploads.s3.amazonaws.com/i/a7915aqtdn24l5aaq47d.jpg)

Nevertheless, in real life, pictures also include gray pixels with various intensity (value from 1 to 254). Therefore, to see how much the gradient changed (see also [gradient magnitude](https://en.wikipedia.org/wiki/Image_gradient) if you are more interested), we use the first derivative to indicate the magnitude of change. It is then up to us to establish a threshold that would determine whether the magnitude of change is large enough to be considered an edge. To take it one step further, we could also use the second derivative and look for zero-crossing values, which capture the local maxima in the picture gradient (see image below). 

![First and second order derivative](https://dev-to-uploads.s3.amazonaws.com/i/kjlsuwegbxd3uwa4iync.jpg)

In addition, due to the nature of edge detection, they are quite sensitive to noise (think about poor image quality; therefore, it is highly recommended to blur the image in order to smooth the noise out and therefore increase the accuracy of the edges. 

> Said in simpler words, first order derivatives are best for identification of strong edges by establishing a threshold whereas second order derivatives are best for locating where the edge is. Both types are noise sensitive, so blur the images first to obtain best results.

# Examples of edge detection
I will be applying various types of edge detection methods on this original image below. Be sure to check out the code on my [GitHub](https://github.com/tinazhouhui/computer_vision/blob/master/image_processing/edge_detection.py).

![Original image](https://dev-to-uploads.s3.amazonaws.com/i/e1vun57sm0hmqvk9k1tr.jpg)
[Original image source](https://envato-shoebox-0.imgix.net/5c82/65c1-5009-4fb0-9df8-19d302da34bc/021718+%285%29.jpg?auto=compress%2Cformat&fit=max&mark=https%3A%2F%2Felements-assets.envato.com%2Fstatic%2Fwatermark2.png&markalign=center%2Cmiddle&markalpha=18&w=1600&s=d3c05b54ebb94a03b503b04eeb65e2b8)


### First order derivative - Sobel operator
Sobel edge detector is a first order derivative edge detection method. It calculates the gradients separately along the X axis and Y axis. The kernels already incorporate a smoothing effect. There are many other types of kernels like [Scharr or Prewitt](https://theailearner.com/2019/05/24/first-order-derivative-kernels-for-edge-detection/).

![Sobel kernel](https://dev-to-uploads.s3.amazonaws.com/i/aez8oighy7eou7vre8hh.jpg)
![Sobel edge detection](https://dev-to-uploads.s3.amazonaws.com/i/rbj2miwjff29z9ncr7u0.jpg)

### Second order derivative - Laplacian operator
Unlike Sobel operator, Laplacian uses only one kernel, nevertheless, more popular use is to combine it with the Gaussian blur in order to reduce noise. 

![Laplacian kernel](https://dev-to-uploads.s3.amazonaws.com/i/cxe17lw1caeqb1kar7hg.jpg)
![Laplacian edge detection](https://dev-to-uploads.s3.amazonaws.com/i/j2dywdq8gx4x5719nou2.jpg)

### Canny edge detection
Canny edge detector is probably one of the most widely used edge detection methods. The reason why it is superior to the so far mentioned methods is due to [“non-maximum suppression”](https://en.wikipedia.org/wiki/Canny_edge_detector#Non-maximum_suppression) to produce a one pixel thick edge. The detection consists of many steps, check it out [here](http://justin-liang.com/tutorials/canny/).

![Canny edge detection](https://dev-to-uploads.s3.amazonaws.com/i/3wrbv4sv88e9bdmwjc2n.jpg)

#Real life application
Edge detection is just a first step that is necessary for further image analysis like shape detection or object identification. Apart from the most obvious use in machine vision (i.e. self driving vehicles), it is also widely used on analysis of medical images to help with diagnostics or even identify pathological objects like tumor. 

All in all, this was yet again a fun way to play around with OpenCV, let me know what you think and may the Python be with you.