OpenCV: Edge detection

Ali Almutawakel
4 min readApr 10, 2017

This is the 3rd introduction for OpenCV. This is a code-along tutorial to learn OpenCV in Python. We will be looking at edge detection concepts and Canny edge detection function in OpenCV.

Edges define the boundaries of an object. A lot of an image’s detail can be taken out and the object can still be identified through its edges. Take the example below of the dog. On the left is the original image, and on the right is the binary form of it. A lot of the detail like colors and background had been taken out but the edges had preserved a lot of details that are useful to identify what’s the object in the image.

Computers love binary. It is fast to process and straight forward (1 or 0). The technique of getting an image’s edges is very useful when it comes to preprocessing an image for object detection. It converts the image to its simplest forms removing unnecessary details that aren’t useful and leaving the interesting aspect of it.

To our eyes, it is very easy to pickup what are the edges of the dog. However, how can we make the computer do the same?

Edge detection concept

When processing an image, almost every algorithm relies on the concept of neighbouring pixels. Comparing pixels to each other can get us a lot of insights about the image. Comparisons can be applied on simply one pixel’s values or a group of pixels’ values against another group.

With edge detection, neighbouring pixels play a great role. Let’s take the following example to demonstrate how it can be done.

Assume we are processing an image to find the edges. Your program is running through all the pixels in that blue line. By comparing the intensity value of one pixel to its previous pixel, we will end up in a graph like the one on the left. That said, we can define edges now as sudden intensity changes in an image. That sudden discontinuity of a particular value is an example of a cue to detect an edge.

Canny Edge Algorithm

The most popular algorithm for edge detection is Canny Edge detection. Developed by John F. Canny in 1986, it became the most widely used algorithm due its accuracy and low error rate. An image undergoes few stages of processing before the final result is produced.

  1. Gaussian blur is applied to remove noise.
  2. Finding the intensity (brightness) gradient of the image like the simple example above.
  3. Apply non-maximum suppression to get rid of pixels that are not edges for processing.
  4. Applies hysteresis. Suppose we have two thresholds U & L. Anything above U is an edge and anything lower than L is not. Anything in between them can possibly be. With hysteresis, we can tell if that a potential edge candidate that is connected to a verified strong edge is considered an edge because edges tend to be continued in real life.

In OpenCV

Fortunately, OpenCV provides few edge detection functions as part of their API and one of them is Canny(image, L, U). In my example, it takes three parameters:

  • Image
  • L: Lower threshold value. Any value below L is considered not to be an edge.
  • U: Upper threshold value. Any value above U is considered to be an edge.
import cv2# Reading the image in grey scale indicated by the 0 parameter
image = cv2.imread('images/input.jpg',0)
# Showing original image
cv2.imshow('Original', image)
cv2.waitKey(0)
# Computing edges with Canny with thresholds 50 and 120
# then showing it
canny = cv2.Canny(image, 50, 120)
cv2.imshow('Canny', canny)
cv2.destroyAllWindows()

Results:

What’s Next?

--

--