Skip to content

Scharr

Fact Value
Also called Scharr X, Scharr 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, scharrFilter
Operator code web/src/core/filters/edges/gradients.ts, GRADIENT_PAIRS
Channels Brightness only, RGB

Overview

The Scharr operator estimates the gradient with 3x3 kernels whose smoothing weights are 3, 10, 3. Scharr chose these weights so that the gradient direction is as accurate as a 3x3 kernel allows, in every direction. The edge strength is nearly the same for edges at any angle.

Two panels: a Siemens star and its Scharr gradient magnitude, with even brightness along spokes of every direction. Two panels: a Siemens star and its Scharr gradient magnitude, with even brightness along spokes of every direction.
The Scharr magnitude is nearly the same for spokes in every direction.

How it works

  1. Take the difference across the edge.
  2. Average along the edge with the weights 3, 10, 3.
  3. Combine the two directions into the magnitude or the direction.

Mathematical foundation

\[ \begin{gathered} S_x = \begin{bmatrix} 3 \\ 10 \\ 3 \end{bmatrix} \begin{bmatrix} -1 & 0 & 1 \end{bmatrix}, \qquad S_y = S_x^{\mathsf T} \end{gathered} \]

The formula shows the default parameter values.

where

  • \(S_x, S_y\): The Scharr kernels

A central difference across the edge and a 3, 10, 3 smoothing along it. Scharr chose these weights so that the error of the gradient direction is as small as possible for a 3x3 kernel. The weights are those of OpenCV's Scharr operator, without normalization.

Like Sobel, each kernel is a central difference times a smoothing, but the smoothing 3, 10, 3 was optimized to make the gradient direction error small for all orientations. OpenCV uses these weights without normalization, and so does this filter. scikit-image divides them by 32, so its values are 32 times smaller.

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 the difference along x and the 3, 10, 3 smoothing 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 (Sx), Vertical change (Sy), 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

scharrFilter in web/src/core/filters/edges/gradients.ts runs each kernel as two 1D passes and combines the results like 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 2880
Gy 0
Magnitude (L2) 2880

Gx = (200 - 20) x (3 + 10 + 3) = 2880. The large weights make the values large, so the output mapping scales them.

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

  • The weights are large, so results need the output mapping.
  • Sensitive to noise, like all 3x3 derivatives.

Artifacts

  • Thick edges, two or three pixels wide.

Tests

The reference correlates with the two kernels. Before it is used, the script checks the horizontal kernel against cv2.Scharr. The test covers all outputs for all five border modes.

References