Instagram
youtube
Facebook
Twitter

Edge Detection

Edge detection is a process of identifying and locating sharp changes in intensity in an image, which can correspond to object boundaries. OpenCV provides various ways to detect edges easily. One of the most popular edge detection algorithm in OpenCV is Canny Edge Detection. In this tutorial, we will learn about how to detect edges in images using OpenCV with some practical work.

What is Canny Edge Detection algorithm?

Canny edge detection is a popular edge detection algorithm in computer vision, which is used to identify edges in an image. This algorithm is widely used in computer vision applications such as image segmentation, object detection, and many more operations. We can use cv2.canny() function to use this algorithm. This function will take parameters like image source, minimum intensity gradient value, maximum intensity gradient value, aperture size(optional), L2 gradient(default).

Example 1:

Input

Code:

import cv2
img = cv2.imread('demo.png')  
image = cv2.Canny(img,100,200)
cv2.imshow('Result', image)
cv2.waitKey(0)  
cv2.destroyAllWindows()

Here, first, we imported cv2 and used cv2.imread to read the image. Then, we used cv2.canny with 100 minimum and 200 as the maximum gradient value. At last, we used cv2.imshow to display the final result.

Result:

 

Example 2:

Input

Code:

import cv2
img = cv2.imread('srk.jpg')  
image = cv2.Canny(img,150,200)
cv2.imshow('Result', image)
cv2.waitKey(0)  
cv2.destroyAllWindows()

Here, first, we imported cv2 and used cv2.imread to read the image. Then, we used cv2.canny with 150 minimum and 200 as the maximum gradient value. At last, we used cv2.imshow to display the final result.

Result: