Get a head start on your coding projects with our Python Code Generator. Perfect for those times when you need a quick solution. Don't wait, try it today!
Edge detection is an image processing technique for finding the boundaries of objects within images. It mainly works by detecting discontinuities in brightness. One of the most popular and widely used algorithms is the Canny edge detector.
Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images.
The main stages are:
Learn more here about the theory behind the Canny edge detector.
Read also: Image Transformations using OpenCV in Python.
Alright, let's implement it in Python using OpenCV, installing it:
Open up a new Python file and follow along:
Now let's read the image when want to detect its edges:
I have an example image in my current directory, make sure you do too.
Before we pass the image to the Canny edge detector, we need to convert the image to grayscale:
Let's see it:
All we need to do now is to pass this image to cv2.Canny()
function which finds edges in the input image and marks them in the output map edges using the Canny algorithm:
Learn also: Real-time Object Tracking with OpenCV and YOLOv8 in Python.
The smallest value between threshold1
and threshold2
is used for edge linking. The largest value is used to find initial segments of strong edges.
Let's see the resulting image:
Interesting, try to fine-tune the threshold values and see if you can make it better.
Building a real-time automatic number plate recognition system using YOLO and OpenCV library in Python
Download EBookIf you want to use the live camera, here is the full code for that:
Alright, we are done!
The purpose of detecting edges is to capture important events and changes in the properties of the world. It is one of the fundamental steps in image processing, image pattern recognition, and computer vision techniques.
Learn also: How to Apply HOG Feature Extraction in Python.
Happy Learning ♥
Loved the article? You'll love our Code Converter even more! It's your secret weapon for effortless coding. Give it a whirl!
View Full Code Transform My Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!