对HOG算法每一个步骤具体详细的介绍可参考:HOG特征——行人识别
对HOG算法的特点和思想的介绍可参考:目标检测的图像特征提取之(一)HOG特征
调用Python中的skimage库提取图像HOG特征的示例代码如下,代码摘自 图像特征工程 HOG特征描述子介绍:
from skimage.io import imread, imshow
from skimage.transform import resize
from skimage.feature import hog
from skimage import exposure
import matplotlib.pyplot as plt
img = imread('lena.jpg')
resized_img = resize(img, (128, 64))
# 创建hog特征
fd, hog_image = hog(resized_img,
orientations=9, # Number of orientation bins
pixels_per_cell=(8, 8), # Size (in pixels) of a cell
cells_per_block=(2, 2), # Number of cells in each block
block_norm='L2', # Normalization using L2-norm
visualize=True, # Return an image of the HOG
multichannel=True)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8), sharex=True, sharey=True)
ax1.imshow(resized_img, cmap=plt.cm.gray)
ax1.set_title('Input image')
# 缩放直方图以便更好地显示
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)
ax2.set_title('HoG image')
plt.show()
运行结果:

程序中的fd就是输入图像的HOG特征向量,向量长度为3780. 这个数值的计算方法可参考本文第一行的链接。