what is edge detection ?
Edge detection is an image processing technique for finding the boundaries of objects within images. It works by detecting discontinuities in brightness.
where edge detection is used ?
Edge detection is used for image segmentation and data extraction in areas such as image processing, computer vision, and machine vision, so knowing how to do it will eventually pay you off.
There are several edge detection algorithms and different libraries supporting it but in this tutorial, I’m going to show you how to do it using OpenCV using the Canny algorithm.
#In Window pip install opencv-python pip install matplotlib #In Linux pip3 install opencv-python pip3 install matplotlib
Let’s get started
Once we have installed now we ready to go to detecting edges with python using Canny algorithms.
we are going to use OpenCV method imread( ) to load an image from the file, Canny() to detect the edges, and then finally visualising the images before detection and after using Matplotlib
Reading images with OpenCV
To read an image from file using imread() method you need to provide two parameter, one of path to to our image and the next one is mode of reading which can either by gray, rgb, hsv, hsl, and etc.
OpenCV syntax to read image
import cv2 image = cv2.imread(path_to_image, mode_of_reading)
Canny algorithms usually works well when the image is in gray scale , when you put 0 on mode of reading opencv just interpret it as grayscale reading.
Using Canny algorithms to detect the edges
To detect edges with Canny you have to specify your raw image, lower pixel threshold, and higher pixel threshold in the order shown below;
image_with_edges = cv2.Canny(raw_image, l_threshold, h_theshold)
How threshold affect edge detection?
The intensity gradient of a pixel is greater than the higher threshold, it will be added as an edge pixel in the output image otherwise it will be rejected completely.
Finalizing our code and Visualizing it with Matplotlib
Now that’s we learned the basics of OpenCV together with Canny detection algorithms now let’s put them together to sample real-world application
Let’s detect the edges in the below sample case image road.jpg
Using the knowledge we just learned above, I have bundled everything together to come up with below final code below, when you run it, it will load an image, perform detection and display it using Matplotlib.
import cv2 import matplotlib.pyplot as plt def detect_edge(image): ''' function Detecting Edges ''' image_with_edges = cv2.Canny(image , 100, 200) images = [image , image_with_edges] location = [121, 122] for loc, img in zip(location, images): plt.subplot(loc) plt.imshow(img, cmap='gray') plt.savefig('edge.png') plt.show() image = cv2.imread('road.jpg', 0) detect_edge(image)
Once Executed it will produce the below results
If you find this post interesting, don’t forget to subscribe to get more posts like this.
I recommend you to also check this;
- Build a real-time bar-code reader in Python
- How to convert picture to sound in Python
- Realtime vehicle detection in Python in 5 minutes
- Getting started with image processing using a pillow
Also in case of anything, drop it in the comment box below and I will get back to you ASAP
To get the full code for this article check out o on My Github