hog.py 1.52 KB
Newer Older
1 2 3 4
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
5
    Extract HOG (Histogram of Oriented Gradient) features.
6
    
7 8
    Dalal, N and Triggs, B, Histograms of Oriented Gradients for Human Detection, IEEE Computer Society Conference on Computer Vision and Pattern Recognition 2005 San Diego, CA, USA
    
9 10 11 12 13 14 15 16 17 18
    Name: hog.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
from skimage import feature

from util.utils import ImageUtils

from extractor import Extractor

class HOG(Extractor):
19
    """Implements HOG (Histogram of Oriented Gradient) feature extraction."""
20 21

    
22 23
    def __init__(self):
        pass        
24 25
        
    
26
    def run(self, image):
27
        """Extract HOG (Histogram of Oriented Gradient) features.
28
        
29 30 31 32 33 34 35 36 37 38
        Parameters
        ----------
        image : opencv image
            Image to be analyzed.
        
        Returns
        -------
        features : tuple
            Returns a tuple containing a list of labels, type and values for each feature extracted.
        """
39 40
        image_grayscale = ImageUtils.image_grayscale(image, bgr = True)
        image_128x128 = ImageUtils.image_resize(image_grayscale, 128, 128)
41
        
42
        values, _ = feature.hog(image_128x128, orientations=8, pixels_per_cell=(32, 32),
43
                    cells_per_block=(1, 1), visualise=True)
44 45 46
                    
        labels = [m+n for m,n in zip(['hog_'] * len(values),map(str,range(0,len(values))))]
        types = [Extractor.NUMERIC] * len(labels)
47 48

        return labels, types, list(values)