Get HOG image features from OpenCV + Python?

前端 未结 7 588
小鲜肉
小鲜肉 2020-11-30 18:58

I\'ve read this post about how to use OpenCV\'s HOG-based pedestrian detector: How can I detect and track people using OpenCV?

I want to use HOG for detecting othe

7条回答
  •  囚心锁ツ
    2020-11-30 19:54

    Here is a solution that uses only OpenCV:

    import numpy as np
    import cv2
    import matplotlib.pyplot as plt
    
    img = cv2.cvtColor(cv2.imread("/home/me/Downloads/cat.jpg"),
                       cv2.COLOR_BGR2GRAY)
    
    cell_size = (8, 8)  # h x w in pixels
    block_size = (2, 2)  # h x w in cells
    nbins = 9  # number of orientation bins
    
    # winSize is the size of the image cropped to an multiple of the cell size
    hog = cv2.HOGDescriptor(_winSize=(img.shape[1] // cell_size[1] * cell_size[1],
                                      img.shape[0] // cell_size[0] * cell_size[0]),
                            _blockSize=(block_size[1] * cell_size[1],
                                        block_size[0] * cell_size[0]),
                            _blockStride=(cell_size[1], cell_size[0]),
                            _cellSize=(cell_size[1], cell_size[0]),
                            _nbins=nbins)
    
    n_cells = (img.shape[0] // cell_size[0], img.shape[1] // cell_size[1])
    hog_feats = hog.compute(img)\
                   .reshape(n_cells[1] - block_size[1] + 1,
                            n_cells[0] - block_size[0] + 1,
                            block_size[0], block_size[1], nbins) \
                   .transpose((1, 0, 2, 3, 4))  # index blocks by rows first
    # hog_feats now contains the gradient amplitudes for each direction,
    # for each cell of its group for each group. Indexing is by rows then columns.
    
    gradients = np.zeros((n_cells[0], n_cells[1], nbins))
    
    # count cells (border cells appear less often across overlapping groups)
    cell_count = np.full((n_cells[0], n_cells[1], 1), 0, dtype=int)
    
    for off_y in range(block_size[0]):
        for off_x in range(block_size[1]):
            gradients[off_y:n_cells[0] - block_size[0] + off_y + 1,
                      off_x:n_cells[1] - block_size[1] + off_x + 1] += \
                hog_feats[:, :, off_y, off_x, :]
            cell_count[off_y:n_cells[0] - block_size[0] + off_y + 1,
                       off_x:n_cells[1] - block_size[1] + off_x + 1] += 1
    
    # Average gradients
    gradients /= cell_count
    
    # Preview
    plt.figure()
    plt.imshow(img, cmap='gray')
    plt.show()
    
    bin = 5  # angle is 360 / nbins * direction
    plt.pcolor(gradients[:, :, bin])
    plt.gca().invert_yaxis()
    plt.gca().set_aspect('equal', adjustable='box')
    plt.colorbar()
    plt.show()
    

    I have used HOG descriptor computation and visualization to understand the data layout and vectorized the loops over groups.

提交回复
热议问题