本文最后更新于:2024年1月14日 晚上

HOG 特征广泛应用于物体识别等领域,但大面积计算 HOG 特征时重复统计会严重拖慢运行速度,使用积分图可以显著加速特征提取。

背景

加速原理

  • 对于 $W\times H$ 的图像 $I$
  • HOG 特征会在提取前根据不同梯度角度确定该点特征属于哪个 bin,计算时会将 bin 相同的值相加作为局部区域特定角度的 HOG 特征,假设当前确定了总bin 数量为 $N$
  • 那么在计算时,我们首先求得每个点的特征幅值和方向
  • 根据方向确定每个点的 bin 下标 ${0, 1, …,N-1}$
  • 创建临时的特征矩阵$F$, 维度为 $W \times H \times N$,将特征幅值填入矩阵对应位置和下标通道中
  • 对 $F$ 创建积分图 $S$, 那么计算任意 HOG 特征可以用 $S$ 以 $O(1)$ 时间复杂度完成

Python OpenCV 实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def gradient_integral_map(img, angle_step):
H, W = img.shape[:2]

# 梯度积分图
angle_feature_channel_num = 360 // angle_step
hog_matrix = np.zeros([H+1, W+1, angle_feature_channel_num], dtype='float32')
hog_gra_matrix = np.zeros([H, W, angle_feature_channel_num], dtype='float32')

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)
angle = np.clip(angle, None, 359.99999)
angle_index = angle // angle_step

for channel_index in range(angle_feature_channel_num):
temp_map = mag * (angle_index == channel_index)
hog_gra_matrix[:,:,channel_index] = temp_map
sum_map = cv2.integral(temp_map)
hog_matrix[:,:,channel_index] = sum_map

return hog_matrix, hog_gra_matrix

参考资料



文章链接:
https://www.zywvvd.com/notes/study/image-processing/feature-extraction/hog-speedup/hog-speedup/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

积分图加速提取 HOG 特征
https://www.zywvvd.com/notes/study/image-processing/feature-extraction/hog-speedup/hog-speedup/
作者
Yiwei Zhang
发布于
2023年1月10日
许可协议