HOG (Histogram of Oriented Gradients): An Amazing Feature Extraction Engine for Medical Images

Girish Ajmera
4 min readJan 3, 2024

--

Source : https://ieeexplore.ieee.org/document/8285523

Histogram of Oriented Gradients (HOG) is a powerful feature extraction technique that is extremely useful for medical image analysis. In this blog, I will deep dive into how HOG can be used as a Feature extractor for Images and what makes HOG an effective tool for extracting features from medical images.

What is HOG?

HOG is a feature descriptor that counts occurrences of gradient orientation in localized portions of an image. It divides the image into small regions called cells, computes a histogram of gradient directions within each cell, and then normalizes local contrast in overlapping blocks.

Mathematical Explanation

The main steps for computing HOG features from an image are:

Compute the gradient magnitude and angle: We calculate the gradient (think of it as the slope) of the pixel intensities in each direction (horizontal and vertical). It is similar to doing convolution with a kernel of [-1,0,+1] in X and Y direction. This will eventually give Gx and Gy value for every pixel. From Gx and Gy, Gradient magnitude and angle is calculated as follows. The magnitude of this gradient tells us how strong the edge is, while the angle tells us its direction.

Calculate Histogram of Gradient for cell : Divide the image into small connected regions called cells (Ex : 8X8 Pixels). Compute a histogram of gradient over each cell. The histogram bins represent the discretization of orientations into 9 bins each of 20 degree. Compute the sum of all the gradient magnitudes of all the pixels with gradient angle in the respective bin. Each cell’s histogram will have 9 values of gradient magnitude.

Normalize the histogram for Blocks : To counteract inconsistencies caused by varying lighting and noise, the histograms are normalized over larger, overlapping area called blocks. Each Block can have multiple cells. (Ex : 2X2 Cells i.e. 16X16 Pixels). Concatenate all cell’s HOGs in single array and then normalize it with L2 Norm. This calculation is done with overlapping block with stride of 1 cell.

Collect all HOG Features : Collect HOG features from all blocks in the image and concatenate into a combined feature vector representing the HOG feature for the entire image. For Image of MXM Size (Assuming M is multiple of 16), Histogram with Gradient angle bins of 20 degree, Cell Size of 8X8, Block size of 2X2 Cells and stride of 1 Cell, total number of Features with HOG Feature extraction will be

Extracting HOG features from a Image in Python

Built-in HOG implementation in scikit-image provides a simple way to extract HOG features. The skimage library has optimized functions for this purpose rather than computing from scratch.

from skimage.feature import hog 

image = "YourImage_asNumpyArray"

hog_features = hog(image,
orientations=9,
pixels_per_cell=(8, 8),
cells_per_block=(2, 2),)

Why HOG is Effective for Medical Images

There are several key properties that make HOG well-suited for feature extraction in medical imaging:

  • Insensitive to geometric and photometric transformations — Medical images can vary significantly in terms of scale, rotation, illumination etc. HOG is invariant to these transformations, allowing robust feature extraction.
  • Captures edge/shape information — The histogram of gradients captures important shape and edge information in an image which can be useful for identifying anatomical structures.
  • Unsupervised feature extraction — HOG does not require any labeled data to extract features. This is important for medical imaging where labeled data is often scarce.
  • Noise tolerant — Due to the coarse regional sampling and normalization, HOG can handle noise in images quite well. Medical images frequently contain noise.
  • Simple and efficient to compute — The HOG algorithm is relatively simple to implement and efficient to compute making it very practical.

Conclusion

The ability of HOG to robustly capture edge, shape and texture information makes it applicable to many medical imaging tasks involving detection, segmentation and classification. Its invariance to geometric transformations and lighting variations helps in analyzing the wide variability encountered in real-world medical images.

In summary, HOG can be a versatile and powerful feature descriptor for medical images.

--

--