Hysteresis thresholding¶
| Fact | Value |
|---|---|
| Also called | Double thresholding with edge tracking, Hysteresis threshold |
| Status | Implemented and tested |
| Family | Edge detection and boundary analysis |
| Engine | Browser |
| Type | Nonlinear, spatial, two thresholds and connectivity |
| Output | Mask |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/edges/hysteresis.ts, hysteresisFilter |
| Operator code | web/src/core/filters/edges/hysteresis.ts, hysteresis |
| Channels | Brightness only, RGB |
Overview¶
Hysteresis thresholding turns a response map, such as a gradient magnitude, into a mask with two thresholds. Pixels above the high threshold are sure edges. Pixels above the low threshold are kept only when a chain of such pixels joins them to a sure edge. So long edges stay whole where they fade, and isolated weak responses, which are often noise, disappear.
How it works¶
- Mark every pixel at or above the high threshold.
- From each marked pixel, step to neighbors at or above the low threshold and mark them too.
- Repeat until nothing changes. Marked pixels become 255, all others 0.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(v(p)\): The input value at p
- \(T_L\): The low threshold
- \(T_H\): The high threshold
v is the input value, T_L the low threshold and T_H the high one. Each step of the path goes to one of the 4 or 8 neighbors. Pixels in E become 255 and all others 0. The tests use greater than or equal, as in Gonzalez and Woods. scikit-image uses greater than, which gives the same result on whole numbers with thresholds lowered by one half.
A single threshold must choose between keeping noise and breaking edges. Canny proposed two thresholds to escape this choice. The result is the union of the connected parts of the low-threshold mask that contain at least one pixel of the high-threshold mask. With 8 neighbors, diagonal steps join pixels. With 4 neighbors, they do not, which is the default of scikit-image.
Kernel or operator¶
There is no kernel. The Inspector shows the strong set, the weak set and the rule that joins them, with the current thresholds.
Algorithm¶
- Mark every pixel at or above the high threshold as an edge.
- From each edge pixel, step to neighbors at or above the low threshold and mark them too.
- Repeat until no new pixel is marked.
- Write 255 at marked pixels and 0 elsewhere.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Low threshold low |
50 | 0 to 255 | Pixels at or above this value are weak. They are kept only when joined to a strong pixel. |
High threshold high |
100 | 0 to 255 | Pixels at or above this value are strong and always kept. If it is below the low threshold, the low threshold is used. |
Neighbors connectivity |
8 neighbors (diagonals join) | 8 neighbors (diagonals join), 4 neighbors | Which pixels count as joined. |
Output mapping outputMapping |
Clip to 0 to 255 | 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. |
Channels channelScope |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
hysteresis in web/src/core/filters/edges/hysteresis.ts marks the strong pixels and grows them through weak neighbors with a stack, so each pixel is visited at most once. No border is needed, because paths stay inside the image. The filter runs in the browser engine through the shared filter framework in web/src/core/filters/run.ts, which handles the channel scope, alpha and the output mapping. It is a global filter. A path can cross the whole image, so the filter always runs on the whole image in one tile. A selection is cut out of the result. By default it works on luma.
Worked example¶
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Value | 30 | 70 | 120 | 80 | 60 | 20 | 90 | 40 |
| Class | - | weak | strong | weak | weak | - | weak | - |
| Result | 0 | 255 | 255 | 255 | 255 | 0 | 0 | 0 |
One row of values with low threshold 50 and high threshold 100. Only x = 2 is strong. The weak pixels at x = 1, 3 and 4 touch it through a chain of weak pixels, so they are kept. The value 90 at x = 6 is weak, but the 20 at x = 5 cuts it off from every strong pixel, so it becomes 0.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels per channel. Each pixel is marked at most once.
Artifacts and limitations¶
Limitations
- A weak noise pixel that touches an edge is kept.
- It needs the whole image, so it cannot run on strips in parallel.
Artifacts
- Short spurs where noise touches an edge.
Tests¶
The reference labels the connected parts of the low-threshold mask with scipy.ndimage.label and keeps those with a strong pixel. Before the cases are written, the script checks it against skimage.filters.apply_hysteresis_threshold, which uses greater than and 4 neighbors, with both thresholds lowered by one half. The test covers 4 and 8 neighbors and a high threshold below the low one.
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.