Learning record - image gradient histogram features

Feature descriptors gradient histogram of the image

Feature descriptor is to simplify the image represented by the image extracting useful information, and irrelevant information is discarded;

Gradient histogram (Histogram of Gradients, HOG)

1. The image is divided into cell according to a set ratio, such as an image 1600 * 3200 * 16 a ratio of 16, can be divided into the cell 100 * 200, where each cell is 16 pixels * 16 * Channel. ( For color images, three kinds of channels will be assessed gradient calculated, taking the maximum gradient)

2. The calculation of the magnitude and direction of each cell (first calculate the x and y directions, respectively, to gx, gy represent, and then combined)

Gradient direction value of the absolute value, and therefore the angular range is [0, 180 °] is calculated gradient histogram of the pixel cell, the first angle range is divided into 9 parts, i.e. 9 bins, each of 20 ° as a unit, i.e. these pixels may be divided into nine groups according to the angle. All the gradient values corresponding to the pixels in every accumulates values 9 can be obtained. The histogram array is composed of nine values, corresponding to the angle 0,20,40,60 ... 160.

Such pixels above direction in the drawing surrounded by blue circle, the angle is 80 degrees, the amplitude corresponding to the pixel 2, the histogram bin corresponding to 80 plus 2 degrees. Red circle surrounding the pixel, an angle of 10 degrees between 0 degrees and 20 degrees, the amplitude is 4, then the value of the gradient was divided in proportion to 0 degrees and 20 degrees corresponding to the bin, each of which is applied 2.

Now with a gradient histogram of number 9 in place of the original three-dimensional matrix, i.e., instead of 16 * 16 * 2 values. At this time, the image elements representing 100 * 200 * 9.

3. Block normalization

HOG to a region 16 * 16 as a cell, again with 2 * 2 cell as a group, become a block. Each cell has nine values, so there is a 2 * 2 block * 9 = 36 values, the HOG block obtained through the sliding window.

Gradient overall image is very sensitive to light, such as to darken the image by the pixel value by 2 all, then the gradient magnitude will be reduced by half, so the value of the histogram will be reduced by half. Ideally, we want our feature descriptors are not affected by illumination changes, then we need to histogram "normalized" .

It illustrates how a normalized histogram before check vectors of length 3 is how a normalized: Suppose we have a vector  [128,64,32]length, vector is sqrt (128 ^ 2 + 64 ^ 2 + 32 ^ 2) = 146.64, which is called the L2 vector norm. Each element of this vector is divided by 146.64 to get a normalized vector  [0.87, 0.43, 0.22]. Now there is a new vector, is 2 times [128x2, 64x2, 32x2] first vector, that is  [256, 128, 64], we will this vector is normalized, you can see the results after the normalized first vector and return a result of the same. So, for vector normalization can eliminate the influence of the overall illumination.

Know how normalized, now the gradient block histograms are normalized (note not a cell), a block there are four histograms, these four histogram spliced ​​vector of length 36, then this vector is normalized.

Because the sliding window is used, so each swipe again, in this window is calculated normalized vectors of length 36.

4. The feature vector is calculated HOG

The image of a 1600 * 3200 above cell have said gradient calculation 16 * 16, * 2, and 1 and 2 in steps of normalization block sliding operation, HOG feature vector is finally obtained (1600 / 16-1) * ( 3200 / 16-1) * 9 * (2 * 2), i.e., [1 110 484].

Code

# 计算x和y以及合并的梯度值和方向
import cv2
import numpy as np
​
# Read image
img = cv2.imread('runner.jpg')
img = np.float32(img) / 255.0  # 归一化
​
# 计算x和y方向的梯度
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0, ksize=1)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1, ksize=1)
​
# 计算合梯度的幅值和方向(角度)
mag, angle = cv2.cartToPolar(gx, gy, angleInDegrees=True)



# 计算整体的HOG特征向量
from skimage import feature, exposure
import cv2
image = cv2.imread('runner.jpg')
fd, hog_image = feature.hog(image, orientations=9, pixels_per_cell=(16, 16),
                    cells_per_block=(2, 2), visualize=True)
​
# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
​
cv2.imshow('img', image)
cv2.imshow('hog', hog_image_rescaled)
cv2.waitKey(0)==ord('q')

 

references

A text explaining Histogram of Oriented Gradients (HOG)

 

Published 117 original articles · won praise 166 · views 260 000 +

Guess you like

Origin blog.csdn.net/u010420283/article/details/104885754