Canny Edge Detector Using Python
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:
- Noise Reduction: Gaussian convolution smooths the input image, reducing noise.
- 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.
- 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.
-
Hysteresis Thresholding: Two thresholds,
t1
(upper) andt2
(lower), witht1 > t2
, control edge tracking. Tracking starts at points abovet1
and continues until the gradient falls belowt2
. Points abovet1
are always edges; points belowt1
but abovet2
are edges only if connected to points abovet1
.
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):
The code:
from skimage import io, feature im = io.imread('boat.png') edges = feature.canny(im) io.imshow(edges) io.show()
The output (edge-detected image):
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()
Arguments: im
(image), lower threshold (25), upper threshold (255), L2gradient=False
(uses L1-norm). matplotlib
displays the results.
The output (edge-detected image):
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





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 within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

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 when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

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...

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...

Using python in Linux terminal...

Fastapi ...
