Python OpenCV - Canny() Function Last Updated : 23 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Canny edge detection algorithm is used in computer vision for identifying edges within an image. It helps in highlighting boundaries which are important for tasks like object detection and image segmentation. In this article, we will see how OpenCV's built-in Canny() function detects 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 Sobel filter.L2Gradient: Boolean parameter used for more precision in calculating Edge Gradient.Return Value: Returns an image with detected edges where edges are marked in white (255) and non-edges in black (0).1. Using Canny() FunctionInput Image:cv2.Canny(img, t_lower, t_upper): Applies Canny edge detection algorithm on the image with the given lower and upper threshold values.t_lower = 50: Sets lower threshold for edge detection in the Canny algorithm.t_upper = 150: Sets upper threshold for edge detection in the Canny algorithm.cv2.imshow('original', img): Displays the original image in a window.cv2.imshow('edge', edge): Displays the edge-detected image in a window. Python import cv2 img = cv2.imread("test.jpeg") t_lower = 50 t_upper = 150 edge = cv2.Canny(img, t_lower, t_upper) cv2.imshow('original', img) cv2.imshow('edge', edge) cv2.waitKey(0) cv2.destroyAllWindows() Output:2. Canny() function with Aperture_sizeThis is optional parameter that is used to specify the order of the Sobel filter used to calculate gradient in the Canny algorithm. We can increase Aperture size when we want to detect more detailed features.cv2.Canny(img, t_lower, t_upper, apertureSize=aperture_size): Applies Canny edge detection with a lower threshold (t_lower), upper threshold (t_upper) and Sobel filter aperture size (aperture_size) to detect edges in the image.aperture_size = 5: Defines the Sobel kernel size for gradient computation must be an odd number between 3 and 7 and a larger value captures more detailed edges. Python import cv2 img = cv2.imread("test.jpeg") t_lower = 100 t_upper = 200 aperture_size = 5 edge = cv2.Canny(img, t_lower, t_upper, apertureSize=aperture_size) cv2.imshow('original', img) cv2.imshow('edge', edge) cv2.waitKey(0) cv2.destroyAllWindows() Output: 3. Canny() function with L2GradientIt's a boolean parameter that specifies if we want to calculate usual gradient equation or the L2Gradient algorithm.aperture_size = 5: Specifies size of the Sobel kernel used to calculate the gradients and we set it to 5 here.L2Gradient = True: Enables more precise gradient calculation using the L2 norm.edge = cv2.Canny(img, t_lower, t_upper, L2gradient=L2Gradient): Applies the Canny edge detection algorithm to the image with the specified thresholds and L2 gradient Python import cv2 img = cv2.imread("test.jpeg") t_lower = 100 t_upper = 200 aperture_size = 5 L2Gradient = True edge = cv2.Canny(img, t_lower, t_upper, L2gradient = L2Gradient ) cv2.imshow('original', img) cv2.imshow('edge', edge) cv2.waitKey(0) cv2.destroyAllWindows() Output: 4. Canny() function with both Aperture size and L2gradientHere we will use both attributes within the same function. Python import cv2 img = cv2.imread("test.jpeg") t_lower = 100 t_upper = 200 aperture_size = 5 L2Gradient = True edge = 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: With the flexibility of the Canny() function we can experiment with different parameters to effectively detect edges and enhance the details in our images. Comment More infoAdvertise with us Next Article Python OpenCV - Canny() Function ayushmankumar7 Follow Improve Article Tags : Python OpenCV Python-OpenCV Practice Tags : python Similar Reads Essential OpenCV Functions to Get Started into Computer Vision Computer vision is a process by which we can understand the images and videos how they are stored and how we can manipulate and retrieve data from them. Computer Vision is the base or mostly used for Artificial Intelligence. Computer-Vision is playing a major role in self-driving cars, robotics as w 7 min read cv2.imread() method - Python OpenCV OpenCV-Python is a Python library used to solve computer vision tasks. cv2.imread() method loads an image from the specified file. If the image cannot be read because of missing file, improper permissions or an unsupported/invalid format then it returns an empty matrix.Example:Pythonimport cv2 image 2 min read Python OpenCV | cv2.imshow() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imshow() method is used to display an image in a window. The window automatically fits the image size. Syntax: cv2.imshow(window_name, image)Parameters: window_name: A string representing the name of the wi 3 min read Python OpenCV | cv2.cvtColor() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.cvtColor() method is used to convert an image from one color space to another. There are more than 150 color-space conversion methods available in OpenCV. We will use some of color space conversion codes be 4 min read Python OpenCV | cv2.imwrite() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory. Syntax: cv2.imwrite(filename, image) Parameters:file 2 min read Python OpenCV | cv2.rectangle() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.rectangle() method is used to draw a rectangle on any image. Syntax: cv2.rectangle(image, start_point, end_point, color, thickness) Parameters:image: It is the image on which rectangle is to be drawn. start 4 min read cv2.circle() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.circle() method is used to draw a circle on any image. We use this image:Example:Pythonimport cv2 path = r'C:\Users\user\Desktop\geeks14.png' src = cv2.imread(path) cv2.circle(src, center=(100, 100), radius 2 min read Python OpenCV | cv2.line() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.line() method is used to draw a line on any image.Syntax:cv2.line(image, start_point, end_point, color, thickness) Parameters: image: It is the image on which line is to be drawn. start_point: It is the sta 3 min read Python OpenCV | cv2.putText() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.putText() method is used to draw a text string on any image. Syntax: cv2.putText(image, text, org, font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) Parameters:image: It is the image on w 5 min read Line detection in python with OpenCV | Houghline method The Hough Transform is a method that is used in image processing to detect any shape, if that shape can be represented in mathematical form. It can detect the shape even if it is broken or distorted a little bit.We will see how Hough transform works for line detection using the HoughLine transform m 6 min read Like