Sobel¶
| Fact | Value |
|---|---|
| Also called | Sobel X, Sobel Y |
| Status | Implemented and tested |
| Family | Edge detection and boundary analysis (also Linear spatial filters) |
| Engine | Browser |
| Type | Linear, spatial, convolution |
| Output | Image |
| Operator shown as | fixed-kernel (exact) |
| Support radius | 1 |
| Code | web/src/core/filters/edges/sobel.ts, sobelFilter |
| Operator code | web/src/core/filters/edges/sobel.ts, sobelKernels |
| Channels | Brightness only, RGB |
Overview¶
The Sobel operator measures how fast brightness changes at each pixel, and in which direction. Its two kernels combine a difference across the edge with a small average along it, which makes it less sensitive to noise than plain differences.
How it works¶
- Look at the 3x3 window around the pixel.
- Subtract the left column from the right column, counting the middle row twice. This is Gx.
- Subtract the top row from the bottom row, counting the middle column twice. This is Gy.
- Combine Gx and Gy into one strength, the magnitude, and one direction.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input values. Luma by default.
- \(K_x,\ K_y\): The horizontal and vertical Sobel kernels shown in the Operator tab
- \(\star\): Correlation. The kernel is applied without flipping.
- \(G_x,\ G_y\): Horizontal and vertical change at each pixel
- \(\lvert \nabla I \rvert\): Gradient magnitude, which is the edge strength
- \(\theta\): Gradient direction in radians, measured from the x axis toward the y axis. The y axis points down.
K_x responds to changes from left to right and K_y to changes from top to bottom. Each kernel takes a difference across the edge and a 1, 2, 1 weighted average along it. G_x and G_y are linear filters. The magnitude and the direction combine them in a nonlinear way.
Gx and Gy approximate the partial derivatives of a slightly smoothed image. Both are linear filters. The magnitude and the direction combine them in a nonlinear way. The direction is measured from the x axis toward the y axis, which points down in images, and it points from dark to bright.
Kernel or operator¶
The two kernels, and their separable factors. The filter runs the factors as two 1D passes.
Algorithm¶
- Convert to luma when the scope is brightness only.
- Pad the image with the border mode.
- Correlate with K_x and with K_y.
- Combine them into the chosen output.
- Map the values to 0 to 255 with the output mapping.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Magnitude | Magnitude, Horizontal change (Gx), Vertical change (Gy), 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 Gx and Gy 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¶
sobelGradients runs two separable passes for each kernel. sobelKernels builds the 3x3 matrices from the same factors for the Inspector. By default the filter works on luma and gives a gray result. Gx and Gy are signed, so the output mapping turns them into pixel values. The filter runs in the browser engine, in Web Workers. The studio splits the image into strips. Each strip gets a halo as wide as the support radius, so the strips give exactly the pixels of a whole-image run. The shared filter framework in web/src/core/filters/run.ts handles the border mode, the channel scope, alpha and the final rounding to 8 bits.
Worked example¶
The window lies on a vertical edge, dark on the left and bright on the right.
| Output | Value |
|---|---|
| Gx | 720 |
| Gy | 0 |
| Magnitude (L2) | 720 |
| Direction | 0 degrees |
Gx = (200 - 20) x (1 + 2 + 1) = 720. Gy is 0, because the top and bottom rows are equal. The direction 0 degrees points from dark to bright, along +x.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels. Each kernel is separable into a 1, 2, 1 average and a -1, 0, 1 difference.
Artifacts and limitations¶
Limitations
- Still responds to noise. Smooth first on noisy images.
- Edges are several pixels thick. Canny gives thin edges.
- Direction estimates are not equally accurate at all angles. Scharr kernels do better.
Artifacts
- Double lines along thin bright lines.
Tests¶
Gx, Gy, both magnitudes and the direction are compared with scipy.ndimage.sobel for all five border modes, within 1e-4 relative to the value. The fixture generator also checks SciPy against OpenCV's Sobel before it writes the reference.
References¶
- Sobel, I. and Feldman, G. A 3x3 isotropic gradient operator for image processing. Talk at the Stanford Artificial Intelligence Project, 1968.
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 10.
- OpenCV documentation, image filtering, Sobel. https://docs.opencv.org/4.x/d4/d86/group__imgproc__filter.html