Home » Blog » Tutorials » Prewitt operator for edge detection

Prewitt operator for edge detection

The Prewitt operator is one of the first algorithms for edge detection by gradient transform. It is developed by Judith M. S. Prewitt and is a popular tool for image analysis.


How does the Prewitt operator works?

The Prewitt operator detects image edges by convolution with two filter masks. One for horizontal and one for vertical direction. At this way we can extract:

  • Horizontal edges
  • Vertical edges
  • Both directions

The coefficients of Prewitt masks are:

prewitt-gradient-operator-masks
Prewitt operator with 3×3 masks

Like other gradient detection operators, this one also has a separable property.

For example we can represent Gx as:

prewitt-operator-separable-gx
Prewitt example of separable convolution for Gx

We apply both masks to each pixel of the image. In this way we find the difference in the intensity levels of the neighboring pixels in both directions. The result of this operation determines the so-called first derivative of the signal (image).

Gradient magnitude calculation

Using the following equation we can calculate the orientation of the edge of each pixel. This parameter allows very fast thinning of the resulting contour lines by the non-maximum suppression algorithm:

Gradient orientation

Example

The image below is an example result of Prewitt edge detector. To reduce noise in the picture, it is enhanced with Gaussian blur prior the edge detection process.

prewitt-gradient-detection-example
Example result of Prewitt edge detector with Gaussian filter with sigma 2.0

Other gradient operators

Gradient detection is a common task in image analysis and there are alternative operators for this purpose. Some of which are:

What is the difference between Prewitt and Sobel?

In fact, the gradient differentiation operators like Prewitt, Sobel and Scharr share the same principle of work. All of them use a separable kernels to calculate the gradient approximation.

The main difference is that the Prewitt filter uses the same weight for all the pixels that fall under the masks. Unlike Prewitt, Sobel and Scharr give less weight to points far from the center pixel.

Source code


Gradient Detection Online

On our online test page you can see and compare the most popular gradient detectors for free without any installation, registration or whatever.

Gradient detection demo app

WEB APPLET

See how it works in the browser!


You can support me at ko-fi.com