hog.py 1.64 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
    Name: hog.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
11 12 13 14 15
	
    Change parameter Visualise for Visualize because is deprecaded
    Date:02/01/2019
    Author: Diego Andre Sant Ana		 	
		
16 17 18 19 20 21 22 23
"""
from skimage import feature

from util.utils import ImageUtils

from extractor import Extractor

class HOG(Extractor):
24
    """Implements HOG (Histogram of Oriented Gradient) feature extraction."""
25 26

    
27 28
    def __init__(self):
        pass        
29 30
        
    
31
    def run(self, image):
32
        """Extract HOG (Histogram of Oriented Gradient) features.
33
        
34 35 36 37 38 39 40 41 42 43
        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.
        """
44
        image_grayscale = ImageUtils.image_grayscale(image, bgr=True)
45
        image_128x128 = ImageUtils.image_resize(image_grayscale, 128, 128)
46

47
        values, _ = feature.hog(image_128x128, orientations=8, pixels_per_cell=(32, 32),
48 49 50
                                cells_per_block=(1, 1), visualise=True)

        labels = [m + n for m, n in zip(['hog_'] * len(values), map(str, range(0, len(values))))]
51
        types = [Extractor.NUMERIC] * len(labels)
52 53

        return labels, types, list(values)