Search code examples
feature-extractionscikit-imageimage-maskingglcm

Using skimage for non-rectangular image areas


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!


Solution

  • 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.sstatic.net/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.