1

So I was going through a research paper [link], which has the following Algo for getting the texture of the image.

algo


So what i understand from the above algo is that I need make grids of 4x4 and find the GLCM matrices for each of those 4x4 sub matrices then calculate the properties.

But my issue with this method is that the image is of size 256x384 which gives 64*96 grids and calculating a GLCM matrix 64*96 times is extremly computation heavy, eapecially because i have 900 such images.

The code is as follows:

def texture_extract(img):
    distance = [1]
    angles = [0, np.pi/4, np.pi/2, 3*np.pi/4]
    properties = ['correlation', 'homogeneity', 'contrast', 'energy', 'dissimilarity']

    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray_img, (5,5), 0)

    texture_features = []
    for i in range(0, blur.shape[0], 4):
        for j in range(0, blur.shape[1], 4):
            block = blur[i:i+4, j:j+4]

            glcm_mat = greycomatrix(block, distances=distance, angles=angles, symmetric=True, normed=True)
            block_glcm = np.hstack([greycoprops(glcm_mat, props).ravel() for props in properties])
            texture_features.append(block_glcm)

    return np.concatenate(texture_features)

All I want to know is that, is my understanding of the algorithm correct or am i making a stupid mistake somewhere.

XXDIL
  • 220
  • 2
  • 10
  • calculating *anything* out of merely 4x4=16 values can't be that expensive, even if you have to do it only 64*96=6144 times. *how* do you do that currently and how long does it take? I see the loops, which can be improved, and I see some calls there, but I can't identify the code that works on 4x4 patches – Christoph Rackwitz Nov 01 '21 at 18:05
  • the greycomatrix() works on the 4x4 block and its computationally expensive. ie there will be 6144 GLCM matrices. but each of these matrices will be used to calculate 20 features (5 properties and each property has 4 angles). So it becomes 6144*20. – XXDIL Nov 01 '21 at 18:32

1 Answers1

0

I am using like that, general way is same with yours.

GLCM Properties Definitions

import numpy as np
from skimage.feature import greycomatrix, greycoprops
from skimage import io, color, img_as_ubyte

# GLCM properties
def contrast_feature(matrix_coocurrence):
    contrast = greycoprops(matrix_coocurrence, 'contrast')
    return contrast

def dissimilarity_feature(matrix_coocurrence):
    dissimilarity = greycoprops(matrix_coocurrence, 'dissimilarity')    
    return dissimilarity

def homogeneity_feature(matrix_coocurrence):
    homogeneity = greycoprops(matrix_coocurrence, 'homogeneity')
    return homogeneity

def energy_feature(matrix_coocurrence):
    energy = greycoprops(matrix_coocurrence, 'energy')
    return energy

def correlation_feature(matrix_coocurrence):
    correlation = greycoprops(matrix_coocurrence, 'correlation')
    return correlation

def asm_feature(matrix_coocurrence):
    asm = greycoprops(matrix_coocurrence, 'ASM')
    return asm

bins32 = np.array([0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160,
            168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 255]) #32-bit

Calculating the GLCM Properties

from skimage.feature import greycomatrix, greycoprops
from skimage import data
from skimage import util, exposure, data
import pywt
from skimage import io
from skimage.color import rgb2gray

lenx = x_train.shape[0] # Your image array!
X_correlationTRAIN = np.zeros((lenx, 4))
...

for i in range(lenx):
    image32 = x_train[i] # Your image array!
    image32 = rgb2gray(image32)
    np.clip(image32, 0, 255, out=image32)
    image = image32.astype('uint8')
    inds = np.digitize(image, bins32)

    max_value = inds.max()+1
    matrix_coocurrence = greycomatrix(inds, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4], levels=max_value, normed=False, symmetric=False)

    X_correlationTRAIN[i] = correlation_feature(matrix_coocurrence)
    ...
    ...