We use the
opencv library to work with images and graphics for computer vision in Artificial Intelligence, Machine Learning, and more techniques. We can use the functionalities from this library to read and process images efficiently.
This tutorial will demonstrate the
cv2.canny() function in Python.
cv2.canny() Function in Python
cv2.canny() function implements the Canny Edge Detection algorithm developed by John F. Canny. We can use this function to detect edges from a given image.
The algorithm in itself has multiple stages.
The first stage involves noise reduction, and for this, the algorithm uses a 5x5 Gaussian Filter.
The next stage involves finding the intensity gradient for the given image. In this stage, the image is smoothened and then passed to the Sobel kernel, where it is filtered along the x and y-axis, and its gradient is found for each axis.
In the third stage, every pixel is checked with the local maxima in the direction of the gradient to remove the undesirable pixels that do not form an edge.
The final stage is the one where the edges are classified. Two threshold values,
maxVal, are taken.
Edges with gradient values greater than
maxValare the edges, and those below
minValare not. The rest of these threshold values are categorized based on their connectivity.
All the above stages are implemented by the
cv2.canny() function. It is necessary to know about these stages while determining the parameters for this function.
In the following example, we will detect the edges from an image.
import cv2 img = cv2.imread('deftstack.png') e = cv2.Canny(img, threshold1 = 50, threshold2 = 100) cv2.imshow('Edges Detected',e) cv2.waitKey(0) cv2.destroyAllWindows()
In the above example, we read the image using the
cv2.imread() function. The edges are detected for this image using the
Notice the two parameters in the function
threshold2. These two parameters are the value of the
maxVal threshold frequency discussed previously.
It is mandatory to provide these two values.
After classifying the edges for the given image, we display it in a new window using the
cv2.imshow() function. The
cv2.waitkey(0) function is used in the example to prevent the interpreter from closing the created window automatically and waiting for the user to press some key.
cv2.destroyAllWindows() function closes all the windows.
cv2.canny() function also accepts two additional optional parameters called
apertureSize parameter specifies the size of the aperture for the Sobel kernel.
By default, its value is three and can take any odd value between three and five. We can increase the
apertureSize to get more features from the image.
L2gradient takes a
False value, defaulting it as
If we specify the
L2gradient parameter as
True, then the new
L2Gradient algorithm is used to calculate the gradient value; otherwise, the traditional equation is used. The new algorithm tends to be a little more accurate.
We discussed how to use the
cv2.canny() function in this tutorial. We discussed the Canny Edge detection algorithm and how this function implements it internally.
The function was demonstrated with an example. We also discussed the parameters of this function.
Some were mandatory,
threshold2, while the others were optional,
- No Module Named CV2 in Mac in Python
- Image Masking in OpenCV
- Utilize Bitwise_AND on an Image Using OpenCV
- OpenCV Package Configuration
- OpenCV ArUco Markers
- SIFT Using OpenCV in Python