Edge detection using OpenCV in Python

In this tutorial, we will learn about what is edge detection, the types of edge detection, Applications of edge detection, and how to perform edge detection using Python.

What is edge detection?

It is a technique used in image processing to detect the boundaries of the object. Boundaries could be outer boundaries or inner edges in the images. These boundaries are defined by the intensity of the color in the images. These are nothing but pixel which connects two different regions in the images. It helps to understand the underlying structure of the image.

Application of edge detection:

  • Object detection
  • Face recognition
  • Autonomous vehicles
  • Robotics
  • Pattern recognition

What are the types of edge detection operators?

What is the operator?

These are the rules or algorithms that detect sudden changes in the intensity of an image.

There are two categories for edge detection:

1. Gradient-based methods:

These methods calculate the gradient of the intensity of the image at each pixel. There are different types of gradient methods—Roberts operator, Sobel operator, etc.

2.  Zero-crossing method:

It is the same as the Gradient-based approach but in this identification of edges is done by finding pixels where the gradient magnitude of the image intensity crosses zero. This means the edge is the point where the intensity changes from increase to decrease and vice versa. There are two types. Canny edge detector, Laplacian or Gaussian

Which is the widely used method for edge detection?

There are different types of edge detection algorithms.

The widely used edge detection algorithm is the Canny edge detection algorithm. We will see how to use it for edge detection step by step.

How to perform edge detection using opencv

First, we will be required to install the OpenCV library. Which is a widely used image-processing library.

We can install it using the following command in pip

pip install opencv-python

This will install an open cv library.

Now we will import other important libraries

import numpy as np
import matplotlib.pyplot as plt

numpy we use for image processing and matplotlib is for displaying images.

Now we will perform image detection as follows:

To import and visualize images:

img_path = r'C:\Users\DELL\Desktop\PRTFLIO\Internship\DJ.jpeg'
img_read= cv2.imread(img_path)
img_show=cv2.cvtColor(img_read, cv2.COLOR_BGR2RGB)
plt.imshow(img_show)
plt.axis('off')  
plt.show()

In the above code first, we are reading the image using the image path and cv2.imread command. Then we are converting the image in RGB format as matplotlib works with RGB format. Then using plt. show command we are visualizing the image.

Output:

To import and visualize images

Convert image to grayscale:

To reduce complexity, and improve speed we convert images into a grayscale. It also helps to detect edges based on changes in intensity in grayscale. So it is better practice to change images grayscale before applying edge detection.

img_path = r'C:\Users\DELL\Desktop\PRTFLIO\Internship\DJ.jpeg'
img_read= cv2.imread(img_path)
img_gray = cv2.cvtColor(img_read, cv2.COLOR_BGR2GRAY)
plt.imshow(img_gray,'gray')
plt.axis('off')  
plt.show()

The above code will convert the image to grayscale.  cv2.cvtColor command is used to convert the image to grayscale.

Output:

Convert image to grayscale Python

In the above output, you can see the edges of the image. The original image is on the left-hand side and an image showing the edge is on the right-hand side.

Edge detection for an image:

edges = cv2.Canny(img_gray,100,200)

plt.subplot(121)
plt.imshow(img_gray,'gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(122)
plt.imshow(edges, 'gray')
plt.title('Edge Image')
plt.axis('off')
plt.show()

 

In the above code cv2.The canny command is used to detect edges in the image using a canny algorithm.

Where 100 is the lower threshold value and 200 is the upper threshold value. These values indicate the range of gradient values for each pixel. Mean all the pixels whose gradients fall within these values will be considered for edge detection

Output:

Edge detection using OpenCV in Python

We learned about how to detect the edge images. For more information, we can visit OpenCV documentation.

https://docs.opencv.org/4.x/index.html

Leave a Reply

Your email address will not be published. Required fields are marked *