glcm.py 2.79 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
    Extract GLCM (Gray-Level Co-Occurrence Matrix) feature.
    
7 8
    Soh and Costas Tsatsoulis, Texture analysis of SAR sea ice imagery using gray level co-occurrence matrices, IEEE Transactions on geoscience and remote sensing, 1999.
    
9 10 11 12 13 14 15 16 17 18 19
    Name: glcm.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import numpy as np
from skimage import feature

from util.utils import ImageUtils

from extractor import Extractor

class GLCM(Extractor):
20
    """Implements GLCM (Gray-Level Co-Occurrence Matrix) feature extraction."""
21 22 23 24 25 26 27

    
    def __init__(self, glcm_levels = 256):
        self.glcm_levels = glcm_levels
        
    
    def run(self, image):
28 29
        """Extract the following texture properties defined in the GLCM: energy, contrast, correlation, homogeneity and dissimilarity,
        with distance 1 and 2 and angles 0, 45 and 90 degrees.
30
        
31 32 33 34 35 36 37 38 39 40
        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.
        """
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        image_grayscale = ImageUtils.image_grayscale(image, bgr = True)

        g = feature.greycomatrix(image_grayscale, [1, 2], [0, np.pi / 4, np.pi / 2], self.glcm_levels, normed=True, symmetric=True)

        contrast = feature.greycoprops(g, 'contrast').tolist()
        dissimilarity = feature.greycoprops(g, 'dissimilarity').tolist()
        homogeneity = feature.greycoprops(g, 'homogeneity').tolist()
        asm = feature.greycoprops(g, 'ASM').tolist()
        energy = feature.greycoprops(g, 'energy').tolist()
        correlation = feature.greycoprops(g, 'correlation').tolist()

        labels = [
            'glcm_cont_1_0', 'glcm_cont_1_45', 'glcm_cont_1_90', 'glcm_cont_2_0', 'glcm_cont_2_45', 'glcm_cont_2_90',
            'glcm_diss_1_0', 'glcm_diss_1_45', 'glcm_diss_1_90', 'glcm_diss_2_0', 'glcm_diss_2_45', 'glcm_diss_2_90',
            'glcm_homo_1_0', 'glcm_homo_1_45', 'glcm_homo_1_90', 'glcm_homo_2_0', 'glcm_homo_2_45', 'glcm_homo_2_90',
            'glcm_asm_1_0', 'glcm_asm_1_45', 'glcm_asm_1_90', 'glcm_asm_2_0', 'glcm_asm_2_45', 'glcm_asm_2_90',
            'glcm_ener_1_0', 'glcm_ener_1_45', 'glcm_ener_1_90', 'glcm_ener_2_0', 'glcm_ener_2_45', 'glcm_ener_2_90',
            'glcm_corr_1_0', 'glcm_corr_1_45', 'glcm_corr_1_90', 'glcm_corr_2_0', 'glcm_corr_2_45', 'glcm_corr_2_90',

        ]
        types = [Extractor.NUMERIC] * len(labels)

        values = contrast[0] + contrast[1] + dissimilarity[0] + dissimilarity[1] + homogeneity[0] + \
                  homogeneity[1] + asm[0] + asm[1] + energy[0] + energy[1] + correlation[0] + correlation[1]

        return labels, types, values