glcm.py 2.25 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 20 21 22 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 48 49 50 51 52 53 54
    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):

    
    def __init__(self, glcm_levels = 256):
        self.glcm_levels = glcm_levels
        
    
    def run(self, image):
        
        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