Computer Vision for Busy Developers

Finding Edges

This article is part of a series introducing developers to Computer Vision. Check out other articles in this series.

Finding Edges with the Sobel Operator

In human vision, one of the first steps in comprehending what we see is to learn and understand the outline of things we see.

We can tell a lot about what is happening in an image by focusing on the edges. (Image Source: Everton Vila)

Edge Detection is an important concept in computer vision where we attempt to extract the outline of objects. We can extract edges from an image when working with kernels and small chunks of pixels. In computer vision, Edges are defined as a region with high-contrast between adjacent pixels — a lighter pixel next to a darker pixel. The larger the difference between adjacent pixels, the higher the contrast and the more defined the edge.

Edges are identified by the difference of intensity of neighboring pixels

In the following examples, we are going to be using the grayscale version of following image to explore edges.

Image Source Simon Migaj

Since edges are defined as a change in contrast between adjacent pixels, there’s an inherent directionality when it comes to edges. More simply, are we looking at the difference between adjacent pixels that are on top of each other or side-by-side. Due to this directionality, we construct our Kernels to match edges of a specific orientation. First, we’re going to be looking for changes in contrast between pixels on top of each other — this will result in horizontal edges. The kernel we are using is the Sobel Kernel as shown below:

[ 1, 2, 1]

[ 0, 0, 0]

[-1, -2, -1]

Looking at the kernel above, we can see how it might emphasize edges in the image. The top side of the kernel is positive and the bottom side of the kernel is negative — emphasizing differences between pixels on top of each other.

Sobel works differently than the blurs we discussed earlier. We are not going to divide by the sum of the kernel values. For now, we’ll just keep the result of the sum operation and use that as interim value.

The Sobel convolution in action

If any of the results from the convolution is equal to zero, then that means that there is no edge information. We are in a “flat” area in the image. Non-zero results indicates that there is an edge and the further from zero in either positive or negative direction corresponds to harder edge. This is a really simple idea and it shows the power of convolutions!

When running Sobel through a simplified image, the process is very noticeable!

As you can see in the following image, the horizontal edges have been highlighted. The horizontal edges are more noticeable on the wooden slats that make up the bridge. Notice that information regarding the telephone pole on the left and the wooden siding of the cabin on the right are largely missing. This is because there is very little horizontal change in contrast in these areas.