Canny edge preprocessing guide
Canny edge detection is the most-used ControlNet preprocessor: it turns a source image into a clean black-and-white edge map that constrains the structure of the generated image. Its output is almost entirely controlled by two numbers — the low and high thresholds — plus the aperture size and the L2 gradient toggle. Get the thresholds wrong and you either lose the composition or trace every speck of noise.
How Canny works under the hood
Canny finds edges in five stages:
- Noise reduction — a Gaussian blur smooths the image first, dampening pixel noise before gradient computation.
- Gradient calculation — a Sobel operator computes the intensity gradient at each pixel. The aperture size controls how large this kernel is.
- Non-maximum suppression — only local maxima along the gradient direction are kept; edges become thin single-pixel lines.
- Double threshold — pixels above the high threshold are definite edges; pixels between low and high are “weak” candidates.
- Hysteresis — weak pixels adjacent to strong ones are kept; isolated weak pixels are discarded as noise.
The hysteresis step is what makes Canny superior to a simple single-threshold approach: it traces complete edge contours without flooding the map with noise.
Threshold recommendations by source type
| Source type | Low threshold | High threshold | Notes |
|---|---|---|---|
| Clean line art, sketches | 30–80 | 80–150 | Capture every line; noise is not a concern |
| Portrait photo, smooth skin | 80–120 | 160–220 | Balance face edges vs skin texture noise |
| Landscape / general photo | 100–150 | 200–300 | Suppress grass/leaf texture, keep horizon |
| Architecture, hard edges | 80–120 | 150–250 | Fine lines and geometry; less texture noise |
| Noisy or grainy photo | 120–180 | 250–350 | Raise both and use aperture 5 or 7 |
| Product / 3D render | 50–100 | 100–200 | Clean source; lower thresholds safe |
These are starting points; iterate from the defaults and adjust based on what the map over- or under-represents.
What the aperture size actually changes
The aperture is the Sobel kernel size — the neighbourhood of pixels used to estimate the gradient at each point. Aperture 3 (default) looks at a 3×3 region: fast and fine-grained. Aperture 5 looks at a 5×5 region, effectively pre-smoothing the image before detecting, which is useful for high-resolution photos where small textures produce spurious gradients. Aperture 7 smooths further still and should only be used on very high-resolution or very noisy sources. Most generation-size (512–1024px) images work best with aperture 3.
Tips for the right edge density
- Match thresholds to source. Line art wants low thresholds (catch every line); busy photos want high thresholds (keep only major contours).
- Widen the gap for more detail. A bigger spread between low and high (say 50/200) preserves more connected faint edges than a narrow one.
- Use aperture 5 for noisy images. Larger apertures smooth before detecting, giving cleaner maps from grainy or high-resolution photos.
- Save L2 gradient for finals. It is slightly slower but produces crisper, more accurate edges when the map quality matters.
- Preview at generation resolution. A map that looks fine at the source image size can look over-dense or sparse once scaled to the generation dimensions. Check the map at the actual input size ControlNet will receive.
- Too many edges? Raise both thresholds proportionally — the ratio between them matters as much as the individual values.
- Too few edges and the composition is lost? Lower the high threshold first; the low threshold controls the sensitivity of the connectivity step.