Home » Blog » Image Processing » Sobel-Feldman filter: Edge Detection and Gradient Extraction
How to implement Sobel filter with FivekoGFX API

Sobel-Feldman filter: Edge Detection and Gradient Extraction

The Sobel filter (also called Sobel-Feldman operator) is an edge detection algorithm, that results in image emphasizing edges. Computer vision apps often use this image processing technique to extract the objects contours.

Interactive Preview: Image Edge Detection

online-edge-detection-after.webp online-edge-detection-before.webp

Use The Online Edge Detection Tool >>

What is Sobel filter?

The Sobel-Feldman operator is a separable edge detection filter. It is named after its discoverers – Irwin Sobel and Gary Feldman.

Like the Prewitt operator, the Sobel filter consists of two 3×3 convolution masks. From a computational point of view, it is relatively cheap to implement.

Application

The Sobel operator is applicable in many computer vision algorithms, some of which are:

Theory

The Sobel masks are with such design that they provide maximum value at horizontal or vertical edge orientation. Both masks have the same coefficients as they are rotated 90 degrees to each other. Occasionally it is desirable to retrieve only one of the gradient orientation – horizontal or vertical.

Sobel filter 3x3 kernel for edge detection
Detecting edges: Sobel operator with 3×3 mask window

The image below is an example result of a Sobel operator, where we can see clear outlines of the objects.

Comparison of image before (left) and after (right) applying the Sobel filter for edge detection.
Comparison of image before (left) and after (right) applying the Sobel operator for edge detection.

Preprocessing

Typically, edge recognition results are easily affected by noise in the image. Because of this it is commonly recommended to apply some low-pass filter like Gaussian.

Smoothing filters often smudge the edges as well. Therefore, many articles suggest the use of nonlinear edge-preserving filtering. Examples of such filters are: Symmetric Nearest Neighbor or Median filter.

How the Sobel filter works?

The Sobel filter works through a simple 3×3 convolution, so it is efficient for both CPU and GPU computations. The Sobel kernels are separable, which is an additional optimization option.

$\displaystyle{G}{x}={\left[\begin{matrix}+{1}&{0}&-{1}\\+{2}&{0}&-{2}\\+{1}&{0}&-{1}\end{matrix}\right]}={\left[\begin{matrix}{1}\\{2}\\{1}\end{matrix}\right]}\ast{\left[\begin{matrix}{1}&{0}&-{1}\end{matrix}\right]}$
Sobel-Feldman horizontal gradient with separable convolution
$\displaystyle{G}{y}={\left[\begin{matrix}+{1}&+{2}&+{1}\\{0}&{0}&{0}\\-{1}&-{2}&-{1}\end{matrix}\right]}={\left[\begin{matrix}{1}\\{0}\\-{1}\end{matrix}\right]}\ast{\left[\begin{matrix}{1}&{2}&{1}\end{matrix}\right]}$
Sobel-Feldman vertical gradient with separable convolution

Each image pixel is processed by each kernel in order to produce the final gradient value using equation (2). Additionally to gain performance it is acceptable to use the sum of absolute directional values as in equation (3).

gradient-magnitude-calculation
Gradient magnitude calculation

After calculating the vertical and horizontal gradients, we can also extract the edge orientation:

gradient-orientation-calc

We can note that usually edge detection creates thick contours of the object. In many cases, this is not very useful for object recognition and we need to apply additional processing.

To extract thin and strong edges of the image, we use the gradient magnitude and orientation. These parameters are sufficient to do edge thinning by non-maximum suppression.

Source code

The Sobel-Feldman filter is a common technique in computer graphics. In the following articles you can find implementation in different programming languages:

Edge Detection App

You can see how the Sobel filter works in our online image analysis demo.

WEB APPLET

See how it works in the browser!

Gradient Non-maximum suppression

References

Transform Your Images: Explore Our Must-Have Tools!