DAY 95-100 DAYS MLCODE: Histogram of Oriented Gradients ( HOG )

My Tech World

DAY 95-100 DAYS MLCODE: Histogram of Oriented Gradients ( HOG )

February 15, 2019 100-Days-Of-ML-Code blog 0

In the previous blogs, we had discussed featured engineering, in this blog, we’ll discuss Histogram of Oriented Gradients ( HOG ).

Till now we have developed several machine learning algorithms and learned a lot about how the machine learning stuff works. But in real life scenario, our data does not look like MNIST data where all the images are a perfectly 28*28 pixel. Most of the time generally spend by Data scientist to cleanse the data and to perform the feature engineering. Since computer vision is one of the main focus areas of machine learning. there was a need to have a special feature engineering method, we are going to study one such technique today.

Histogram of Oriented Gradients ( HOG ):

As per Wikipedia, HOG is:


The histogram of oriented gradients (HOG) is a feature descriptor used in computer vision and image processing for the purpose of object detection.

Wikipedia

Histogram of Oriented Gradients (HOG) transforms image pixels into a vector representation. This is sensitive to predominant informative image features regardless of confounding factors like brightness or angels of the image.

Instead of focusing on theory, let’s develop a face detection pipeline, using machine learning algorithms.

HOG Features

HOG feature extraction procedure was developed in the context of identifying pedestrians within images. HOG generally involves the following steps:

  • Avoid Illumination Variances: HOG pre-normalize the images. This lresults to features that resist dependence on variations in illumination.
  • Convolve the image with two filters that are sensitive to horizontal and vertical brightness gradients. These capture edge, contour, and texture information.
  • Subdivide the image into cells of a predetermined size, and compute a histogram of the gradient orientations within each cell.
  • Normalize the histograms in each cell by comparing to the block of neighboring cells. This further suppresses the effect of illumination across the image.
  • Construct a one-dimensional feature vector from the information in each cell.

Scikit-Image provides a fast method “hog” in the class “features” to perform the HOG. Let’s try it out visualize the oriented gradients within each cell of an image.

image_astronaut = color.rgb2gray(data.astronaut())
hog_vec, hog_astronaut = feature.hog(image_astronaut, visualise=True)

fig, ax = plt.subplots(1, 2, figsize=(12, 6),
                       subplot_kw=dict(xticks=[], yticks=[]))
ax[0].imshow(image_astronaut, cmap='gray')
ax[0].set_title('input image')

ax[1].imshow(hog_astronaut)
ax[1].set_title('visualization of HOG features');

Output:

HOG Features
HOG Features

In conclusion, HOG is a feature engineering that breaks patches up in blocks and then constructs histograms representing a gradient in the block. In the next blog, we’ll use this to create a classifier. You can find the code here.