Skip to content

Prewitt

Fact Value
Also called Prewitt X, Prewitt Y
Status Implemented and tested
Family Edge detection and boundary analysis
Engine Browser
Type Linear, spatial, convolution
Output Image
Operator shown as fixed-kernel (exact)
Support radius 1
Code web/src/core/filters/edges/gradients.ts, prewittFilter
Operator code web/src/core/filters/edges/gradients.ts, GRADIENT_PAIRS
Channels Brightness only, RGB

Overview

The Prewitt operator estimates the brightness gradient with two 3x3 kernels. Each takes a difference across the edge and a plain sum of three rows along it. It is the simplest of the classic gradient kernels and gives almost the same edges as Sobel.

Three panels: bright disks on a dark background, the Prewitt gradient magnitude with bright rings at the disk edges, and the Sobel magnitude, which looks almost the same. Three panels: bright disks on a dark background, the Prewitt gradient magnitude with bright rings at the disk edges, and the Sobel magnitude, which looks almost the same.
Prewitt and Sobel give almost the same edges. Sobel weights the middle row more.

How it works

  1. Take the difference between the right and left columns of the window for G_x.
  2. Take the difference between the bottom and top rows for G_y.
  3. Combine them into the magnitude or the direction.

Mathematical foundation

\[ \begin{gathered} G_x = P_x \star I, \quad G_y = P_y \star I, \qquad P_x = \begin{bmatrix} -1 & 0 & 1 \\ -1 & 0 & 1 \\ -1 & 0 & 1 \end{bmatrix}, \quad P_y = P_x^{\mathsf T} \end{gathered} \]

The formula shows the default parameter values.

where

  • \(P_x, P_y\): The Prewitt kernels
  • \(\star\): Correlation, without flipping

P_x takes a difference across the edge and a plain sum of three rows along it. P_y is the same turned by 90 degrees. The magnitude and direction combine G_x and G_y as for the Sobel filter. Compared with Sobel, the plain sum smooths less and treats the three rows equally.

Each Prewitt kernel is separable: a central difference (-1, 0, 1) times a box (1, 1, 1). The box smooths less than the Sobel weights. So Prewitt is a little more sensitive to noise and a little less rotation invariant. scikit-image divides the kernels by 3. Here they keep their whole-number weights, as in Gonzalez and Woods.

Kernel or operator

The Inspector shows the kernels that run, read from the same code. The Impulse response demo in the Learn panel shows how one bright pixel spreads through them.

Algorithm

  1. Pad the image with the border mode.
  2. Correlate with a difference along x and a sum along y for G_x, and the reverse for G_y.
  3. Combine G_x and G_y into the chosen output.
  4. Map the result to 8 bits with the output mapping.

Parameters

Parameter Default Values Meaning
Output output Magnitude Magnitude, Horizontal change (Px), Vertical change (Py), Direction Which part of the gradient to show.
Magnitude norm norm L2 (square root of the sum of squares) L2 (square root of the sum of squares), L1 (sum of absolute values) How the two derivatives are combined into the magnitude.
Output mapping outputMapping Stretch to full range Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 How signed results, such as gradients, become 8-bit pixel values.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Channels channelScope Brightness only RGB, Brightness only Which part of the color image the filter changes.

Implementation

prewittFilter in web/src/core/filters/edges/gradients.ts runs each kernel as two 1D passes and combines the results with the code of the Sobel filter. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts, which handles the border mode, the channel scope, alpha and the output mapping. By default it works on luma and gives a gray result.

Worked example

\[ W = \begin{bmatrix} 20 & 20 & 200 \\ 20 & 20 & 200 \\ 20 & 20 & 200 \end{bmatrix} \]

The window lies on a vertical edge, dark on the left and bright on the right.

Output Value
Gx 540
Gy 0
Magnitude (L2) 540

Gx = (200 - 20) x (1 + 1 + 1) = 540. The three rows count equally. Sobel gives 720 here, because it weights the middle row by 2.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels per channel. Each kernel runs as two passes of 3 taps.

Artifacts and limitations

Limitations

  • Less rotation invariant than Sobel or Scharr. Diagonal edges come out weaker.
  • Sensitive to noise.

Artifacts

  • Thick edges, two or three pixels wide.

Tests

The reference correlates with the two kernels using scipy.ndimage.correlate. Before it is used, the script checks that the horizontal kernel is three times skimage.filters.prewitt along x. The test covers the magnitude with both norms, both derivatives and the direction, for all five border modes.

References