Histogram of Oriented Gradients (HOG) in Laptop Imaginative and prescient | by Mohamad Hamza | Nov, 2024

What’s a Histogram of Oriented Gradients (HOG)?

The HOG is a world descriptor (function extraction) methodology utilized to every pixel inside a picture to extract neighborhood info(neighborhood of pixel) like texture and compress/summary that info from a given picture right into a diminished/condensed vector type known as a function vector that would describe the function of this picture which could be very helpful when it got here to captures edge and gradient buildings in a picture. Furthermore, we are able to examine this processed picture for object recognition or object detection.

1- Calculate the Gradient Picture

To extract and seize edge info, we apply a Sobel operator consisting of two small matrices (filter/kernel) that measure the distinction in depth at grayscale(wherever there’s a sharp change in depth).

created by creator

We apply this kernel on pixels of the picture by convolution :

We slide the 3×3 Kerne/filter (Sobel Operator) over the picture, multiply element-wise, and add the outputs. (Created by the creator)

After making use of Sobel Kernel to the picture now, we are able to calculate the magnitude and orientation of a picture :

Gradient magnitude.(Created by creator)
Gradient orientation.(Created by creator)

The gradients decide the perimeters’ energy (magnitude) and course (orientation) at a particular level. The sting course is perpendicular to the gradient vector’s course on the location the place the gradient is calculated. In different phrases, the size and course of the vector.

2- Calculate the Histogram of the Gradient

For every pixel, we’ve two values: magnitude and orientation. To mix this info into one thing significant, we use a histogram, which helps arrange and interpret these values successfully.

https://learnopencv.com/histogram-of-oriented-gradients/ , edited by the creator .

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 every of 20 levels ( spanning 0° to 180°). Every pixel’s magnitude worth (edge energy) 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 depend upon lighting circumstances, normalization scales the histograms to scale back the affect of lighting and distinction variations.

https://learnopencv.com/histogram-of-oriented-gradients/ , edited by the creator .

Every block usually consists of a grid of cells (2×2). This block slides throughout the picture with overlap, that means every cell is included in a number of blocks.
Every block has 4 histograms ( 4 cells ), which could be concatenated to type a 4(cells) x 9 (bins )x1 vector = 36 x 1 factor vector for every block; total picture within the instance: 7 rows x 15 cols = 7x15x36=3780 parts. This function vector is known as the HOG descriptor, and the ensuing vector is used as enter for classification algorithms like SVM.

# we shall be utilizing the hog descriptor from skimage because it has visualization instruments out there for this instance
import cv2
import matplotlib.pyplot as plt
from skimage import shade, function, publicity

# Load the picture
img = cv2.imread('The trail of the picture ..')

# Convert the unique picture to RGB
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Convert the unique picture to grey scale
img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_RGB2GRAY)

plt.imshow(img_gray,cmap="grey")

# These are the standard fundamental parameters to tune within the HOG algorithm.
(H,Himage) = function.hog(img_gray, orientations=9, pixels_per_cell=(8,8), cells_per_block=(2,2),visualize=True)

Himage = publicity.rescale_intensity(Himage, out_range=(0,255))
Himage = Himage.astype("uint8")

fig = plt.determine(figsize=(15, 12), dpi=80)
plt.imshow(Himage)

Mona Lisa image by (WikiImages ) https://pixabay.com/

As you possibly can see, HOG successfully captures the overall face form (eyes, nostril, head) and arms. This is because of HOG’s give attention to gradient info throughout the picture, making it extremely efficient for detecting strains and shapes. Moreover, we are able to observe the dominant gradients and their depth at every level within the picture.

(HOG) algorithm for human detection in a picture

HOG is a well-liked function descriptor in laptop imaginative and prescient, significantly efficient for detecting shapes and descriptions, such because the human type. This code leverages OpenCV’s built-in HOG descriptor and a pre-trained Assist Vector Machine (SVM) mannequin particularly skilled to detect individuals

# Load the picture
img = cv2.imread('The trail of the picture ..')

# Convert the unique picture to RGB
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Convert the unique picture to grey scale
img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_RGB2GRAY)

#Initialize the HOG descriptor and set the default SVM detector for individuals
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

# Detect individuals within the picture utilizing the HOG descriptor
bbox, weights = hog.detectMultiScale(img_gray ,winStride = (2,2), padding=(10,10),scale=1.02)

# Draw bounding bins round detected individuals
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 take a look at picture, we use HOG’s detectMultiScale methodology to detect individuals, with winStride set to skip one pixel per step, enhancing velocity by buying and selling off a little bit of accuracy, which is essential in object detection since it’s a computationally intensive course of. Though the detector might establish all individuals, generally there’s a false optimistic the place a part of one particular person’s physique is detected as one other particular person. To repair this, we are able to apply Non-Maxima Suppression (NMS) to remove overlapping bounding bins, although dangerous configuration(winterize, padding, scale) can sometimes fail to detect objects.

wrap –up The HOG descriptor computation entails a number of steps:
1. Gradient Computation
2. Orientation Binning
3. Descriptor Blocks
4. Normalization
5. Function Vector Formation
On this article, we explored the maths behind HOG and the way simple it’s to use in just some strains of code, because of OpenCV! I hope you discovered this information useful and loved working by the ideas. Thanks for studying, and see you within the subsequent publish!