Home Backend Development Python Tutorial Canny Edge Detector Using Python

Canny Edge Detector Using Python

Feb 28, 2025 am 09:49 AM

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

See all articles