How to use Canny Edge detector with Python OpenCV

How to use Canny Edge detector with Python OpenCV

How to use Canny Edge detector with Python OpenCV


Canny Edge Detection is a popular image processing technique used for detecting edges in images. It is a robust algorithm based on a multi-step process, including Gaussian filtering, gradient calculation, and edge tracking.

In this tutorial, we will explore:

  1. The theory behind Canny Edge Detection.
  2. Its implementation in Python using OpenCV.
  3. The key parameters and their impact on the result.

The Theory Behind Canny Edge Detection

Canny Edge Detection involves several steps:

  1. Gaussian Filtering: Removes noise from the image using a Gaussian filter, which smooths the image.
  2. Gradient Calculation: Determines the gradient intensity and direction using Sobel operators.
  3. Non-Maximum Suppression: Thins out the detected edges to a single pixel by suppressing weaker responses.
  4. Hysteresis Thresholding: Edges are classified as strong or weak, and weak edges are preserved only if they are connected to strong ones.

Code Example

Below is a practical example of using the Canny edge detector with OpenCV. The first step is to create a new file named edges.py, then add the following content:

import cv2
import numpy as np

image_path = 'images/drop.jpg' 
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

blurred = cv2.GaussianBlur(image, (5, 5), 1.4)

low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blurred, low_threshold, high_threshold)

cv2.imwrite("edges.jpg", edges)



Detailed Explanations

Image loading
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)


For this example, we are using the image drop.jpg from the images test directory, but you can experiment with any other image.

The image is loaded in grayscale because the Canny algorithm operates on monochrome images.

Gaussian Filtering
blurred = cv2.GaussianBlur(image, (5, 5), 1.4)


  • The Gaussian kernel (5, 5) defines the size of the matrix used for smoothing the image. Larger sizes reduce more noise but may blur fine details.
  • Sigma 1.4 controls the intensity of the smoothing effect.
Edge Detection
edges = cv2.Canny(blurred, low_threshold, high_threshold)


  • low_threshold and high_threshold control hysteresis. Strong edges (with a high gradient) are immediately classified, while weak edges are kept only if they are connected to strong ones.
  • Adjusting these thresholds can influence the sensitivity of the algorithm.
Saving the image
cv2.imwrite("edges.jpg", edges)


To view the image, you first need to save it with the applied changes, and then you can open it in the VSCode editor.

To run this code execute the following command in the terminal:

python3 edges.py


After running the program, a new file named edges.jpg should be created in the working folder. This file contains the resulting image after applying the Canny filter and should look similar to the image below:

Key Parameters and Their Effects

  1. Gaussian Kernel Size:

    • Larger sizes remove more noise but can also eliminate fine details.
  2. Lower and Upper Thresholds:

    • Lower values detect more edges, including noise.
    • Higher values reduce noise but may miss important details.
Experiment by adjusting these values to observe their impact on the results.

Conclusion

The Canny Edge Detector is an effective method for edge detection in images. By tuning its parameters, it can be adapted for a wide range of applications, such as object recognition, image segmentation, or contour tracking. With this tutorial, you can understand how to implement this technique and optimize the results to fit your specific needs.


Trebuie să fii autentificat pentru a accesa laboratorul cloud și a experimenta cu codul prezentat în acest tutorial.

Autentifică-te