Python OpenCV – Canny() Function
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.
Canny Edge detection is an Algorithm consisting of 4 major steps:
- 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:

Basic example of Canny() function
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:

Canny() function with Aperture_size
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:

Canny() function with L2Gradient
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:

Canny() function with both Aperture size and L2gradient
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:




.png)