Calculation of Entropy from a GLCM Image

I use the skimage library for most of the image analysis.

I have an RGB image and intend to extract features from the texture image such as entropy , energy , homogeneity and contrast .

Following are the steps that I am following:

 from skimage import io, color, feature from skimage.filters import rank rgbImg = io.imread(imgFlNm) grayImg = color.rgb2gray(rgbImg) print(grayImg.shape) # (667,1000), a 2 dimensional grayscale image glcm = feature.greycomatrix(grayImg, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4]) print(glcm.shape) # (256, 256, 1, 4) rank.entropy(glcm, disk(5)) # throws an error since entropy expects a 2-D array in its arguments rank.entropy(grayImg, disk(5)) # given an output. 

My question is, is the calculated entropy from the grayscale image (directly) the same as the entropy function extracted from GLCM (texture function)?

If not, what is the correct way to extract all texture objects from an image?

Notes: I already mentioned:

Entropy - skimage

GLCM Features - Texture

+7
python numpy entropy scikit-image glcm
source share
2 answers

Is the calculated entropy from the gray image (directly) the same as the entropy function extracted from GLCM (texture function)?

No, these two entropies are quite different:

  • skimage.filters.rank.entropy(grayImg, disk(5)) gives an array of the same size as grayImg , which contains local entropy from an image computed on a circular disk centered at the corresponding pixel and 5 pixel radius. Take a look at Entropy (information theory) to find out how entropy is calculated. The values ​​in this array are useful for segmentation (see this link to see an example of detecting an object based on entropy). If your goal is to describe the entropy of an image through a single (scalar) value, you can use skimage.measure.shannon_entropy(grayImg) . This function basically applies the following formula to the full image:
    entropy
    Where n is the number of gray levels (256 for 8-bit images), probability is the probability that the pixel will have a gray level intensity and base is the base of the logarithm function. When base set to 2, the return value is measured in bits .
  • The Gray Level Match Matrix (GLCM) is a histogram of matching shades of gray at a given offset in the image. Functions such as entropy, energy, contrast, correlation, etc., are usually extracted to describe the texture of an image. From several matching matrices calculated for different offsets. In this case, the entropy is defined as follows:
    entropy GLCM
    Where n and base again are the number of gray levels and the base of the logarithm function, respectively, and GLCM element means the probability of two pixels separated by a specified offset with intensity intensity and j . Unfortunately, entropy is not one of the GLCM properties that you can calculate using scikit-image * . If you want to compute this function, you need to pass GLCM to skimage.measure.shannon_entropy .

* At the time when it was last edited, the latest version of scikit-image 0.13.1.

If not, what is the correct way to extract all texture objects from an image?

There are many possibilities for describing the texture of an image, for example, local binary patterns, Gabor filters, bursts, law masks, and many others. Haralick GLCM is one of the most popular texture descriptors. One possible approach to describing image texture through GLCM functions is to compute GLCM for different offsets (each offset is determined by distance and angle) and extract various properties from each GLCM.

Consider, for example, three distances (1, 2, and 3 pixels), four angles (0, 45, 90, and 135 degrees) and two properties (energy and uniformity). This leads to 12 displacements (and therefore 12 GLCMs) and vector size functions 24 . Here is the code:

 import numpy as np from skimage import io, color, img_as_ubyte from skimage.feature import greycomatrix, greycoprops from sklearn.metrics.cluster import entropy rgbImg = io.imread('/img/01626ffdcd9476a0adbe66921077a4ee.jpg') grayImg = img_as_ubyte(color.rgb2gray(rgbImg)) distances = [1, 2, 3] angles = [0, np.pi/4, np.pi/2, 3*np.pi/4] properties = ['energy', 'homogeneity'] glcm = greycomatrix(grayImg, distances=distances, angles=angles, symmetric=True, normed=True) feats = np.hstack([greycoprops(glcm, prop).ravel() for prop in properties]) 

Results obtained using this image:

sample image - lion :

 In [56]: entropy(grayImg) Out[56]: 5.3864158185167534 In [57]: np.set_printoptions(precision=4) In [58]: print(feats) [ 0.026 0.0207 0.0237 0.0206 0.0201 0.0207 0.018 0.0206 0.0173 0.016 0.0157 0.016 0.3185 0.2433 0.2977 0.2389 0.2219 0.2433 0.1926 0.2389 0.1751 0.1598 0.1491 0.1565] 
+12
source share
 from skimage.feature import greycomatrix, greycoprops dis = (greycoprops(glcm, 'dissimilarity')) plt.hist(dis.ravel(), normed=True, bins=256, range=(0, 30),facecolor='0.5');plt.show() 
0
source share

All Articles