lbp.py 1.87 KB
Newer Older
1 2 3 4
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
5
    Extract LBP (Local Binary Patterns) features.
6
    
7 8
    Multiresolution Gray-Scale and Rotation Invariant Texture Classification with Local Binary Patterns. Timo Ojala, Matti Pietikainen, Topi Maenpaa. 2002.

9 10 11
    Name: lpb.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
12
import numpy as np
13 14 15 16 17 18 19
from skimage import feature

from util.utils import ImageUtils

from extractor import Extractor

class LBP(Extractor):
20
    """Implements LBP (Local Binary Patterns) feature extraction."""
21
    
22
    def __init__(self, lbp_radius = 2, n_bins = 18):
23 24 25 26 27 28 29 30 31
        """Constructor.
        
        Parameters
        ----------
        lbp_radius : float, optional, default = 2
            Radius of circle (spatial resolution of the operator).
        n_bins : integer, optional, default = 18
            Number of circularly symmetric neighbour set points (quantization of the angular space).
        """
32 33 34 35
        self.lbp_radius = lbp_radius
        self.n_bins = n_bins
        
    
36
    def run(self, image):
37 38 39 40 41 42
        """Extract LBP (Local Binary Patterns) features.
        
        Parameters
        ----------
        image : opencv image
            Image to be analyzed.
43
        
44 45 46 47 48
        Returns
        -------
        features : tuple
            Returns a tuple containing a list of labels, type and values for each feature extracted.
        """
49
        image_grayscale = ImageUtils.image_grayscale(image, bgr = True)
50
        
51 52 53 54
        lbp = feature.local_binary_pattern(image_grayscale, 8 * self.lbp_radius, self.lbp_radius, 'uniform')
        values, _ = np.histogram(lbp, normed=True, bins=self.n_bins, range=(0, self.n_bins))        
        
        labels = [m+n for m,n in zip(['lbp_'] * len(values),map(str,range(0,len(values))))]
55 56 57
        types = [Extractor.NUMERIC] * len(labels)

        return labels, types, list(values)