Breaking

Be Strong Let's Try!

Wednesday, 8 September 2021

Texture Based Descriptor And How To Use It In Python | Digital Image Processing

 Objective: 

The objective of this topic is to develop an understanding of texture based descriptors and how they can be further used. 

Theory: 

Texture Based Descriptors can be useful for gleaning such information from an image that can provide good features for the said image. Grey Level Co-occurrence Matrix contains the information of two gray levels occurring together in an image based on a predefined rule. The changes in the rule can change the GLCM. The following parameters of GLCM can be used for texture description:


Some Useful Commands:

The python package that will be needed for the following function is scikit-image. You may need to download the .whl file and install it manually if pip doesn’t work.

 1. To find the GLCM of an image: my_GLCM = skimage.feature.greycomatrix(my_image, [distances_at_which_the_covariance_is_to_be_checked], [angles_at_which_the_covariance_is_to_be_checked], levels=None, symmetric=False, normed=False)

The output of the above function will be a 4D array. The value my_GLCM [i,j,d,theta] is the number of times that grey-level j occurs at a distance d and at an angle theta from grey-level i. If normed is False, the output is of type uint32, otherwise it is float64. The dimensions are: levels x levels x number of distances x number of angles.

2. To calculate different properties of a GLCM matrix: 

my_property = skimage.feature.greycoprops(my_GLCM, prop=’my_property’) The prop flag can be set to ‘contrast’, ‘dissimilarity’, ‘homogeneity’, ‘energy’, ‘correlation’ or ‘ASM’

Here is the task to do:

Calculate the Grey Level Co-occurrence Matrix (GLCM) for following images. Then calculate the parameters of GLCM mentioned above for all the images and compare the results.

Texture Based Descriptor And How To Use It In Python | Digital Image Processing
Solution:

Code:

from skimage.feature import  greycomatrix, greycoprops
import cv2 as cv
def Result(img):
    result = greycomatrix(img,[
1],[0],levels=256)
   
return result
def ASM(R):
    a = greycoprops(R,
prop='ASM')
   
return a
def Contrast(R):
    ct = greycoprops(R,
prop='contrast')
   
return ct
def Correlation(R):
    cn = greycoprops(R,
prop='correlation')
   
return cn
def Dissimilarity(R):
    d = greycoprops(R,
prop='dissimilarity')
   
return d
def Energy(R):
    e = greycoprops(R,
prop='energy')
   
return e
def Homogeneity(R):
    h = greycoprops(R,
prop='homogeneity')
   
return h
# Reading 3 images
img1 = cv.imread(‘freestudystrom.tif',0)
img2 = cv.imread(
'freestudystrom.tif',0)
img3 = cv.imread(
'freestudystrom.tif',0)
# Calculating grey-level co-occurrence matrices
R1 = Result(img1)
R2 = Result(img2)
R3 = Result(img3)
# Calculating GLCM
GLCM1 = R1[:,:,0,0]
GLCM2 = R2[:,:,
0,0]
GLCM3 = R3[:,:,
0,0]
# Calculating texture property ASM
ASM1 = ASM(R1)
ASM2 = ASM(R2)
ASM3 = ASM(R3)
# Calculating texture property Contrast
Contrast1 = Contrast(R1)
Contrast2 = Contrast(R2)
Contrast3 = Contrast(R3)
# Calculating texture property Correlation
Correlation1 = Correlation(R1)
Correlation2 = Correlation(R2)
Correlation3 = Correlation(R3)
# Calculating texture property Dissimilarity
Dissimilarity1 = Dissimilarity(R1)
Dissimilarity2 = Dissimilarity(R2)
Dissimilarity3 = Dissimilarity(R3)
# Calculating texture property Energy
Energy1 = Energy(R1)
Energy2 = Energy(R2)
Energy3 = Energy(R3)

# Calculating texture property Homogeneity
Homogeneity1 = Homogeneity(R1)
Homogeneity2 = Homogeneity(R2)
Homogeneity3 = Homogeneity(R3)

# Showing Input Images
cv.imshow('a',img1)
cv.imshow(
'b',img2)
cv.imshow(
'c',img3)
cv.waitKey(
0)
# Printing all parameters
print('Image                ','a                     ','b                ','c')
print('ASM              ',ASM1,'        ',ASM2,'       ',ASM3)
print('Contrast         ',Contrast1,'      ',Contrast2,'       ',Contrast3)
print('Correlation      ',Correlation1,'        ',Correlation2,'          ',Correlation3)
print('Dissimilarity    ',Dissimilarity1,'       ',Dissimilarity2,'          ',Dissimilarity3)
print('Energy           ',Energy1,'        ',Energy2,'           ',Energy3)

print('Homogeneity      ',Homogeneity1,'        ',Homogeneity2,'           ',Homogeneity3)

 

Output:


 

Texture Based Descriptor And How To Use It In Python | Digital Image Processing


Texture Based Descriptor And How To Use It In Python | Digital Image Processing



Texture Based Descriptor And How To Use It In Python | Digital Image Processing
Texture Based Descriptor And How To Use It In Python | Digital Image Processing

Comparison

Homogeneity Value of Image c is the highest as we have more similar patterns in this image as compared to others.

Dissimilarity and Contrast of Image b has more value than other 2 images showing that the pixels in this image have differ more to each other.

Energy and ASM value for Image a more which means more uniformity i.e the texture is more smooth than other images.

Correlation value for Image b is minimum due to more randomness as compared to other 2 images.

 

 


No comments:

Post a Comment

Pages