Skip to content

Histogram of Oriented Gradients

Fact Value
Also called HOG
Status Implemented and tested
Family Texture, corners, blobs and features
Engine Science engine
Type Nonlinear, spatial, gradient orientation histograms
Output Image
Operator shown as algorithm (exact)
Support radius global
Code engine/src/pixelnexus_engine/filters/texture.py, hog_filter
Operator code engine/src/pixelnexus_engine/filters/texture.py, hog_representation
Packages scikit-image

Overview

The histogram of oriented gradients, HOG, describes the shape in each small cell of an image by the directions of its edges. Dalal and Triggs built it for detecting people. A classifier reads one long vector of all cell histograms. This filter shows the cell histograms the way scikit-image draws them. Each cell becomes a star of lines through its center, one per direction, as bright as the edges in that direction. It runs in the PixelNexus engine, with scikit-image.

Four panels. A Siemens star with 12 black and white spokes. With cells of 8 pixels, the HOG view shows short lines along the spokes, and lines along the round edge of the star. With cells of 16 pixels, the lines are fewer and longer. On the texture test image, the stripes give a grid of vertical lines, the fine noise gives small stars with lines in all directions, the coarse noise gives faint short lines, and the flat gray stays almost black. Four panels. A Siemens star with 12 black and white spokes. With cells of 8 pixels, the HOG view shows short lines along the spokes, and lines along the round edge of the star. With cells of 16 pixels, the lines are fewer and longer. On the texture test image, the stripes give a grid of vertical lines, the fine noise gives small stars with lines in all directions, the coarse noise gives faint short lines, and the flat gray stays almost black.
A Siemens star with 12 spokes and its HOG view with cells of 8 and 16 pixels. Then the view of the texture test image with stripes, fine noise, coarse noise and a flat gray, with cells of 8 pixels.

How it works

  1. Take the 8-bit luma and its centered differences.
  2. Build a weighted histogram of gradient directions in each cell.
  3. Normalize blocks of cells for the descriptor.
  4. Draw each cell histogram as a star of lines, and stretch the view.

Mathematical foundation

\[ G_x = I(x + 1, y) - I(x - 1, y), \qquad \theta = \arctan \frac{G_y}{G_x} \bmod 180^\circ, \qquad h_c(b) = \sum_{x \in c,\ \theta(x) \in b} \lvert G(x) \rvert \]

The formula shows the default parameter values.

where

  • \(G_x, G_y\): The centered differences
  • \(\theta\): The unsigned gradient direction
  • \(h_c(b)\): The weight of bin b in cell c

The gradient comes from centered differences, with its magnitude and an unsigned direction from 0 to 180 degrees. Each cell gets a histogram of the directions of its pixels, weighted by the magnitude. Dalal and Triggs then normalize blocks of cells together, so the descriptor does not depend on the lighting, and chain all blocks into one long vector for a classifier. The view of scikit-image draws each cell histogram as lines through the cell center, one per bin, as bright as its weight. Each line lies across its gradient direction, so it runs along the edges in the cell. The view shows the cell histograms before the block normalization, so the block settings only change the descriptor.

The direction is unsigned. A step up and a step down at the same angle fall into the same bin. So the descriptor does not care which side of an edge is brighter. Each pixel adds its gradient magnitude to the bin of its direction, so strong edges count more than faint ones.

Lighting changes the magnitude of the gradients. Dalal and Triggs therefore normalize groups of cells, the blocks, so that each block has the same total. Blocks overlap, so each cell is normalized several times, with different neighbors. With L2-Hys, the block vector is normalized to length 1, values above 0.2 are cut to 0.2, and the vector is normalized again.

Kernel or operator

The gradient uses the centered difference [-1, 0, 1] in x and in y, without smoothing. Dalal and Triggs found that this simple kernel works best. The Inspector asks the engine for the stages with the current settings.

Parameters

Parameter Default Values Meaning
Orientation bins orientations 9 2 to 36 The number of bins between 0 and 180 degrees. Dalal and Triggs use 9.
Cell size cellSize 8 4 to 64 px The side of the square cells. Dalal and Triggs use 8.
Block size blockSize 3 1 to 8 The side of the blocks of cells that are normalized together. scikit-image uses 3.
Block normalization blockNorm L2-Hys L1, L1 then square root, L2, L2-Hys How each block is normalized. L2-Hys is L2, then values are clipped at 0.2 and normalized again, as in the paper.

Implementation

hog_filter in engine/src/pixelnexus_engine/filters/texture.py calls skimage.feature.hog on the 8-bit luma with visualize=True. Here scikit-image puts each pixel into a single bin and divides each cell histogram by the number of pixels in the cell. Dalal and Triggs share each vote between neighboring bins and cells, and scikit-image does not. The view draws the cell histograms before the block normalization, so the block settings do not change it. Each line is drawn across its gradient direction, so it runs along the edges in the cell. The view is stretched so its brightest line is white. Cells that do not fit at the right and bottom edges are left out.

Worked example

Cell size (px) Block size Cells Descriptor length
8 2 1024 34596
8 3 1024 72900
16 2 256 8100
16 3 256 15876
32 2 64 1764
32 3 64 2916

The Siemens star at 256 x 256 with 9 orientation bins. The descriptor has one histogram of 9 bins for each cell of each block, and blocks overlap by all but one cell, so most cells count in several blocks. The view is the same for both block sizes.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels.

Artifacts and limitations

Limitations

  • The view does not show the block normalization.
  • Cells that do not fit at the right and bottom edges are not drawn.

Artifacts

  • Star shapes in noisy cells, where all directions are present.

Tests

The view must be the visualization of scikit-image, stretched, for two block norms. Next to a vertical edge, the cell must show one near vertical line. The block size and norm must not change the view, and a flat image must give a black view. The engine API test runs the filter.

References

  • Dalal, N., and Triggs, B. Histograms of oriented gradients for human detection. IEEE Conference on Computer Vision and Pattern Recognition, 886 to 893, 2005. DOI 10.1109/CVPR.2005.177.
  • scikit-image documentation, skimage.feature.hog.