Holistically-Nested Edge Detection with OpenCV and Deep Learning


Holistically-Nested Edge Detection (HED) is a deep learning-based method for detecting edges in images which can be performed using deep learning and a Python library, OpenCV. The holistically-Nested Edge detection was first introduced by Xie and Tu in 2015 and has since been widely used in computer-vision applications.Currently, it has gained a lot of popularity in recent years due to its ability to produce accurate and high-quality edge maps in an image.

In this article, we will discuss the basics of HED, how it works, and how to implement it using OpenCV and deep learning, and also using Canny

What is HED(Holistically-Nested Edge Detection)?

In computer vision, Edge detection is an important task that involves identifying and localizing sharp discontinuities in an image. These discontinuities in an image are often used as the basis of the more complex computer vision process such as object segmentation, and object detection.

Traditional edge detection methods use image features that are hand-crafted and heuristics to identify the edges of the image. These methods are often limited by their inability to handle the edges of complex structures or any variations in image texture or lighting.

Holistically-Nested Edge Detection is a deep-learning-based approach to detecting the edges in the image that overcomes the limitation of the traditional edge detection methods by learning edge features directly from the image data. It uses a deep convolutional neural network(CNN) to create a set of hierarchical edge maps, where each map represents the edges at a different scale.

Steps to perform Holistically-Nested Edge Detection with OpenCV and Deep Learning

Follow the steps given below to perform Holistically-Nested Edge Detection with OpenCV and Deep Learning −

  • Import the necessary libraries. We import cv2 for image processing and numpy for numerical operations.

  • Load the pre-trained edge detection model using cv2.dnn.readNetFromCaffe(). We need to provide the path to two files: deploy.prototxt and hed_pretrained_bsds.caffemodel. These files contain the architecture and weights of the model, respectively.

  • Load the input image using cv2.imread() and resize it to a smaller size of 512 x 512 pixels. Resizing the image to a smaller size is optional, but it can help speed up the edge detection process.

  • Then, we load the input image and resize it to a maximum size of 1000 pixels to speed up processing. We convert the resized image to grayscale using cv2.cvtColor().

  • We also provide some additional parameters to the function, such as the scale factor, image size, mean values, etc. These values are specific to the pre-trained model we are using.

  • Apply Canny edge detection to the grayscale image using cv2.Canny(). This helps to detect strong edges in the image.

  • We perform Holistically-Nested Edge Detection using the pre-trained model. We first set the input to the model using model.setInput(). We pass a blob of the grayscale image as input.

  • We threshold the output image using cv2.threshold() to obtain binary edges. We use the Otsu thresholding method, which automatically calculates the optimal threshold value.

  • Finally, we obtain the output of the model using model.forward().

  • display the input image, the Canny edges, and the HED edges using cv2.imshow(), and wait for the user to press a key before closing the windows using cv2.waitKey() and cv2.destroyAllWindows().

Example

import cv2
import numpy as np

# Load the pre-trained HED model
hed_model = cv2.dnn.readNetFromCaffe("deploy.prototxt", "hed_pretrained_bsds.caffemodel")

# Load the input image
image = cv2.imread("sample2.jpg")

# Resize the image for faster processing
height, width = image.shape[:2]
max_size = max(height, width)
scale = 1.0
if max_size > 1000:
   scale = 1000.0 / max_size
resized = cv2.resize(image, None, fx=scale, fy=scale)

# Convert the image to grayscale
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
# Apply Canny edge detection to the grayscale image
canny = cv2.Canny(gray, 100, 200)
# Apply HED to the color image
hed_model.setInput(cv2.dnn.blobFromImage(resized, scalefactor=1.0, size=(width, height), mean=(104.00698793, 116.66876762, 122.67891434), swapRB=False, crop=False))
hed = hed_model.forward()
hed = np.squeeze(hed)

# Convert the HED output to a binary image
hed = np.squeeze(hed)
hed = np.uint8(hed * 255)
hed_edges = cv2.convertScaleAbs(hed)
ret, hed_edges = cv2.threshold(hed_edges, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Threshold the output image to obtain binary edges
ret, hed_edges = cv2.threshold(hed, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Display the input and output images
cv2.imshow("Input", resized)
cv2.imshow("Canny", canny)
cv2.imshow("HED", hed_edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Conclusion

In conclusion, Holistically-Nested Edge Detection (HED) is a powerful technique for detecting edges in an image and offers quality results and high accuracy. It is based on deep learning algorithms that can learn and adapt to different image variations and structures, which makes it suitable for a wide range of applications in image processing and computer vision. We learned that OpenCV provides a convenient framework for Holistically-Nested Edge Detection and is one of the efficient ways to perform edge detection.

Updated on: 24-Jul-2023

410 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements