1

Let's say I'm concerned with part of an image that I'm wanting to calculate a GLCM for that's not rectangular. How should I go about this? I've made a masking procedure that zeroes out the portion of the image that I don't care about, I just don't know how to take this "masked" image without considering the zeroed out portions of the image...

Thanks for your help!

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Brad Flynn
  • 145
  • 8
  • One possible solution would consist in using mahotas library to compute GLCM features as described [here](https://stackoverflow.com/questions/40703086/python-taking-the-glcm-of-a-non-rectangular-region/42837786#42837786) – Tonechas Jun 09 '17 at 21:41

1 Answers1

2

If you are able to assign the zero intensity value to background pixels, you can obtain the GLCM of the region of interest by simply discarding the first line and the first column of the full image's GLCM. This actually equivals to getting rid of those co-occurrences that involve background pixels.

Demo

The following snippet demonstrates how to extract a couple of Haralick features from the GLCMs of a circular object on a black background:

circular object

In [25]: import numpy as np

In [26]: from skimage import io

In [27]: from skimage.feature import greycomatrix, greycoprops

In [28]: img = io.imread('https://i.stack.imgur.com/6ESoP.png')

In [29]: glcm = greycomatrix(img, 
    ...:                     distances=[1, 2], 
    ...:                     angles=[0, np.pi/4, np.pi/2, 3*np.pi/4],
    ...:                     symmetric=True,
    ...:                     normed=False)
    ...: 

In [30]: glcm_br = glcm[1:, 1:, :, :]

In [31]: glcm_br_norm = np.true_divide(glcm_br, glcm_br.sum(axis=(0, 1)))

In [32]: np.set_printoptions(threshold=1000, precision=4)

In [33]: props = ['energy', 'homogeneity']

In [34]: feats_br = np.hstack([greycoprops(glcm_br_norm, p).ravel() for p in props])

In [35]: feats_br
Out[35]: 
array([ 0.0193,  0.0156,  0.0173,  0.0166,  0.0151,  0.0156,  0.0136,
        0.0166,  0.1255,  0.0788,  0.0978,  0.0929,  0.0782,  0.0788,
        0.0545,  0.0929])

Please notice that the GLCM has to be normalized after getting rid of the first line and the first column of the GLCM of the full image.

Note: the suffix _br stands for background removed.

Community
  • 1
  • 1
Tonechas
  • 13,398
  • 16
  • 46
  • 80
  • thanks! Just so I'm clear, this doesn't "normalize" the matrix in some way? I'm just concerned if I have a larger masked-out area in one image than another, the first row/column will skew the others and I'll have a bad measurement. Does this make sense? Sorry if it's unclear – Brad Flynn Jun 12 '17 at 16:40
  • Yes, absolutely. I've edited my answer to fix the code. Thanks for spotting this. – Tonechas Jun 12 '17 at 17:11