gabor.py 2.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
    Extract Gabor features.
    
    Hans G. Feichtinger, Thomas Strohmer: "Gabor Analysis and Algorithms", Birkhäuser, 1998
    
    Name: gabor.py
    Author: Hemerson Pistori (pistori@ucdb.br)

    Based on: https://github.com/riaanvddool/scikits-image/blob/master/doc/examples/plot_gabor.py

"""

import numpy as np

from util.utils import ImageUtils
from extractor import Extractor
from scipy import ndimage as ndi
from skimage import feature
22
from skimage.filters import gabor_kernel
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
from skimage.util import img_as_float



class GABOR(Extractor):
    """Implements GABOR feature extraction."""

    
    def __init__(self):
        pass        
        
    
    def run(self, image):
        """Extract GABOR features.
        
        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.
        """
48
        #deprecaded
49 50 51 52 53 54 55 56 57 58 59
        def compute_feats(image, kernels):
            feats = np.zeros((len(kernels), 2), dtype=np.double)
            for k, kernel in enumerate(kernels): 
                filtered = ndi.convolve(image, kernel, mode='wrap') 
                feats[k, 0] = filtered.mean()
                feats[k, 1] = filtered.var()

                values.append(feats[k, 0])
                values.append(feats[k, 1])
            return feats 

60
        #deprecaded
61 62 63 64 65 66 67
        def power(image, kernel):
                image = (image - image.mean()) / image.std()
                return np.sqrt(ndi.convolve(image, np.real(kernel), mode='wrap')**2 +
                            ndi.convolve(image, np.imag(kernel), mode='wrap')**2)


        image_grayscale = ImageUtils.image_grayscale(image, bgr = True)
68
        image_float = img_as_float(image_grayscale)
69 70 71 72 73 74 75


        #Prepare filter bank kernels
        labels = []
        values = []
        kernels = []
        index = 0
76 77
        for theta in range(8):
            theta = theta / 8. * np.pi
78
            for sigma in (1, 3):
79
                for frequency in (0.01, 0.10, 0.25, 0.5,0.9):
80 81 82 83 84 85 86 87 88
                    kernel = np.real(gabor_kernel(frequency, theta=theta, sigma_x=sigma, sigma_y=sigma))
                    #kernels.append(kernel)

                    filtered = ndi.convolve(image_float, kernel, mode='wrap')
                    import time

                    values.append(filtered.mean())
                    values.append(filtered.var())
                    #print ("Thet_%f_Sigma_%i_Frequencia_%.2f" % (theta, sigma, frequency))
89 90 91 92
                    for stat in ("Mean", "Variance"):
                        labels.append("Thet_%f_Sigma_%i_Frequencia_%.2f_%s" % (theta, sigma, frequency, stat))

   
93
        #compute_feats(image_float,kernels)
94 95 96 97 98 99 100 101 102

        types = ['numeric'] * len(labels)

        return labels, types, values