72 min total

Computer Vision & OpenCV

HOG and SIFT

HOG and SIFT

Histogram of Oriented Gradients (HOG): algorithm that produces feature descriptor used for human detection. Captures structure of object by analyzing distribution of gradient orientations

Step01 - Extract a square window (Block): Given an image I and pixel location (i, j) define a block (16x16) around this pixel, block is the region where we will analyze gradients and orientations

Step02 - Divide each block into square (8x8) cells, each cell captures local edge orientations in different regions of blocks

The notebook writes arctan(Iᵧ/Iₓ). atan2(Iᵧ,Iₓ) retains the quadrant and handles Iₓ=0 without explicitly dividing by zero. For unsigned orientation histograms, reduce the resulting direction modulo 180°.

Step 3: Compute image gradients and their orientations. Each cell accumulates a histogram with nine orientation bins over 0–180 degrees, weighted by gradient magnitude.

Higher the gradient magnitude, more it contributes to orientation bin.

Step04 - once we compute orientation histograms for each cell within a block, we concatenate them into a single feature vector.

4 cells, 9-bin histogram in each then Feature vector length: 36

Step05 - we normalize feature vector to make descriptor robust to illumination changes.

  • Option 1: L2 Norm

v is feature vector e is constant

L2 helps reduce sensitivity to lighting and contrast variations

  • Option 2: L1 Norm

Slightly less sensitive to outliers

  • Option 3: L2-Hys Norm

Apply L2 then Clip values that exceed 0.2 (set them to 0.2) then Reapply L2

Prevents few strong gradients from dominating descriptor

Concatenating four nine-bin cells gives a 36-dimensional descriptor for one block. Concatenating blocks gives the full HOG descriptor.

Quantizing Gradients into bins

BinAngle range (degrees)
10–20
220–40
340–60
460–80
580–100
6100–120
7120–140
8140–160
9160–180

Here θ₁ and θ₂ are the two neighboring reference orientations bracketing θ. The notebook calls them bin edges; implementations often interpolate between bin centers. Use the same convention throughout.

Instead of assigning gradient entirely to one bin, we use bilinear (fractional) interpolation

Means gradient contributes to two neighboring bins based on how close the angle is to them

For each pixel, we compute angle & magnitude

In the orientation histogram, the horizontal axis lists orientation bins and the vertical axis records the accumulated gradient magnitude assigned to each bin. Each pixel votes with its gradient magnitude, split between neighboring orientations according to the interpolation weights.

Example A

Gradient magnitude M = 10

Gradient angle θ = 50°. For this arithmetic example, use neighboring reference orientations of 40° and 60°.

  • To compute weights:-

For interpolation between reference orientations 40° and 60°, an orientation of 50° gets equal weights.

  • To distribute magnitude:-

With gradient magnitude 10, each of the two neighboring orientation bins receives 0.5 × 10 = 5. Bin-center and interval-label conventions must be used consistently.

The source assigns 0.5×10=5 to “bin 3 (40°–60°)” and 0.5×10=5 to “bin 4 (60°–80°).” Those interval labels do not consistently describe the reference orientations 40° and 60°. Keep the worked interpolation weights, but define bin centers explicitly before assigning array indices.

Example B

Gradient magnitude M = 8

Gradient angle θ = 47°. Use the same reference orientations of 40° and 60°.

  • To distribute magnitude:-

With magnitude 8 and interpolation weights 0.65 and 0.35, the contributions are 5.2 and 2.8.

The source’s corresponding bin-3 and bin-4 arithmetic is 0.65×8=5.2 and 0.35×8=2.8. These are the two neighboring-reference contributions; the exact bin indices depend on the bin-center convention.

  • To compute weights:-
Orientation histogram with interpolated bin contributions

Scale-Invariant Feature Transform (SIFT): algorithm that produces features that are robust to scale, rotation, illumination, minor distortions.

Properties of SIFT:

  • invariant to scale
  • invariant to rotation
  • invariant to translation
  • invariant to illumination

SIFT process

Step01 - Scale space extrema detection: find interest points that are invariant to scale, achieved using DoG filtering to detect blobs. The original image is blurred with Gaussian filter at multiple scales (differing sigma values σ). Each blurred image is subtracted from the next producing a Difference-of-Gaussian image. Each pixel is compared to its 26 neighbors (8 in the current scale, 9 in the scale above, and 9 in the scale below).

If a pixel is greater or smaller than all its neighbors, its identified as a keypoint.

Gaussian scale space and difference-of-Gaussian extrema

Step 2: Refine candidate keypoint locations and scales with a Taylor approximation; reject low-contrast candidates and unstable responses along edges.

Step03 - Orientation assignment: Each keypoint is assigned an orientation based on local image gradients. 16x16 window taken around the keypoint, gradient magnitude and direction computed for each pixel. A histogram of orientations (36 bins from 0 to 360) is created. Dominant orientation (highest peak in histogram) is assigned to the keypoint. This is done to achieve rotation invariance.

Step04 - Keypoint Descriptor Generation: this descriptor encodes local gradient information to uniquely describe the keypoints. A 16x16 window is obtained around keypoint and rotated (as per pervious dominant orientation) then divided into 4x4 subregions, for each subregion a histogram of 8 gradient directions is created resulting in a 4x4x8=128 dim feature vector per keypoint.

SIFT descriptor construction from local image gradients

Step 5: L2-normalize the 128-dimensional descriptor, clip entries above 0.2, and normalize again to reduce sensitivity to illumination changes.

~ We finally get 128 dim feature descriptors per keypoint from an image in this manner

Automatic Scale selection: Goal of this process in SIFT is to find keypoints that are consistent across different scales. σ)) measures image features at different scale. LoG filter detects blobs in an image (circular structures around keypoints) but is computationally expensive so we use DoG (approximation of LoG) which is much faster. The function f should have strong responses at similar feature locations at different scales.