HOG 特征广泛应用于物体识别等领域,但大面积计算 HOG 特征时重复统计会严重拖慢运行速度,使用积分图可以显著加速特征提取。
背景
- HOG 特征介绍
- 积分图介绍
加速原理
Python OpenCV 实现
代码语言:javascript复制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://cloud.tencent.com/developer/article/2142112
- https://cloud.tencent.com/developer/article/2068852