The Histogram of Oriented Gradients (HOG) describes local shape by how gradient strength is distributed over orientations in small cells, normalized over larger blocks for robustness to lighting.
Crop and gradients. With cell size , first crop the image to its top-left part. On the cropped image use the centred difference without smoothing, and , with in the first and last column and in the first and last row. The magnitude is and the unsigned orientation is in degrees, modulo 180, so .
Cell histograms. Use bins of width with centres for . Each pixel splits its magnitude between the two nearest centres by linear interpolation: with , and , bin receives and bin receives . The bins wrap around, so with an angle of votes into bins 8 and 0.
Blocks. A block is a group of cells; blocks slide one cell at a time and are visited in raster order. A block's vector concatenates the histograms of its cells, cells in raster order. Normalize it with L2-Hys: , then clip every entry at clip, then normalize again the same way. The descriptor is the concatenation of all block vectors.
Implement cell_histograms(img, cell=8, nbins=9) returning a float array of shape (H // cell, W // cell, nbins), and hog(img, cell=8, block=2, nbins=9, clip=0.2, eps=1e-5) returning the 1-D float descriptor.