While the Sobel operator gives us a good starting point by highlighting areas of high intensity change, the resulting "edges" can often be thick, noisy, and disconnected. For many applications, we need a more refined result: clean, thin, continuous lines that accurately represent object boundaries. This is where the Canny edge detector comes in. Developed by John F. Canny in 1986, it's a multi-stage algorithm designed specifically to produce superior edge maps and is arguably the most widely used edge detection algorithm today.
The Canny algorithm aims to satisfy three main criteria for good edge detection:
To achieve these goals, the Canny algorithm follows a sequence of well-defined steps:
Real-world images almost always contain some level of noise (random variations in pixel intensity). Edge detection algorithms, especially those based on gradients (intensity changes), are very sensitive to noise. A small noisy fluctuation could be misinterpreted as an edge.
Therefore, the first step in the Canny algorithm is to smooth the image slightly to reduce noise. This is typically done using a Gaussian filter, just like the smoothing filters we discussed in the previous chapter. Applying a Gaussian blur helps suppress minor intensity variations that aren't part of significant edges. The amount of smoothing (controlled by the size and standard deviation, sigma, of the Gaussian kernel) is a parameter that can affect the final result; too much smoothing might blur out weaker edges, while too little might leave too much noise.
Once the image is smoothed, the next step is to find the intensity gradients, similar to how the Sobel operator works. This involves calculating the first derivative of the image intensity in both the horizontal (Gx) and vertical (Gy) directions. From these, we can compute the gradient magnitude (edge strength) and direction for each pixel:
The gradient magnitude gives us an image where brighter pixels indicate stronger potential edges. However, these edges are still usually thick. The gradient direction is important for the next step.
This crucial step aims to thin the thick edges found in the gradient magnitude image down to single-pixel width lines. The idea is simple: for any given pixel, we check if its gradient magnitude is the largest compared to its neighbors along the gradient direction.
Imagine an edge boundary. The gradient magnitude will be highest at the center of the edge and will fall off on either side. We want to keep only the pixels at the very peak of this gradient "ridge" and suppress the others (set their value to zero).
To do this, the algorithm looks at each pixel's gradient direction (θ). The direction is rounded to one of four main orientations (e.g., horizontal, vertical, or one of the two diagonals). Then, the pixel's gradient magnitude (G) is compared with the gradient magnitudes of the two neighboring pixels along that direction.
This process effectively thins the edges, leaving mostly single-pixel-wide lines representing the locations with the sharpest intensity change.
Flow of Non-Maximum Suppression for a pixel P and its neighbors N1, N2 along the gradient direction.
The non-maximum suppression step gives us thin edges, but some might be caused by noise or minor color variations that we don't consider "real" edges. Double thresholding helps filter these out based on their gradient magnitude (strength).
Instead of using a single threshold, Canny uses two:
These thresholds are applied to the image resulting from non-maximum suppression:
Choosing appropriate threshold values is important. If Thigh is too high, strong edges might be broken into segments. If Tlow is too low, noise might start appearing in the final edge map. Typically, Thigh is set to be 2 or 3 times Tlow.
This final step determines which of the weak edge pixels (those between Tlow and Thigh) should be kept. The logic is based on connectivity: a weak edge pixel is kept only if it is connected to a strong edge pixel.
The algorithm scans the image and performs the following check for each weak pixel:
Essentially, we start from the definite "strong" edge pixels and follow connections through the "weak" edge pixels. Any weak pixel that cannot be reached from a strong edge pixel via a path of other weak pixels is discarded (set to 0).
This hysteresis process helps achieve two goals:
The final output is a binary image where white pixels represent the detected edges, and black pixels represent the background. These edges are typically thin, well-localized, and more continuous compared to simpler methods.
The Canny edge detector provides a robust way to identify significant edges while suppressing noise and insignificant details. Understanding its steps helps in interpreting its results and tuning its parameters (Gaussian sigma, Tlow, Thigh) for specific applications, which you will practice in the hands-on section.
© 2025 ApX Machine Learning