Canny Edge Detection
Canny edge detection, proposed by John Canny in 1986, is considered a highly effective and widely used edge detection algorithm. It aims to find optimal edges in images corrupted by white noise, based on a set of specific criteria.
Scale-Space Concept (Context)
(Relevant context mentioned in Sonka, p. 144)
- Edges can exist at multiple scales. Analyzing features at different scales can be done using scale-space representation.
- An image is smoothed by convolution with a Gaussian kernel at varying standard deviations (): (5.51 - 1D Gaussian)
- The resulting function (5.52) creates a scale-space image over the plane.
- Inflexion points in for a fixed describe the curve qualitatively. These occur where: and (5.53)
- Tracking these inflexion points across different scales () helps localize events from coarse to fine scales. Canny edge detection utilizes scale as part of its process.
Optimality Criteria (Canny’s Design Goals)
Canny sought an edge detector that optimizes three main criteria for step edges corrupted by white noise:
-
Good Detection:
- Maximize the probability of detecting true edges (low false negatives).
- Minimize the probability of detecting non-edges (low false positives).
- Related to maximizing the Signal-to-Noise Ratio (SNR).
- SNR Definition (CMU Slide 3): where is the filter, is the edge signal (e.g., step edge), is the mean-squared noise amplitude per unit length, and the denominator represents the filter’s root-mean-squared response to noise only.
-
Good Localization:
- The detected edge position should be as close as possible to the actual edge center.
- Related to minimizing the distance between the detected and true edge, often measured by the reciprocal of the root mean square (rms) distance.
- Localization Definition (CMU Slide 3): where is the expected squared distance of the marked edge from the center. The numerator relates to the second derivative of the filtered response, indicating the steepness of the zero-crossing. Sharper slope means better localization.
-
Single Response (One Response Criterion):
- Minimize the number of local maxima around a single edge. Ideally, only one response per true edge.
- This criterion helps prevent multiple detections for a single edge, especially in the presence of noise, and works against non-smooth operators.
- Related to maximizing the distance between peaks in the noise response.
- Inter-maximum Spacing (CMU Slide 5): The mean distance between zero-crossings of the filtered noise response derivative is related to the filter characteristics:
- This distance is constrained to be a fraction of the operator width : .
Derivation and Filter Approximation
- Canny initially derived a filter for 1D signals optimizing the first two criteria using the calculus of variations.
- Adding the third criterion (single response) requires numerical optimization.
- Result: The optimal filter can be effectively approximated (error < 20%) by the first derivative of a Gaussian smoothing filter.
- This approximation provides an efficient implementation.
- There is a similarity to the LoG (Laplacian of Gaussian) / Marr-Hildreth detector, but Canny uses the first derivative for detection and localization, which provides directional information, unlike the non-directional Laplacian.
Generalization to 2D
- A 2D step edge is defined by position, orientation, and magnitude (strength).
- The 1D approach is generalized by:
- Convolving the image with a symmetric 2D Gaussian .
- Differentiating the smoothed image in the direction n perpendicular (normal) to the edge.
- Let be the operator representing the first derivative of in direction n: (5.54)
- The edge normal direction n is not known a priori but can be estimated from the gradient direction of the smoothed image: (5.55)
- Edge Location: Edges are located at points where the gradient magnitude is maximal in the direction n. This corresponds to zero-crossings of the second derivative in the direction n: (5.56)
- Using the associativity of convolution and differentiation, this condition becomes: (5.57)
- This equation forms the basis for non-maximal suppression.
- Edge Strength: The magnitude (strength) of the edge is measured by the gradient magnitude of the smoothed image: (5.58)
Algorithm Steps (Algorithm 5.4 / Combined View)
-
Noise Reduction (Gaussian Smoothing):
- Convolve the input image with a Gaussian kernel of scale .
- The choice of affects the scale of edges detected. Larger reduces noise more but blurs edges.
-
Gradient Calculation:
- Compute the gradient components (, ) of the smoothed image (e.g., using Sobel operators).
- Calculate the Gradient Magnitude: (approximates edge strength, Eq 5.58).
- Calculate the Gradient Direction: (estimates direction perpendicular to the edge, related to n in Eq 5.55).
-
Non-Maximal Suppression (NMS):
- Goal: Thin wide ridges around edges down to single-pixel width (improves localization).
- Process: For each pixel, check if its gradient magnitude is a local maximum along the gradient direction .
- Quantify into discrete directions (e.g., horizontal, vertical, +45°, -45°).
- Compare the pixel’s magnitude with the magnitudes of its two neighbors along the gradient direction.
- Interpolation: Since the gradient direction is continuous, the neighbors’ magnitudes often need to be interpolated. For example (CMU Slide 12):
- If the gradient direction points between pixel and , interpolate the gradient magnitude at point A along the gradient line using magnitudes and . Let this be .
- Similarly, interpolate the magnitude at point B on the opposite side using and . Let this be .
- Interpolation formula (example for ): (assuming gradient vector is and ). Note: Simpler linear interpolation based on angle is common.
- Suppression Rule: If is less than either interpolated neighbor magnitude ( or ), suppress the pixel (set its magnitude to 0). Otherwise, keep .
-
Hysteresis Thresholding:
- Goal: Separate significant edges from spurious responses (improve detection) and bridge gaps in edge contours (‘streaking’ problem).
- Uses two thresholds: (low threshold) and (high threshold), with .
- Threshold Determination (CMU Slide 7): Thresholds can be set adaptively based on image statistics, e.g., using percentiles (like the median) of the gradient magnitude histogram. Canny suggested they could be slowly varying functions across the image.
- Procedure: a. Pixels with magnitude are marked as strong (‘seed’) edge pixels. b. Pixels with magnitude are marked as weak edge pixels. c. Pixels with magnitude are suppressed (non-edges). d. Keep all strong edge pixels. e. Keep weak edge pixels only if they are connected (via 8-connectivity) to a strong edge pixel, either directly or through a path of other connected weak pixels. Discard unconnected weak pixels.
-
Multi-Scale Aggregation / Feature Synthesis (Optional - Sonka p. 146):
- The standard Canny algorithm (steps 1-4) is often run at a single scale .
- Canny also proposed a ‘feature synthesis’ approach to handle multiple scales:
- Run the detector (steps 1-5) for a sequence of increasing .
- Start with the smallest scale . Mark significant edges.
- For the next scale , predict the edge response based on the edges found at .
- Compare the predicted response with the actual response at . Mark additional edges only if they are significantly stronger than predicted.
- Build a cumulative edge map. Edges are typically localized using the smallest scale at which they were reliably detected.
- Note: Full feature synthesis is less commonly implemented than the single-scale version.
- Figure 5.23: Shows Canny edge detection results at two different scales ( and , without feature synthesis). Larger results in smoother, less detailed edges, removing weaker features and noise.
Parametric Edge Models (Facet Model - Alternative/Related Concept)
(Sonka p. 147, CMU Slides 13, 14)
- An alternative approach to edge detection models the underlying continuous image intensity function in local neighborhoods using parametric surfaces, called facets.
- The discrete image is seen as a sampled, noisy version of this underlying function.
- Model Complexity:
- Flat Facet: Piecewise constant intensity.
- Sloped Facet: Piecewise linear function (plane).
- Quadratic/Bi-cubic Facet: Higher-order polynomials.
- Example Polynomial Fit:
- Parameter Estimation: Coefficients ( to ) are fitted to the pixel intensities in a neighborhood by minimizing the Mean Square Error (MSE) or Euclidean norm:
- Edge Detection: Once the continuous facet model is estimated for each neighborhood, edges can be detected with subpixel precision as:
- Extrema of the first directional derivative.
- Zero-crossings of the second directional derivative of the facet function .
- This approach allows for robust edge detection and characterization directly from the estimated continuous surface.
Comparison with Simpler Operators
(CMU Slides 15, 16)
- Robert’s Cross Operator: Simpler diagonal gradient operator. Derived from fitting a least-squares planar surface over a window. , or ,
- Prewitt Operator: operator, approximates gradient. Derived by fitting a quadratic surface over a window and differentiating. , (Note: in slide seems transposed relative to common definition).
- Sobel Operator: Similar to Prewitt but gives more weight to center pixels (gradient of a surface smoothed by a mean filter). ,
- Canny vs Simpler Operators: Canny provides superior noise suppression, localization, and single-edge response due to its multi-stage approach (Gaussian smoothing, NMS, hysteresis) based on optimizing specific criteria, whereas Roberts, Prewitt, and Sobel are simpler approximations of the gradient.