How to Apply HOG Feature Extraction in Python?

Posted in /  

How to Apply HOG Feature Extraction in Python?
vinaykhatri

Vinay Khatri
Last updated on October 31, 2024

    In this article, we will discuss how to apply HOG feature extraction in Python. Histogram of Oriented Gradients (HOG) is an image detection technique that is often used in computer vision to detect an object in machine learning models.

    How to Apply HOG Feature Extraction in Python?

    Step 1: Define the patch dimensions on which the HOG feature extraction will be performed. The patches define the fixed aspect ratio of 1:2, and in an image, we can define the patch with the cropped and resized image.

    Step 2: Calculate Gradients in X and Y directions, and compute the overall gradient magnitude using the following formula: Step 3: Calculate the Gradient Angle or orientation for the patch grids. To calculate the gradient angle, we use the following formula: Now we have both gradient magnitude and angle.

    This is all about HOG techniques. It just places the gradient magnitude and gradient angles for the specified patches. Every specified patch pixel will represent a star, which is a combination of gradient magnitude and angle.

    Next, open your best Python IDE or text editor and start implementing the HOG feature extraction in Python, but before that, let's install the required Python libraries.

    In this Python tutorial, we will be using the Python scikit-image library because it comes with the inbuilt hog module. We will also be using Matplotlib, the standard Python data visualization library , to display the image.

    Use the following command to install matplotlib:

    pip install scikit-image matplotlib

    For this Python tutorial, we will be performing the HOG feature extraction on the following tesla.jpg image.

    Python Code

    Let's start with importing the required modules:

    from skimage.io import imread
    from skimage.feature import hog
    import matplotlib.pyplot as plt

    Load the image with the help of the skimage imread() method.

    #load image
    tesla_image = imread("tesla.jpg")

    Perform the HOG feature extraction on the loaded image, i.e., tesla_image with the help of the hog(image, orientations, pizels_per_cell, cells_per_block, visualize, multichannel) method.

    #hog extraction 
    _, hog_image = hog(tesla_image, orientations=8, pixels_per_cell=(16, 16),
    cells_per_block=(1, 1), visualize=True, multichannel=True)

    The hog() method accepts the following six arguments:

    1. image - Represents the image on which we want to perform the hog extraction.
    2. orientations - Defines the number of bins in the histogram.
    3. pixels_per_cell - Defines the number of grids per patch.
    4. cells_per_block - It is the number of cells for every block.
    5. visualize - A Boolean value that specifies whether to return the image of HOG or not.
    6. multichannel It is a Boolean value. True sets the last dimension to the color channel rather than spatial.

    Lastly, show the hog image with the plt.imshow() method.

    #show image
    plt.imshow(hog_image)
    plt.show()

    Now, put all the code together and execute.

    #Python Program to Apply HOG Feature Extraction

    from skimage.io import imread
    from skimage.feature import hog
    import matplotlib.pyplot as plt
    
    #load image
    tesla_image = imread("tesla.jpg")
    
    #hog extraction 
    _, hog_image = hog(tesla_image, orientations=8, pixels_per_cell=(16, 16),
                        cells_per_block=(1, 1), visualize=True, multichannel=True)
    
    #show image
    plt.imshow(hog_image)
    plt.show()

    Output

    Conclusion

    In this Python tutorial, you learned how to apply HOG feature extraction in Python using scikit-image and matplotlib libraries. There are other feature extraction algorithms like SIFT, SURF, and GLOH too. It's very easy to find features of an image by using the scikit-image library's hog method.

    We will suggest you read the official research paper of HOG feature extraction to know more about the technique.

    People are also reading:

    Leave a Comment on this Post

    0 Comments