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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.