Edge Detection in OpenCV

In this blog, we are going to understand that, what is edge detection and why it is used in image processing

In this blog, we are going to understand that, what is edge detection and why it is used in image processing, and then we would talk about the most widely used technique of edge detection i.e., Canny Edge detection, and finally we will code Canny Edge detection using OpenCV's Canny() function.

What is edge detection ?

Edge detection is basically a mathematical operation which is done on image to get the edges of the objects present in the image by checking for the sharp change in the light(intensity of pixels), moving from one pixel to another.

Canny Edge Detection

Edge detection is a fundamental operation in image processing and the most the most widely used algorithm to perform the edge detection is Canny Edge Detection.
Canny Edge Detection is a multi-stage algorithm, which perform several operations on the image to make the edge detection much better, it performs the following consecutive operations:

  • Noise Reduction
  • Finding Intensity Gradient of the Image
  • Non-maximum Suppression
  • Hysteresis Thresholding

For more information about these individual step, you can this article

OpenCV provides a simple method Canny() to perform these above steps in a single go. Canny() method needs three arguments to be passed, first is the source image, second is the minVal which is the minmum thresholding value for edge-detection, and third is the maxVal which is the maximum thresholding value for edge-detection.

Let's see how to write the code for doing edge detection using Canny() method of OpenCV.

import cv2

# Reading image
img = cv2.imread("color-img.png")

# Edge-detection using Canny() method
canny = cv2.Canny(img, 100, 200)

# Showing the edge-detected image
cv2.imshow("Canny Image",canny)

# waiting for key event
cv2.waitKey(0)

# destroying all windows
cv2.destroyAllWindows()

Output

Akash Srivastava
Software Engineer @ Seawoods Ventures Inc.
© 2019-2024 dev-akash.in