O'Reilly logo

Computer Vision with Python 3 by Saurabh Kapur

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Canny edge detector

Now, let's see the Canny() function in OpenCV. The Canny() function takes the image, min and max threshold values, and the size of the kernel as input. The following code shows how to use Canny edge detection in Python:

>>> import cv2>>> img = cv2.imread("image.jpg")>>> gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)>>> edges = cv2.Canny(gray, 100, 200, 3)>>> cv2.imwrite("canny_edges.jpg", edges)>>> cv2.imshow("canny_edges", edges)

Here is the output image of the preceding code:

Figure 15: The image on the left is the input image and the image on the right is the output of the Canny edge detector

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required