Python OpenCV – Canny() Function

22 Nov, 2021
In this article, we will see the Canny Edge filter in OpenCV. Canny() Function in OpenCV is used to detect the edges in an image.
Syntax: cv2.Canny(image, T_lower, T_upper, aperture_size, L2Gradient)
Where: 
Image: Input image to which Canny filter will be applied
T_lower: Lower threshold value in Hysteresis Thresholding
T_upper: Upper threshold value in Hysteresis Thresholding
aperture_size: Aperture size of the Sobel filter.
L2Gradient: Boolean parameter used for more precision in calculating Edge Gradient.
Reduce Noise using Gaussian Smoothing.
Compute image gradient using Sobel filter.
Apply Non-Max Suppression or NMS to just jeep the local maxima
Finally, apply Hysteresis thresholding which that 2 threshold values T_upper and T_lower which is used in the Canny() function.
Input Image:
Python3
import cv2  img = cv2.imread("test.jpeg")  # Read image  # Setting parameter valuest_lower = 50  # Lower Thresholdt_upper = 150  # Upper threshold  # Applying the Canny Edge filteredge = cv2.Canny(img, t_lower, t_upper)  cv2.imshow('original', img)cv2.imshow('edge', edge)cv2.waitKey(0)cv2.destroyAllWindows()
Output:
This is an optional parameter that is used to specify the order of the Sobel filter used to calculate the gradient in the Canny algorithm. The default value is 3 and its value should be odd between 3 and 7. You can increase the Aperture size when you want to detect more detailed features.
Python3
import cv2  img = cv2.imread("test.jpeg")  # Read image  # Setting All parameterst_lower = 100  # Lower Thresholdt_upper = 200  # Upper thresholdaperture_size = 5  # Aperture size  # Applying the Canny Edge filter# with Custom Aperture Sizeedge = cv2.Canny(img, t_lower, t_upper,                  apertureSize=aperture_size)cv2.imshow('original', img)cv2.imshow('edge', edge)cv2.waitKey(0)cv2.destroyAllWindows()
Output: 
It’s a boolean parameter that specifies if you want to calculate the usual gradient equation or the L2Gradient algorithm. Again, it’s an optional parameter. L2gradient is nothing my sqrt(gradient_x_square + gradient_y_square) whereas L1gradient is just abs(gradient_x) + abs(gradient_y).
Python3
import cv2  img = cv2.imread("test.jpeg") # Read image  t_lower = 100 # Lower Thresholdt_upper = 200 # Upper thresholdaperture_size = 5 # Aperture sizeL2Gradient = True # Boolean  # Applying the Canny Edge filter with L2Gradient = Trueedge = cv2.Canny(img, t_lower, t_upper, L2gradient = L2Gradient )  cv2.imshow('original', img)cv2.imshow('edge', edge)cv2.waitKey(0)cv2.destroyAllWindows()
 Output: 
Here we will use both attributes within the function.
Python3
import cv2   img = cv2.imread("test.jpeg") # Read image  # Defining all the parameterst_lower = 100 # Lower Thresholdt_upper = 200 # Upper thresholdaperture_size = 5 # Aperture sizeL2Gradient = True # Boolean  # Applying the Canny Edge filter # with Aperture Size and L2Gradientedge = cv2.Canny(img, t_lower, t_upper,                 apertureSize = aperture_size,                  L2gradient = L2Gradient )   cv2.imshow('original', img)cv2.imshow('edge', edge)cv2.waitKey(0)cv2.destroyAllWindows()
Output: 
Picked
Python-OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Python | datetime.timedelta() function