Home > Backend Development > Python Tutorial > Canny Edge Detector Using Python

Canny Edge Detector Using Python

William Shakespeare
Release: 2025-02-28 09:49:10
Original
883 people have browsed it

Edge detection is a crucial image analysis technique for object recognition based on outlines and is vital for image information recovery. It extracts key features like lines and curves, often used by advanced computer vision and image processing algorithms. A robust edge detection algorithm accurately identifies major edges while suppressing noise-induced false edges.

Edges represent significant local changes in image intensity (pixel values), typically occurring at region boundaries. This tutorial explains the Canny edge detection algorithm and its Python implementation.

The Canny Edge Detector

Named after its inventor, John F. Canny (1986), the Canny detector takes a grayscale image as input and outputs an image highlighting intensity discontinuities (edges).

The process involves:

  1. Noise Reduction: Gaussian convolution smooths the input image, reducing noise.
  2. Gradient Calculation: A first derivative operator highlights areas with high spatial derivatives. Gradient magnitude and direction are determined using x and y derivatives, crucial for edge direction identification.
  3. Non-Maximal Suppression: This step thins the edges. The algorithm traces along gradient ridges, setting non-ridge pixels to zero, resulting in a thin edge line. This involves comparing the gradient to its neighbors; only the maximal gradient is retained.
  4. Hysteresis Thresholding: Two thresholds, t1 (upper) and t2 (lower), with t1 > t2, control edge tracking. Tracking starts at points above t1 and continues until the gradient falls below t2. Points above t1 are always edges; points below t1 but above t2 are edges only if connected to points above t1.

The Gaussian kernel width and the t1/t2 thresholds are parameters influencing the Canny detector's output.

Python Implementation

Two methods are shown: using scikit-image and OpenCV.

Using scikit-image

Install scikit-image (e.g., sudo apt-get install python-skimage on Ubuntu). The canny() function (in the feature module) applies the Canny detector.

Using the sample image "boat.png" (shown below):

Canny Edge Detector Using Python

The code:

from skimage import io, feature

im = io.imread('boat.png')
edges = feature.canny(im)
io.imshow(edges)
io.show()
Copy after login
Copy after login

The output (edge-detected image):

Canny Edge Detector Using Python

Parameter adjustments yield varying edge detection results.

Using OpenCV

Install OpenCV (see relevant installation guides for your operating system). OpenCV's Canny() function performs edge detection.

The code:

from skimage import io, feature

im = io.imread('boat.png')
edges = feature.canny(im)
io.imshow(edges)
io.show()
Copy after login
Copy after login

Arguments: im (image), lower threshold (25), upper threshold (255), L2gradient=False (uses L1-norm). matplotlib displays the results.

The output (edge-detected image):

Canny Edge Detector Using Python

Conclusion

This tutorial covered the Canny edge detector and its straightforward implementation using scikit-image and OpenCV, demonstrating its effectiveness in edge detection.

The above is the detailed content of Canny Edge Detector Using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template