Holistically-Nested Edge Detection with OpenCV and Deep Learning
Last Updated :
21 Mar, 2024
Holistically-nested edge detection (HED) is a deep learning model that uses fully convolutional neural networks and deeply-supervised nets to do image-to-image prediction. HED develops rich hierarchical representations automatically (directed by deep supervision on side replies) that are critical for resolving ambiguity in edge and object boundary detection.
Why Holistically-Nested Edge Detection(HED)
The proposed holistically nested edge detector (HED) tackles two critical issues:
- Holistic image training and prediction, inspired by fully convolutional neural networks for image-to-image classification (the system takes an image as input, and directly produces the edge map image as output)
- Nested multi-scale feature learning, inspired by deeply-supervised nets that performs deep layer supervision to “guide” early classification results.
- HED is a powerful technique for edge detection that leverages the capabilities of FCNs and deep supervision to produce accurate and detailed edge predictions in images.
Model Architecture of Holistically-Nested Edge Detection
The model is VGGNet with a few modifications-
- The side output layer is connected to the last convolutional layer in each stage, respectively conv1_2, conv2_2, conv3_3, conv4_3,conv5_3. The receptive field size of each of these convolutional layers is identical to the corresponding side-output layer.
- The last stage of VGGNet is removed including the 5th pooling layer and all the fully connected layers. By removing these layers, HED focuses on leveraging the convolutional layers for feature extraction and hierarchical representation learning, which is essential for edge detection.
The final HED network architecture has 5 stages, with strides 1, 2, 4, 8, and 16, respectively, and with different receptive field sizes, all nested in the VGGNet. HED modifies VGGNet by connecting side output layers to specific convolutional layers and removing the last stage, resulting in a model architecture that is tailored for holistic and hierarchical edge detection.
Steps Required for Implementing HED
- Import all the libraries required.
We will use OpenCV for reading the input image, resizing it, and loading the parameters of the trained network.
Import cv2
img = cv2.imread("path")
blob = cv2.dnn.blobFromImage(img, scalefactor=1.0, size=(W, H),swapRB=False, crop=False)
- Load the pre-trained Caffe model
- This framework is built on top of publicly available implementations of FCN and DSN and is implemented using the publicly available Caffe Library. From an initialization using the pre-trained VGG-16 Net model, the entire network in our HED system is fine-tuned.
- This Caffe model is encoded into two files
- A prototxt file: A text Caffe JSON file that includes the model definition (deploy) (i.e. layers, expected input, …..)
- The pre-trained Caffe model: Neural Network weights.
These files can be downloaded from this link. We need these files to train our model and apply predictions on our input image.
net = cv2.dnn.readNetFromCaffe("path to prototxt file", "path to model weights file")
- Pass the blob of the image to the model and find the output.
net.setInput(blob)
hed = net.forward()
- Format the data in the correct format to display (if required)
hed = cv2.resize(hed[0, 0], (W, H))
hed = (255 * hed).astype("uint8")
cv2.imshow("HED", hed)
Python Implementation of HED
We will use a pre-trained HED model to detect the edge of our input image
Input Image:
Input image for HED
Python3
import cv2
img = cv2.imread("input.webp")
(H, W) = img.shape[:2]
blob = cv2.dnn.blobFromImage(img, scalefactor=1.0, size=(W, H),
swapRB=False, crop=False)
net = cv2.dnn.readNetFromCaffe("deploy.prototxt", "hed_pretrained_bsds.caffemodel")
net.setInput(blob)
hed = net.forward()
hed = cv2.resize(hed[0, 0], (W, H))
hed = (255 * hed).astype("uint8")
cv2.imshow("Input", img)
cv2.imshow("HED", hed)
cv2.waitKey(0)
Output:
Output
Using Different Input image
Input image:
Input image for HED
Python3
import cv2
img = cv2.imread("pexels-ylanite-koppens-2343170(1).jpg")
(H, W) = img.shape[:2]
blob = cv2.dnn.blobFromImage(img, scalefactor=1.0, size=(W, H),
swapRB=False, crop=False)
net = cv2.dnn.readNetFromCaffe("deploy.prototxt", "hed_pretrained_bsds.caffemodel")
net.setInput(blob)
hed = net.forward()
hed = cv2.resize(hed[0, 0], (W, H))
hed = (255 * hed).astype("uint8")
cv2.imshow("Input", img)
cv2.imshow("HED", hed)
cv2.waitKey(0)
Output
Output
Similar Reads
Age Detection using Deep Learning in OpenCV
The task of age prediction might sound simple at first but it's quite challenging in real-world applications. While predicting age is typically seen as a regression problem this approach faces many uncertainties like camera quality, brightness, climate condition, background, etc. In this article we'
5 min read
Feature detection and matching with OpenCV-Python
In this article, we are going to see about feature detection in computer vision with OpenCV in Python. Feature detection is the process of checking the important features of the image in this case features of the image can be edges, corners, ridges, and blobs in the images. In OpenCV, there are a nu
5 min read
Black and white image colorization with OpenCV and Deep Learning
In this article, we'll create a program to convert a black & white image i.e grayscale image to a colour image. We're going to use the Caffe colourization model for this program. And you should be familiar with basic OpenCV functions and uses like reading an image or how to load a pre-trained mo
3 min read
How does Deep Learning helps in Detecting Multiple Objects in Single Image?
Answer: Deep Learning employs object detection algorithms, such as YOLO or SSD, to identify and localize multiple objects within a single image.Deep Learning contributes significantly to detecting multiple objects in a single image through the implementation of sophisticated object detection algorit
2 min read
OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
Content has been removed on Author's request.
1 min read
Object Detection with YOLO and OpenCV
Object Detection is a task of computer vision that helps to detect the objects in the image or video frame. It helps to recognize objects count the occurrences of them to keep records, etc. The objective of object detection is to identify and annotate each of the objects present in the media. YOLO(Y
6 min read
Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
Edge detection is one of the fundamental image-processing tasks used in various Computer Vision tasks to identify the boundary or sharp changes in the pixel intensity. It plays a crucial role in object detection, image segmentation and feature extraction from the image. In Real-time edge detection,
5 min read
Python | Corner detection with Harris Corner Detection method using OpenCV
Harris Corner detection algorithm was developed to identify the internal corners of an image. The corners of an image are basically identified as the regions in which there are variations in large intensity of the gradient in all possible dimensions and directions. Corners extracted can be a part of
2 min read
Implement Canny Edge Detector in Python using OpenCV
In this article, we will learn the working of the popular Canny edge detection algorithm developed by John F. Canny in 1986. Usually, in Matlab and OpenCV we use the canny edge detection for many popular tasks in edge detection such as lane detection, sketching, border removal, now we will learn the
5 min read
Face and Hand Landmarks Detection using Python - Mediapipe, OpenCV
In this article, we will use mediapipe python library to detect face and hand landmarks. We will be using a Holistic model from mediapipe solutions to detect all the face and hand landmarks. We will be also seeing how we can access different landmarks of the face and hands which can be used for diff
4 min read