How to Detect the Edges of an Image using the Canny Edge Detection in Python using OpenCV


In this article, we show how to detect the edges of an image using the canny edge detection method in Python using the OpenCV module.

The Canny Edge Detection Method is one of several ways to detect edges in an image using computer vision software such as OpenCV.

Canny edge detection is a multi-stage algorithm to detect a wide range of edges in images. It utilizes nose supression using a Gaussian filter to reduce noise and unwanted details to simplify the image.

So in this example, we're going to try a simple image shown below with circles.

Circles with multiple colors

You can download this image above at the following link: Circles.

We then turn perform canny edge detection on the image, which converts the image into the following image below.

Image with Canny edge detection in Python using OpenCV

Looking at the image above, you can see how it clearly outlines the 3 circles in the original image.

You can see that the canny edge image is in black white. The Canny Edge method simplifies the image to the point that it isn't concerned about colors and other noise. It simply is there to identify edges found on objects within the image. For this reason, it is standard practice to take the original image and first convert it to grayscale. Then we take this simplified grayscale image and use the canny edge to find the edges in the image. However, you can still get good results converting it directly from the original image and using the Canny Edge Detection method.

So let's now go over the code to turn an original image into an image where the edges are detected using the Canny Edge Detection method.

Let's now go over this code.

First, we import OpenCV using the line, import cv2

Next, we read in the image that we want to detect the images of using the canny method. In this case, it is, Circles.png

Next, we create a variable, edges, which stores the Canny image version of the image, which is the image with the edges outlined in black and white color. The cv2.Canny() function allows us to get the Canny version of the image. The first parameter is the image it is reading in. The second and third parameters are the minimum and maxium aperture sizes. It can be difficult to explain this, so these can be kept standard.

We then show the image.

So this is one way of doing it.

The other method is simply to first convert the original image to grayscale and then apply the Canny Edge Detection method.

This is shown below in the code below.

So all the code is basically the same as before but we create a grayscale version of the image and then pass this into the Canny Edge Detection method.

The resultant output image is largely the same as if passing in the original image for this photo, but it is common practice to first convert images to grayscale before using the Canny method, as a grayscale image is largely simplified and the Canny method works with a simpler image. Converting the image to grayscale does not degrade the image in any way because after all, the Canny method only produces black and white images. So grayscale does not affect the edge detection in any way.

And this is how we can detect edges in an image using the Canny Edge Detection method in Python using OpenCV.

Related Resources

How to Draw a Rectangle in Python using OpenCV

How to Draw a Circle in Python using OpenCV

How to Draw a Line in Python using OpenCV

How to Add Text to an Image in Python using OpenCV

How to Display an OpenCV image in Python with Matplotlib

How to Use Callback functions to Connect Images to Events in Python using OpenCV

How to Check for Multiple Events in Python using OpenCV

HTML Comment Box is loading comments...