Sobel Edge Filter

Shailesh Kumpawat
Analytics Vidhya
Published in
3 min readAug 9, 2020

We can think of an image, which is comprised of individual pixels, as a function, f. Each pixel also has its own value. For a grayscale image, each pixel would have an intensity between 0 and 255, with 0 being black and 255 being white. f(x,y) would then give the intensity of the image at pixel position (x,y), assuming it is defined over a rectangle, with a finite range: f: [a,b] x [c,d] → [0, 255].

A color image is just a simple extension of this. f(x,y) is now a vector of three values instead of one. Using an RGB image as an example, the colors are constructed from a combination of Red, Green, and Blue (RGB).

We use filters(kernels) to do image processing. Image filtering changes the range (i.e. the pixel values) of an image, so the colors of the image are altered without changing the pixel positions. Image filtering is done using the convolution operation over the image.

Edge Detection

In computer vision terms we can say, edges are sudden discontinuities in an image, which can arise from surface normal, surface color, depth, illumination, or other discontinuities.

We can pinpoint where edges occur from an image’s intensity profile along a row or column of the image. Wherever there is a rapid change in the intensity of the function, we detect an edge. And where there is change there is derivative. In other words, wherever the function’s derivative has local extrema, there’s an edge.

An image gradient, which is a generalization of the concept of derivative to more than one dimension, points in the direction where intensity increases the most. Since our function, i.e. the image is not a continuous function, we cannot take the derivative in the usual limiting sense. Thus we use a discrete variant that tries to abstract the idea of a derivative or a gradient. This can be done using a Sobel Filter. There are two of them, one each for x and y directions.

Sobel Filter

Although just using the Sobel filter won’t give great results as there might be noise in the image. But thresholding the output can improve the result.

To improve Edge detection significantly, we can use Canny Edge Detection. It first smoothes the image using a filter so that there’s no noise and then performs edge detection. It further improves the edge detection using non-max suppression which thins the edges. But the idea of Canny Edge Detection cannot be captured in a single filter.

Applying the x and y derivatives of a Gaussian filter to the image to eliminate noise, improve localization, and have a single response. Finding the magnitude and orientation of the gradient at each pixel.
Performing non-maximum suppression, which thins the edges down to a single pixel in width, since the extracted edge from the gradient after step 2 would be quite blurry and since there can only be one accurate response.
Thresholding and linking, also known as hysteresis, to create connected edges. The steps are to 1. determine the weak and strong edge pixels by defining a low and a high threshold, respectively, and to 2. link the edge curves with the high threshold first to start with the strong edge pixels, continuing the curves with the low threshold.

--

--