What’s a Histogram of Oriented Gradients (HOG)?
The HOG is a world descriptor (feature extraction) method applied to every pixel inside a picture to extract neighborhood information(neighborhood of pixel) like texture and compress/abstract that information from a given image right into a reduced/condensed vector form called a feature vector that might describe the feature of this image which could be very useful when it got here to captures edge and gradient structures in a picture. Furthermore, we are able to compare this processed image for object recognition or object detection.
1- Calculate the Gradient Image
To extract and capture edge information, we apply a Sobel operator consisting of two small matrices (filter/kernel) that measure the difference in intensity at grayscale(wherever there’s a pointy change in intensity).
We apply this kernel on pixels of the image by convolution :
After applying Sobel Kernel to the image now, we are able to calculate the magnitude and orientation of a picture :
The gradients determine the sides’ strength (magnitude) and direction (orientation) at a selected point. The sting direction is perpendicular to the gradient vector’s direction at the situation where the gradient is calculated. In other words, the length and direction of the vector.
2- Calculate the Histogram of the Gradient
For every pixel, we have now two values: magnitude and orientation. To mix this information into something meaningful, we use a histogram, which helps organize and interpret these values effectively.
We create a histogram of gradients in these cells ( 8 x 8 ), which have 64 values distributed to bins within the histogram, that are quantized into 9 bins each of 20 degrees ( spanning 0° to 180°). Each pixel’s magnitude value (edge strength) is added as a “vote” to the corresponding orientation bin so the histogram’s peaks reveal the pixel’s dominant orientations.
3- Normalization
Since gradient magnitudes rely upon lighting conditions, normalization scales the histograms to scale back the influence of lighting and contrast variations.
Each block typically consists of a grid of cells (2×2). This block slides across the image with overlap, meaning each cell is included in multiple blocks.
Each block has 4 histograms ( 4 cells ), which might be concatenated to form a 4(cells) x 9 (bins )x1 vector = 36 x 1 element vector for every block; overall image in the instance: 7 rows x 15 cols = 7x15x36=3780 elements. This feature vector is named the HOG descriptor, and the resulting vector is used as input for classification algorithms like SVM.
# we shall be using the hog descriptor from skimage because it has visualization tools available for this instance
import cv2
import matplotlib.pyplot as plt
from skimage import color, feature, exposure# Load the image
img = cv2.imread('The trail of the image ..')
# Convert the unique image to RGB
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Convert the unique image to gray scale
img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_RGB2GRAY)
plt.imshow(img_gray,cmap="gray")
# These are the standard primary parameters to tune within the HOG algorithm.
(H,Himage) = feature.hog(img_gray, orientations=9, pixels_per_cell=(8,8), cells_per_block=(2,2),visualize=True)
Himage = exposure.rescale_intensity(Himage, out_range=(0,255))
Himage = Himage.astype("uint8")
fig = plt.figure(figsize=(15, 12), dpi=80)
plt.imshow(Himage)
As you possibly can see, HOG effectively captures the overall face shape (eyes, nose, head) and hands. That is resulting from HOG’s give attention to gradient information across the image, making it highly effective for detecting lines and shapes. Moreover, we are able to observe the dominant gradients and their intensity at each point within the image.
(HOG) algorithm for human detection in a picture
HOG is a well-liked feature descriptor in computer vision, particularly effective for detecting shapes and descriptions, reminiscent of the human form. This code leverages OpenCV’s built-in HOG descriptor and a pre-trained Support Vector Machine (SVM) model specifically trained to detect people
# Load the image
img = cv2.imread('The trail of the image ..')# Convert the unique image to RGB
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Convert the unique image to gray scale
img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_RGB2GRAY)
#Initialize the HOG descriptor and set the default SVM detector for people
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
# Detect people within the image using the HOG descriptor
bbox, weights = hog.detectMultiScale(img_gray ,winStride = (2,2), padding=(10,10),scale=1.02)
# Draw bounding boxes around detected people
for (x, y, w, h) in bbox:
cv2.rectangle(img_rgb, (x, y),
(x + w, y + h),
(255, 0, 0), 4)
plt.imshow(img_rgb)
After loading the test image, we use HOG’s detectMultiScale method to detect people, with winStride set to skip one pixel per step, improving speed by trading off a little bit of accuracy, which is crucial in object detection because it is a computationally intensive process. Although the detector may discover all people, sometimes there’s a false positive where a part of one person’s body is detected as one other person. To repair this, we are able to apply Non-Maxima Suppression (NMS) to eliminate overlapping bounding boxes, though bad configuration(winterize, padding, scale) can occasionally fail to detect objects.
wrap –up The HOG descriptor computation involves several steps:
1. Gradient Computation
2. Orientation Binning
3. Descriptor Blocks
4. Normalization
5. Feature Vector Formation
In this text, we explored the maths behind HOG and the way easy it’s to use in only just a few lines of code, due to OpenCV! I hope you found this guide helpful and enjoyed working through the concepts. Thanks for reading, and see you in the following post!
- L. Daudet, B. Izrar, and R. Motreff, Histograms of Oriented Gradients for Human Detection(2010), HAL open science.
- Satya Mallick, Image Recognition and Object Detection: Part 1,2016, learn OpenCV.
- Satya Mallick, Histogram of Oriented Gradients explained using OpenCV , 2016, learn OpenCV