参考资料:https://www.cnblogs.com/alexme/p/11361563.html
https://blog.csdn.net/qq_43348528/article/details/108638030
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from skimage import exposure
from skimage.feature import hog
from skimage import data,color,exposureimg = cv.imread("../SampleImages/tifa.jpg", cv.IMREAD_COLOR)
plt.imshow(img[:,:,::-1])#转换为灰度图
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
plt.imshow(img_gray, plt.cm.gray)#HoG特征计算
#参考资料:https://www.cnblogs.com/alexme/p/11361563.html
# https://blog.csdn.net/qq_43348528/article/details/108638030
#1. 创建HoG对象
# hog = cv.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nbins)
# winSize:检测窗口大小
# blockStride:block块的滑动步长
# cellSize:cell单元大小
# nbins:统计梯度的方向数目,一般为9,即一个cell统计9个角度范围的梯度直方图
winSize = (64,128)
blockSize = (16,16)
blockStride = (8,8)
cellSize = (8,8)
nbins = 9
hog_obj = cv.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins)
#2. 计算HoG特征
# hogDes = hog.compute(img,winStride,padding)
# img:原图
# winStride:检测窗口的滑动步长
# padding:填充,在图像周围填充点的边界处理
# 返回hogDes:对整幅图像的HoG特征描述符
hogDes = hog_obj.compute(img_gray, winStride=(8,8))
#使用OPENCV的HOGDescriptor不能将HOG处理后的梯度直方图结合原图显示
print("HogDes Size:",hogDes.size)
print(hogDes)#使用skimage
fd,hog_image = hog(img_gray, orientations=8, pixels_per_cell=(16,16), cells_per_block=(1,1), visualize=True)
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0,0.02))
#叠加HoG梯度直方图到图像上
img_hog_display = img_gray * hog_image_rescaled
plt.figure(figsize=(16,16), dpi=80)
plt.imshow(img_hog_display, plt.cm.gray)