Sobel with non-maximum suppression¶
| Fact | Value |
|---|---|
| Also called | Thinned Sobel, Non-maximum suppression |
| Status | Implemented and tested |
| Family | Edge detection and boundary analysis |
| Engine | Browser |
| Type | Nonlinear, spatial, gradient and non-maximum suppression |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | 2 |
| Code | web/src/core/filters/edges/nms.ts, sobelNmsFilter |
| Operator code | web/src/core/filters/edges/nms.ts, suppress |
| Channels | Brightness only, RGB |
Overview¶
Sobel with non-maximum suppression keeps the Sobel edge strength only where it peaks across the edge. The Sobel magnitude of an edge is two or three pixels wide. Suppression keeps its ridge line and sets the rest to 0, so edges become one pixel thick. It is the second stage of the Canny detector, shown here on its own.
How it works¶
- Compute the Sobel strength and direction at every pixel.
- Round the direction to horizontal, vertical or one of the two diagonals.
- Keep the strength only if it is at least as large as both neighbors along that direction.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(M\): The Sobel magnitude
- \(d_{\theta}\): The step to the neighbor along the rounded gradient direction
M is the Sobel magnitude and theta the gradient direction. The step d_theta goes to the neighbor along the gradient, rounded to horizontal, vertical or one of the two diagonals. The sector borders are at 22.5, 67.5, 112.5 and 157.5 degrees. A pixel survives only if it is at least as strong as both neighbors across the edge, so each edge keeps its ridge line. The greater-or-equal test keeps both pixels of a flat-topped ridge.
Across an edge, the gradient magnitude rises to a peak and falls again. The peak is where the brightness changes fastest, the best place for the edge. Comparing each pixel with its neighbors along the gradient finds that peak. The direction is rounded to four sectors 45 degrees apart, as in Gonzalez and Woods. Canny used this step in his detector, before hysteresis thresholding.
Kernel or operator¶
There is no single kernel. The Sobel kernels are on the Sobel page. The worked example below follows one row across an edge.
Algorithm¶
- Extend the image by two pixels with the border mode.
- Compute the Sobel derivatives, the magnitude and the direction.
- Round the direction to one of four sectors.
- Keep the magnitude where it is at least as large as both neighbors along that direction, and cut the extension off.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
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¶
suppress in web/src/core/filters/edges/nms.ts compares each magnitude with its two neighbors along the rounded direction. The filter extends the plane once by two pixels with the border mode, computes the Sobel derivatives, suppresses, and cuts the extension off. It runs in the browser engine, in Web Workers, through the shared filter framework, and works on luma by default.
Worked example¶
| x | 29 | 30 | 31 | 32 | 33 | 34 |
|---|---|---|---|---|---|---|
| Input | 41 | 50 | 93 | 162 | 205 | 214 |
| Sobel magnitude | 40 | 208 | 255 | 255 | 208 | 40 |
| After suppression | 0 | 0 | 255 | 255 | 0 | 0 |
A soft vertical edge, with every row the same, so the Sobel magnitude is 4 times the difference of the two horizontal neighbors. The table shows it clipped at 255. At x = 31 it is 4 x (162 - 50) = 448, and at x = 32 it is 4 x (205 - 93) = 448. The gradient points along x, so each pixel is compared with its left and right neighbors. These two pixels form the top of the ridge and keep their value. The rest become 0.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels per channel.
Artifacts and limitations¶
Limitations
- Rounding the direction to four sectors can leave small gaps or double pixels on curved edges.
- Noise still gives many small ridges. Thresholding, as in Canny, removes them.
Artifacts
- Broken lines on diagonal and curved edges.
Tests¶
The reference computes scipy.ndimage.sobel on the extended plane and applies the same four-sector rule with NumPy. The test covers all five border modes.
References¶
- Canny, J. A computational approach to edge detection. IEEE Transactions on Pattern Analysis and Machine Intelligence PAMI-8(6), 679 to 698, 1986. DOI 10.1109/TPAMI.1986.4767851.
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 10.2, the Canny edge detector.