Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Canny Edge Detection

Canny edge detection is a multistage algorithm that reduces noise, detects sharp brightness changes, and creates thin edge maps for computer vision.

Canny edge detection is a multistage image-processing algorithm that identifies sharp changes in brightness and converts them into a thin, binary edge map. It reduces noise, locates intensity transitions, and links reliable edge pixels into contours. In computer vision, these contours describe image structure for shape analysis and measurement.

John Canny developed the method in 1983 and described it in his 1986 paper, A Computational Approach to Edge Detection. Its design balances three goals: detecting genuine edges, locating them accurately, and producing one response per edge. "Canny edge detector" is a synonym for the algorithm. Consumer-product and game uses of "Canny" refer to unrelated subjects.

How Canny Edge Detection Works#

The algorithm combines data preprocessing with filtering and connectivity analysis. Its five stages have distinct jobs:

  • Noise reduction: Gaussian smoothing averages nearby pixels with distance-dependent weights, suppressing fluctuations that would create unwanted edges.
  • Gradient estimation: Horizontal and vertical derivatives measure how rapidly brightness changes. Their combination gives edge strength and a direction perpendicular to the boundary. OpenCV uses Sobel derivatives for this stage.
  • Edge thinning: Non-maximum suppression retains local gradient peaks along the gradient direction, producing narrow ridges instead of broad bands.
  • Double thresholding: A high threshold identifies strong edges; a low threshold removes weak responses. Pixels between the thresholds become candidates.
  • Hysteresis linking: Candidate pixels survive when connected to a strong edge through other candidates. Isolated candidates disappear.

Thresholds, Smoothing, and Failure Modes#

The thresholds operate on gradient strength, rather than raw brightness or model confidence. Lowering them retains more detail and noise; raising them removes weak boundaries. Raising the high threshold also reduces the strong-edge seeds available for hysteresis linking.

Gaussian scale controls a separate trade-off. Greater smoothing suppresses noise but removes fine structures. In scikit-image's noisy-square example, changing the Gaussian standard deviation changes how much noise survives around the square.

In crack inspection, rough concrete surfaces produce unwanted texture edges and incomplete crack contours. Increasing the thresholds removes some false edges while also losing genuine crack sections.

Sobel estimates intensity derivatives; Canny combines gradient estimation with smoothing, thinning, and connectivity-based selection. Canny's thinning stage compares neighboring pixels, while object-detection NMS compares overlapping predicted boxes.

Canny Edge Detection in Python#

Install the dependencies with pip install opencv-python numpy. This example uses OpenCV to save an edge map of the bus sample:

from urllib.request import urlopen

import cv2
import numpy as np

with urlopen("https://ultralytics.com/images/bus.jpg") as response:
    encoded = np.frombuffer(response.read(), dtype=np.uint8)
gray = cv2.imdecode(encoded, cv2.IMREAD_GRAYSCALE)

# Smooth the image before calculating intensity gradients.
smoothed = cv2.GaussianBlur(gray, (5, 5), 1.4)
edges = cv2.Canny(smoothed, 50, 150, L2gradient=True)
cv2.imwrite("canny_edges.png", edges)

The saved PNG shows retained edges in white against a black background. In the OpenCV edge-detection workflow, Canny's second and third arguments specify the low and high thresholds. L2gradient=True uses Euclidean gradient magnitude; the default uses the sum of absolute horizontal and vertical derivatives. These choices change gradient values and therefore threshold behavior.

Canny supports geometric feature extraction: scikit-image's line-detection example applies it to a cameraman photograph before a probabilistic Hough transform extracts straight segments. Canny supplies edge pixels; Hough voting groups them into lines. Object detection assigns categories and boxes, while instance segmentation assigns pixels to individual objects.

Ultralytics YOLO26 segmentation returns masks for object isolation, which can select image regions for separate Canny processing. This combines learned object selection with classical boundary extraction.

In generative AI, ControlNet uses Canny maps as spatial conditions for diffusion models. Its Canny-conditioned implementation guides Stable Diffusion 1.5 generation using extracted outlines alongside a text prompt.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning