Edge Detection with OpenCV in Python

Python OpenCV – Edge detection

To find the edges in an image with OpenCV in Python, you can apply Canny Edge Detection technique using cv2.Canny() function.

Python OpenCV - Edge detection

In this tutorial, you will learn how to use cv2.Canny() function to find the edges in a given image, with examples.

Syntax of Canny()

The syntax of cv2.Canny() function to find edges is

cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient]]]) 

where

ParameterDescription
imageInput/source image array.
threshold1First threshold for the hysteresis procedure.
threshold2Second threshold for the hysteresis procedure.
edges[Optional] Number of channels in the destination image.
apertureSize[Optional] Aperture size of the operator.
L2gradient[Optional] A flag indicating whether a more accurate L2 norm value should be used while calculating gradient.

In the examples that we cover, we do not use the optional parameters. We shall use the following syntax.

cv2.Canny(image, threshold1, threshold2) 

Please note that the image should be a grayscale image. Therefore, if the input is a color image, convert that to grayscale image, and then give that grayscale image as input to cv2.Canny().

Steps to find edges in an image

  1. Read an image into an array using cv2.imread() function.
  2. Convert the image to grayscale image using cv2.cvtColor() function.
  3. Define threshold values for finding the edges.
  4. Pass the grayscale image and the threshold values as arguments to cv2.Canny() function. The function returns the edges of image.
  5. Save the edges image using cv2.imwrite() function.

Examples

1. Find edges in image using Canny Edge Detection

In the following program, we find the edges of an input image using cv2.Canny() function.

Python Program

import cv2

# Load the image
img = cv2.imread('test_image_house.jpg')

# Convert input image to grayscale
grayscale_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Define thresholds
threshold1 = 30
threshold2 = 200

# Perform Canny edge detection
edges = cv2.Canny(grayscale_img, threshold1, threshold2)

# Save edges image
cv2.imwrite('edges.jpg', edges)

Input Image – test_image_house.jpg

Python OpenCV - Convert image to grayscale - input

edges.jpg

Python OpenCV - Find edges in image using Canny Edge Detection

2. Different threshold values

In this example, we shall modify the threshold values from the previous example, and observe the output.

Python Program

import cv2

# Load the image
img = cv2.imread('test_image_house.jpg')

# Convert input image to grayscale
grayscale_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Define thresholds
threshold1 = 50
threshold2 = 100

# Perform Canny edge detection
edges = cv2.Canny(grayscale_img, threshold1, threshold2)

# Save edges image
cv2.imwrite('edges1.jpg', edges)

Input Image – test_image_house.jpg

Python OpenCV - Convert image to grayscale - input

edges1.jpg

Python OpenCV - Find edges in image using Canny Edge Detection

Summary

In this Python OpenCV Tutorial, we did edge detection in an image using Canny Edge Detection function, with the help of examples.

Code copied to clipboard successfully 👍