HOG输出维度计算方法
一、HOG API
from skimage import feature as ft
features = ft.hog(image, # input image
orientations=ori, # number of bins
pixels_per_cell=ppc, # pixel per cell
cells_per_block=cpb, # cells per blcok
block_norm = 'L1', # block norm : str {‘L1’, ‘L1-sqrt’, ‘L2’, ‘L2-Hys’}
transform_sqrt = True, # power law compression (also known as gamma correction)
feature_vector=True, # flatten the final vectors
visualise=False) # return HOG map
使用方式如下:
from skimage.feature import hog
import scipy
def rgb2gray(rgb_image):
"""
Convert RGB image to grayscale
Returns: grayscale image
"""
return np.dot(rgb_image[..., :3], [0.299, 0.587, 0.144])
def get_hog_feature(img_path, save_path):
image = scipy.misc.imread(img_path)
gray = rgb2gray(image) / 255.0
feature = hog(gray, orientations=9, block_norm='L1', pixels_per_cell=[8, 8], cells_per_block=[4, 4],
visualize=False, transform_sqrt=True)
二、HOG特征维度计算
我这里使用的图像是Fruits-360中的,图像大小为100x100.
根据代码orientations=9
得知bins=9.
根据代码pixels_per_cell=[8, 8], cells_per_block=[4, 4]
得知,
每个cell的大小为8x8,每个block包含4x4=16个cell,每个block的大小为(8x4)x(8x4)=32x32.
block stride默认为8x8,(图像大小100 - block大小32)/ 步长8 + 1 = 9
因此block的个数为9x9=81个.
所以特征维度为bins x block个数 x 每个block包含的cell个数 = 9 x (9x9) x (4x4) = 11664.
可见,实际运行得到的和计算出来的一致.