HOG(Histogram of Oriented Gradients)

参考资料:
Histogram of Oriented Gradients
斯坦福CS131-1718作业3、作业7

目标64×128×3-->3780

  • 预处理

剪出一个patch,缩放为64×128


  • 计算梯度图像

Left : Absolute value of x-gradient. Center : Absolute value of y-gradient. Right : Magnitude of gradient.
  • 在8×8的单元格内计算HOG

8×8×3 经过梯度计算得到8×8×2(2是分别是梯度大小和方向)最后再表示成9个柱子的直方图,可以用大小为9的数组来表示,越来越紧凑。单独的梯度可能是噪声,但8×8计算出来鲁棒性比较好。
8×8是个超参数,这对于64×128的行人图像比较合适地可以获取我们感兴趣的特征,人脸,头等等。
所有梯度根据方向分成9个格子,每个格子累加它们的幅度。
下图的计算过程:165度余20等于5,5/20=1/4,因此高的bin得到1/4,低的bin得到3/4。165在160~180中有3/4,在0度只有1/4。每个格子代表的是一个刻度,不是一个区间。165介于160和180(0)度之间,其中比较偏向于160度,故160度有3/4,180(0)度只有1/4。



可以看到下面的图片在0和180度附近的梯度幅度累加比较大,说明在这个方向上变化比较大。


  • 16×16 Block Normalization

为了不让光照影响上面的梯度计算,采用L2 规范化,向量除以向量长度。
一个16×16的patch可以根据前面步骤计算出4×9的特征向量,然后进行规范化;16×16的各自继续向右移动8个像素,重复上述操作。换行的时候也是移动8个像素。所以一张64×128的图像有8×16个8×8的格子,16×16的窗口按照上面的方式可以计算7×15次,每次有36个元素,故一共为7×15×36=3780元素


其主要的思想还是投票算法的思想,梯度之类的都是老套路了。

  • 算法具体实现

作业3用HOG在用harris选出角点后,需要配对两幅图片中对应的点,需要一个descriptor来描述一个个点附近的patch,后面好比较。简单的就是直接normalize,然后flatten,也就是NCC,这里改用hog。hog是根据patch梯度来的。

def hog_descriptor(patch, pixels_per_cell=(8,8)):
    """
    patch一般是16*16,cell是小的单元格,只有8*8
    Generating hog descriptor by the following steps:

    1. compute the gradient image in x and y (already done for you)
    2. compute gradient histograms
    3. normalize across block 
    4. flattening block into a feature vector

    Args:
        patch: grayscale image patch of shape (h, w)
        pixels_per_cell: size of a cell with shape (m, n)

    Returns:
        block: 1D array of shape ((h*w*n_bins)/(m*n))
    """
    assert (patch.shape[0] % pixels_per_cell[0] == 0),\
                'Heights of patch and cell do not match'
    assert (patch.shape[1] % pixels_per_cell[1] == 0),\
                'Widths of patch and cell do not match'

    n_bins = 9
    degrees_per_bin = 180 // n_bins

    Gx = filters.sobel_v(patch)
    Gy = filters.sobel_h(patch)
   
    # Unsigned gradients
    G = np.sqrt(Gx**2 + Gy**2)
    theta = (np.arctan2(Gy, Gx) * 180 / np.pi) % 180

    G_cells = view_as_blocks(G, block_shape=pixels_per_cell)
    theta_cells = view_as_blocks(theta, block_shape=pixels_per_cell)
    rows = G_cells.shape[0]
    cols = G_cells.shape[1]

    cells = np.zeros((rows, cols, n_bins))

    # Compute histogram per cell
    ### YOUR CODE HERE
    cell_rows,cell_cols = pixels_per_cell

    # 2. compute gradient histograms
    # r,c是patch中cell的定位,c_r,c_c是cell中每个元素的定位
    for r in range(rows):
        for c in range(cols):
            for c_r in range(cell_rows):
                for c_c in range(cell_cols):
                    degree = theta_cells[r][c][c_r][c_c]
                    value = G_cells[r][c][c_r][c_c]

                    lower_bin = int(degree / degrees_per_bin) % n_bins
                    upper_bin = (lower_bin + 1) % n_bins
                    ratio = float(degree % degrees_per_bin) / degrees_per_bin

                    cells[r][c][lower_bin] += (1 - ratio) * value
                    cells[r][c][upper_bin] += ratio * value

    block = (cells/np.linalg.norm(cells)).flatten()
    ### YOUR CODE HERE
    
    return block
  • HOG用于检测是否是人脸

作业7

计算多张已对齐的人脸的平均脸,和对应的HOG特征

检测是否是人脸

用窗口滑过检测的图片,选出一个个窗口,计算HOG,每次都跟上面计算出来的HOG特征向量进行点击,值比较大的地方就是人脸。



缺点

如果对图片进行缩放,那么可能就会识别错误,这个就需要用金字塔来表示图片了,见金字塔博客。

你可能感兴趣的:(HOG(Histogram of Oriented Gradients))