Want to code faster? Our Python Code Generator lets you create Python scripts with just a few clicks. Try it now!
Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.
The Histogram of Oriented Gradients (HOG) is a feature descriptor used in computer vision and image processing applications for the purpose of the object detection. It is a technique that counts events of gradient orientation in a specific portion of an image or region of interest.
In 2005, Dalal and Triggs published a research paper named Histograms of Oriented Gradients for Human Detection. After the release of this paper, HOG is used in a lot of object detection applications.
Here are the most important aspects of HOG:
(64, 128)
shape.Below are the essential steps we take on HOG feature extraction:
As mentioned previously, if you have a wide image, then crop the image to the specific part in which you want to apply HOG feature extraction, and then resize it to the appropriate shape.
Now after resizing, we need to calculate the gradient in the x and y direction. The gradient is simply the small changes in the x and y directions, we need to convolve two simple filters on the image.
The filter for calculating gradient in the x-direction is:
The following is when we apply this filter to an image:
The filter for calculating gradient in the y-direction is:
The following is when we apply this filter to an image:
To calculate the magnitude of the gradient, the following formula is used:
The gradient direction is given by:
Let's take an example, say we have the matrix below:
The gradient in the x-axis will simply be 94-56 = 38, and 93-55 = 38 in the y-axis.
The magnitude will be:
And the gradient direction will be:
Related: Mastering YOLO: Build an Automatic Number Plate Recognition System with OpenCV in Python.
Now that we understand the theory, let's take a look on how we can use scikit-image library to extract HOG features from images.
First, let's install the necessary libraries for this tutorial:
pip3 install scikit-image matplotlib
I'm gonna perform HOG on a cute cat image, get it here and put it in the current working directory (you can use any image you want, of course). Let's load the image and show it:
#importing required libraries
from skimage.io import imread
from skimage.transform import resize
from skimage.feature import hog
from skimage import exposure
import matplotlib.pyplot as plt
# reading the image
img = imread('cat.jpg')
plt.axis("off")
plt.imshow(img)
print(img.shape)
Output:
(1349, 1012, 3)
Resizing the image:
# resizing image
resized_img = resize(img, (128*4, 64*4))
plt.axis("off")
plt.imshow(resized_img)
print(resized_img.shape)
Output:
(128, 64, 3)
Now we simply use
hog()
function from scikit-image library:
#creating hog features
fd, hog_image = hog(resized_img, orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(2, 2), visualize=True, multichannel=True)
plt.axis("off")
plt.imshow(hog_image, cmap="gray")
Output:
The hog()
function takes 6 parameters as input:
image
: The target image you want to apply HOG feature extraction.orientations
: Number of bins in the histogram we want to create, the original research paper used 9 bins so we will pass 9 as orientations.pixels_per_cell
: Determines the size of the cell, as we mentioned earlier, it is 8x8.cells_per_block
: Number of cells per block, will be 2x2 as mentioned previously.visualize
: A boolean whether to return the image of the HOG, we set it to True
so we can show the image.multichannel
: We set it to True
to tell the function that the last dimension is considered as a color channel, instead of spatial.Finally, if you want to save the images:
# save the images
plt.imsave("resized_img.jpg", resized_img)
plt.imsave("hog_image.jpg", hog_image, cmap="gray")
Alright, now you know how to perform HOG feature extraction in Python with the help of scikit-image library.
Check the full code here.
Related tutorials:
Finally, I've collected some useful resources and courses for you for further learning, I highly recommend the following courses:
Happy Learning ♥
Let our Code Converter simplify your multi-language projects. It's like having a coding translator at your fingertips. Don't miss out!
View Full Code Convert My Code
Got a coding query or need some guidance before you comment? Check out our Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!