OpenCV Python Canny Edge Detection

In this OpenCV Python article we are going to talk about Canny Edge Detection, but first of all let’s talk about OpenCV and Installation of OpenCV.

 

 

What is OpenCV?

OpenCV or Open Source Computer Vision Library is an open-source computer vision and machine learning software library. It’s used for building computer vision applications and to accelerate the development of different vision-related tasks.

 

 

How to Install OpenCV for Python?

You can install OpenCV for Python using pip command like this.

 

 

 

What Is Canny Edge Detection ?

OpenCV also offers a very handy function called Canny Edge Detection, the inventor of this algorithm was, John F. Canny), which is very popular not only because of its effectiveness, but also the simplicity of its implementation in an OpenCV program.

 

 

These are the steps for Canny Edge Detection

1: Noise Reduction : Since edge detection is susceptible to noise in the image, first step is to remove the noise in the image with a 5×5 Gaussian filter.

2: Calculate the gradients

3: Non-maximum Suppression: After getting gradient magnitude and direction, a full scan of image is done to remove any unwanted pixels

which may not constitute the edge. for this, at every pixel, pixel is checked if it is a local maximum in its neighborhood in the direction of gradient.

4: Double threshold on all the detected edges

5: Analyzes all the edges and their connection

 

 

So now this is the complete code for OpenCV Python Canny Edge Detection

 

So in all five steps OpenCV puts all the above in single function, cv2.Canny(). We will see how to use it. First argument is our input image. Second and third arguments are our minVal and maxVal respectively. Third argument is aperture_size. It is the size of Sobel kernel used for find image gradients. By default it is 3. Last argument is L2gradient which specifies the equation for finding gradient magnitude. If it is True, it uses the equation mentioned above which is more accurate, otherwise it uses this function: Edge_Gradient(G)=|Gx|+|Gy|. By default, it is False.

 

 

This line of code is for reading our image make sure to have an image in your working directory.

 

 

Also this is our Canny function, we have our input image, minValue and maxValue.

 

 

 

 

 

Run the complete code and this will be the result

OpenCV Python Canny Edge Detection
OpenCV Python Canny Edge Detection

 

 

FAQs:

 

How to use Canny edge detection in Python?

For using Canny edge detection in Python with OpenCV, follow these steps:

  • Install OpenCV: Ensure you have OpenCV installed. You can install it using pip:

 

 

  • Read the Image: Load the image you want to process.

 

  • Apply Canny Edge Detection: Use cv2.Canny() function

 

 

  • Display the Result: Show the original and edge-detected images.

 

 

 

What is Canny edge detection OpenCV?

Canny edge detection is a multi step algorithm, and it is used to detect different edges in images. It was developed by John F. Canny in 1986 and is known for its efficiency and accuracy. OpenCV provides a function cv2.Canny() for applying this algorithm. The steps involved in Canny edge detection are:

  1. Noise Reduction: Applying a Gaussian filter to smooth the image and reduce noise.
  2. Gradient Calculation: Calculating intensity gradients of the image.
  3. Non-Maximum Suppression: Removing spurious response to edge detection.
  4. Double Thresholding: Determining potential edges.
  5. Edge Tracking by Hysteresis: Finalizing edge detection by suppressing all the other edges that are weak and not connected to strong edges.

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×