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.
How it works¶
- Take the difference across the edge.
- Average along the edge with the weights 3, 10, 3.
- Combine the two directions into the magnitude or the direction.
Mathematical foundation¶
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¶
- Pad the image with the border mode.
- Correlate with the difference along x and the 3, 10, 3 smoothing along y for G_x, and the reverse for G_y.
- Combine G_x and G_y into the chosen output.
- 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¶
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¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 10.2.
- OpenCV documentation, cv::Scharr. https://docs.opencv.org/4.x/d4/d86/group__imgproc__filter.html