What is the Canny edge detection technique in OpenCV

This recipe explains what is the Canny edge detection technique in OpenCV

Recipe Objective: What is the Canny edge detection technique in OpenCV?

This recipe will help you understand the Canny edge detection technique in OpenCV

Step 1: Import the libraries and read the image

Let us first import the necessary libraries and read the image. The image that we are using here is the one shown below. Let us read the image in grayscale this time as Canny works only on grayscale images.

Input Image

import numpy as np
import cv2
image = cv2.imread('edgeflower.jpg',0)

Step 2: Understanding the theory of the Canny edge detection algorithm

The Canny edge detection algorithm was first invented by John F. Canny and hence the name Canny algorithm. This algorithm involves a series of steps which we will discuss in this step.

  1. The first step is to reduce the noise and smoothen the image using a 5 x 5 Gaussian filter.
  2. The second step is to find the intensity gradient of the image using a Sobel kernel.
  3. The third step is called Non - maximum suppression. In this step, each pixel is scanned individually, and non-edge pixels are removed. Each point is checked if it is the local maximum of its neighborhood. If yes, then it is detected as an edge, else omitted.
  4. The last step is Hysteresis Thresholding. This is the previous step which decides if the pixel is an edge pixel or not. This takes a maxVal and a minVal. The pixels edges with an intensity gradient more than the maxVal are considered as a definite edge. Similarly, the ones below the minVal are regarded as a definite non-edge pixel. The pixels between the maxVal and minVal are categorized relative to the definite edges.

Step 3: Implementation of Canny edge detection

All the steps mentioned above are condensed into one function called cv2.Canny() takes the following arguments.

  • image: The input image in grayscale
  • threshold1: The value for minVal
  • threshold2: The value for maxVal

Here's how we can use it for our image

canny_edges = cv2.Canny(image,100,190)

Step 4: Display the output

Let us display the detected output using matplotlib

plt.figure(figsize=(13,5))
plt.subplot(1,2,1)
plt.imshow(image,cmap = 'gray')
plt.title('Original Image')
plt.xticks([])
plt.yticks([])

plt.subplot(1,2,2)
plt.imshow(canny_edges,cmap = 'gray')
plt.title('Edges detected using Canny algorithm')
plt.xticks([]), plt.yticks([])

plt.show()
plt.tight_layout()

Output:

Canny edges

Download Materials

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.