You’re implementing the hysteresis thresholding step that typically comes after non-maximum suppression (NMS) in Canny edge detection.
1. Background Knowledge (concepts & theory)
After you compute the gradient magnitude of an image and perform non-maximum suppression, you get a thin edge magnitude map: large values at likely edge locations, small values elsewhere. However, this map is still noisy: many weak responses may be due to noise, texture, or small fluctuations, not true object boundaries.
A single global threshold (e.g., “keep all pixels with magnitude > T”) is often too crude:
If T is high → you lose real but weak edges (breaks in contours).
If T is low → you keep a lot of noise and speckles.
Hysteresis thresholding uses two thresholds to fix this. Pixels with magnitude above a high threshold are considered strong edges (reliable). Pixels between low and high thresholds are weak edges: they are kept only if they are connected (via an 8-connected path) to at least one strong edge. Everything at or below the low threshold is discarded. This preserves continuous edges while suppressing isolated noise.
2. Algorithm / Approach Pattern
At a high level, the pattern is:
Classify pixels into:
Strong edge
Weak edge
Non-edge
Graph traversal from strong edges:
Treat each strong edge pixel as a seed.
From each seed, perform a search (DFS/BFS) over neighbors.
Whenever you encounter a weak edge that is connected via 8-connectivity, promote it to a final edge and continue from there.
Output binary edge map:
All visited strong + connected weak pixels → 1
Everything else → 0
Conceptually, you’re doing connected-component growing starting from strong edges, but you are allowed to grow only through pixels labeled as weak.
3. Step-by-Step Strategy to Implement
Assume you are given:
mag – 2D array of edge magnitudes after NMS
low – low threshold
high – high threshold
Target: output edges – 2D binary array (0/1 or False/True).