lbp.py 1.13 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
    Extract LBP (Local Binary Patterns) feature.
    
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 20 21
from skimage import feature

from util.utils import ImageUtils

from extractor import Extractor

class LBP(Extractor):

    
22
    def __init__(self, lbp_radius = 2, n_bins = 18):
23 24 25 26
        self.lbp_radius = lbp_radius
        self.n_bins = n_bins
        
    
27 28 29
    def run(self, image):
        
        image_grayscale = ImageUtils.image_grayscale(image, bgr = True)
30
        
31 32 33 34
        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))))]
35 36 37
        types = [Extractor.NUMERIC] * len(labels)

        return labels, types, list(values)