Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does cv2.Canny() perform a Gaussian blur?

I know it's important to apply Gaussian blur to an image before using Canny to detect edges. My question is: does cv2.Canny() do Gaussian blur on its own or it is necessary to apply cv2.GaussianBlur() before cv2.Canny()? The documentation isn't clear on this point.

like image 609
DLopezG Avatar asked Oct 27 '25 17:10

DLopezG


1 Answers

Recall that using OpenCV in Python is in fact a wrapper to OpenCV's C++ interface. The Canny edge detection algorithm's implementation file in C++ can be found here: https://github.com/opencv/opencv/blob/master/modules/imgproc/src/canny.cpp

If you examine this source, you can see that there is no blurring done on the image whatsoever. However, thanks to the correction made by Yves Daoust (see below), the Sobel edge detection algorithm is used for finding the gradient. There is an aperture input parameter that specifies the size of the Sobel kernel. Using the default means you're using a 3 x 3 kernel and no blurring is applied. However, anything above 3 x 3 will use a Gaussian kernel. Therefore, if you want to use the default Sobel kernel, you would need to apply the blurring yourself. If you vary the size of the Sobel kernel you don't have to perform blurring yourself.

In fact, in the official OpenCV tutorial where they demonstrate the use of Canny, the image is blurred manually prior to the detection: https://docs.opencv.org/3.4/da/d5c/tutorial_canny_detector.html. However, they use a 3 x 3 box filter here prior to the algorithm as you're using a 3 x 3 Sobel kernel.

tl;dr - You need to blur the image yourself prior to using the Canny edge detection algorithm if you decide to use the default aperture / Sobel kernel size. If you go larger than 3, you don't have to blur the image.

like image 51
rayryeng Avatar answered Oct 30 '25 08:10

rayryeng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!