Open In App

Implement Canny Edge Detector in Python using OpenCV

Last Updated : 31 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Canny edge detection algorithm is a widely used technique for identifying edges in images. Its efficiency and accuracy make it a crucial tool in applications such as lane detection, object recognition, image segmentation and more.

The Canny edge detection algorithm is structured as a multi-stage process, each step essential for producing clean and continuous edges. Its steps are:

1. Noise Reduction Using a Gaussian Filter

To prevent false detection caused by image noise, the algorithm first applies a Gaussian blur. This smoothes out minor intensity variations making true edges more prominent.

  • The Gaussian kernel used is typically 5×55×5 with a standard deviation (\sigma) of 1.4
  • The kernel must be normalized so its values sum to 1.
  • The equation for Gaussian filter kernel is:

G_\sigma = \frac{1}{2\pi\sigma^2} e^{-\frac{x^2 + y^2}{2\sigma^2}}

2. Gradient Calculation

The algorithm computes the intensity gradient for each pixel, both horizontally and vertically, using Sobel operators:

  • Sobel-X: Detects vertical edges.
  • Sobel-Y: Detects horizontal edges.

3. Non-Maximum Suppression 

To refine the edge map and thin the edges to one-pixel width, non-maximum suppression identifies and keeps only those pixels at the local maxima (in the gradient direction), suppressing all others.

  • Each pixel is compared along the gradient direction with its neighbours.
  • If a neighbouring pixel has a greater gradient magnitude, the current pixel is suppressed (set to zero).

4. Double Thresholding 

This step distinguishes strong edges from weak ones based on two threshold values:

  • Pixels above the high threshold: Marked as strong edges.
  • Pixels below the low threshold: Suppressed (not considered edges).
  • Pixels between thresholds: Marked as weak i.e they may or may not be an edge.

5. Edge Tracking by Hysteresis

Weak pixels remain part of the final edge only if they are connected to strong pixels i.e., part of a continuous edge. Hysteresis ensures that only genuine edges form the output.

Step-by-Step Implementation of the Canny Edge Detection

Let's see the implementation of Canny Edge Detection using OpenCV in Python,

Step 1: Import libraries

Python
import numpy as np
import cv2
import matplotlib.pyplot as plt

Step 2: Define Canny_detector Function

  • Color images are converted to grayscale, as edge detection operates on intensity changes.
  • A Gaussian blur smooths the image to reduce the impact of noise which might otherwise produce false edges.
  • Computes horizontal (x-direction) and vertical (y-direction) intensity gradients using Sobel operators.
  • Converts these to magnitude and angle for each pixel.
  • Initializes "weak" and "strong" edge thresholds as fractions of the maximum magnitude found.
  • Classifies pixels as strong edges, weak edges or non-edges based on the calculated thresholds.
Python
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\

gx = cv2.Sobel(np.float32(img), cv2.CV_64F, 1, 0, 3)
gy = cv2.Sobel(np.float32(img), cv2.CV_64F, 0, 1, 3)
mag, ang = cv2.cartToPolar(gx, gy, angleInDegrees=True)

mag_max = np.max(mag)
if weak_th is None:
    weak_th = mag_max * 0.1
if strong_th is None: strong_th = mag_max * 0.5

   for i_x in range(width):
        for i_y in range(height):
            grad_ang = ang[i_y, i_x]
            grad_ang = abs(
                grad_ang - 180) if abs(grad_ang) > 180 else abs(grad_ang)

            if grad_ang <= 22.5:
                neighb_1_x, neighb_1_y = i_x - 1, i_y
                neighb_2_x, neighb_2_y = i_x + 1, i_y
            elif grad_ang > 22.5 and grad_ang <= 67.5:
                neighb_1_x, neighb_1_y = i_x - 1, i_y - 1
                neighb_2_x, neighb_2_y = i_x + 1, i_y + 1
            elif grad_ang > 67.5 and grad_ang <= 112.5:
                neighb_1_x, neighb_1_y = i_x, i_y - 1
                neighb_2_x, neighb_2_y = i_x, i_y + 1
            elif grad_ang > 112.5 and grad_ang <= 157.5:
                neighb_1_x, neighb_1_y = i_x - 1, i_y + 1
                neighb_2_x, neighb_2_y = i_x + 1, i_y - 1
            else:
                neighb_1_x, neighb_1_y = i_x - 1, i_y
                neighb_2_x, neighb_2_y = i_x + 1, i_y

            if 0 <= neighb_1_x < width and 0 <= neighb_1_y < height:
                if mag[i_y, i_x] < mag[neighb_1_y, neighb_1_x]:
                    mag[i_y, i_x] = 0
                    continue

            if 0 <= neighb_2_x < width and 0 <= neighb_2_y < height:
                if mag[i_y, i_x] < mag[neighb_2_y, neighb_2_x]:
                    mag[i_y, i_x] =

    ids = np.zeros_like(img)
    for i_x in range(width):
        for i_y in range(height):
            grad_mag = mag[i_y, i_x]
            if grad_mag < weak_th:
                mag[i_y, i_x] = 0
            elif strong_th > grad_mag >= weak_th:
                ids[i_y, i_x] = 1
            else:
                ids[i_y, i_x] = 2
    return mag

Step 3: Load and Check the Input Image

  • Reads the target image file.
  • Checks if the file loads successfully to prevent errors later.
Python
frame = cv2.imread('sample.jpeg')
if frame is None:
    print("Error: image not found! Please check the path.")

Step 4: Apply Canny Edge Detection

Calls the Canny_detector function to process the image and obtain the edge map.

Python
canny_img = Canny_detector(frame)

Step 5: Visualize Results

Python
plt.subplot(1, 2, 1)
plt.title('Input Image')
plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.title('Canny Edges')
plt.imshow(canny_img, cmap='gray')
plt.axis('off')
plt.show()

Output:

canny
Output

The Canny edge detector is a useful and reliable method for edge detection in images. By implementing its steps from scratch, we gain a deeper understanding of the algorithm and its effectiveness. While OpenCV’s built-in function is efficient for practical use, building our own version offers valuable insights into computer vision fundamentals.


Similar Reads