The Canny detector turns a smoothed grey image into thin, connected edges. The image you receive is already blurred, so you build the three stages that follow the blur.
1. Sobel gradients. Extend the image by mirror reflection without repeating the edge pixel (NumPy's reflect padding: a row a b c d extends as c b | a b c d | c b). With along columns (pointing right) and along rows (pointing down), and weights , :
The magnitude is .
2. Non-maximum suppression. Let in degrees, taken modulo 180 so that it lies in . Compare with its two neighbours along the gradient direction:
The output keeps when it is greater than or equal to both neighbours and is 0 otherwise. Pixels in the first and last row and the first and last column are always 0.
3. Hysteresis with thresholds . A pixel of the suppressed map is strong if its value is high and a candidate if it is low. The edges are the candidates that can be reached from some strong pixel through a chain of candidates, each step going to one of the 8 surrounding pixels.
Implement sobel_gradients(img) returning (gx, gy) float arrays, nonmax_suppression(mag, gx, gy) returning a float array, hysteresis(nms, low, high) returning an integer 0/1 array, and canny(img, low, high) chaining the three stages. Every output has the input's shape.