Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Texture Measures from GLCM – MATLAB CODE


We have seen how to generate the GLCM matrix and as a continuation, we will focus on the different measures that can be obtained from the GLCM matrix. These measures are broadly classified into three different groups.

Contrast Group:
  • Contrast (Con)
  • Dissimilarity (Diss)
  • Homogeneity (Hom)

Orderliness Group:
  • Angular Second Moment (Asm)
  • Maximum Probability (Max)
  • Entropy (Entropy)

Statistics Group:
  • GLCM Mean
  • GLCM Variance
  • GLCM Correlation

GLCM matrix contains the number of occurrences of two grey levels and let’s perform normalization to measure the probability of occurrence of these two pixels.

NORMALIZATION:
Divide the GLCM matrix by total counts of the pixel occurrences. Sum of the values in GLCM matrix = 20

MATLAB CODE:

GProb = glcm./sum(glcm(:));



















MATLAB CODE:

%INITIALIZE THE VARIABLES
Con=0;
Diss=0;
Hom=0;
Asm=0;
Meanx=0;
Meany=0;
Varx=0;
Vary=0;
GLCMCorrelation=0;

%NORMALIZATION
GProb = glcm./sum(glcm(:));

for i = 1:size(glcm,1)
    for j = 1:size(glcm,2)
        ij = (i-j);
        Con = Con +(GProb(i,j).*(ij).^2);
        Diss = Diss +(GProb(i,j).*abs(ij));
        Hom = Hom +(GProb(i,j)/(1 + ij * ij));
        Asm = Asm+(GProb(i,j).^2);
        Meanx = Meanx + (GProb(i,j)*i);
        Meany = Meany + (GProb(i,j)*j);
       
    end
end

Energy = sqrt(Asm);
MaxGLCM = max(GProb(:));
Entropy = sum(-GProb .* log10(GProb+0.00001));

CG=sprintf("Contrast Group:\nContrast:%f\nDissimilarity:%f\nHomogeneity:%f\n",Con,Diss,Hom);
OG=sprintf("Orderliness Group:\nAngular Second Moment :%f\nMax Probability:%f\nEntropy:%f\nEnergy:%f\n",Asm,MaxGLCM,Entropy,Energy);

display(CG);
display(OG);



MATLAB CODE:

GLCMMean = (Meanx+Meany)/2;


%COMPUTE VARIANCE
for i = 1:size(glcm,1)
    for j = 1:size(glcm,2)
        Varx = Varx+(GProb(i,j)*(i-Meanx).^2);
        Vary = Vary+(GProb(i,j)*(j-Meany).^2);
       
    end
end

GLCMVar = (Varx+Vary)/2;


%COMPUTE CORRELATION
for i = 1:size(glcm,1)
    for j = 1:size(glcm,2)
        GLCMCorrelation = GLCMCorrelation +(GProb(i,j)*(i-Meanx)*(j-Meany)/sqrt(Varx*Vary));
    end
end



SG=sprintf("Statistics Group:\nGLCM Mean:%f\nGLCM Variance:%f\nGLCM Correlation:%f\n",GLCMMean,GLCMVar,GLCMCorrelation);

display(SG); 



EXPLANATION:
The above listed measures are computed on the normalized GLCM matrix.  

MATLAB CODE:
A = imread('cameraman.tif');

%OFFSET AND DIRECTION
Displacementx = -1;
Displacementy = 1;


  
As = (A-min(A(:)))+1;

NumQuantLevels = max(As(:));
glcm = zeros([NumQuantLevels,NumQuantLevels]);

if ( Displacementx < 0 ) 
    sx=abs(Displacementx)+1;
    ex=size(As,1);
else
    sx=1;
    ex=size(As,1)-Displacementx;
end

if ( Displacementy < 0 ) 
    sy=abs(Displacementy)+1;
    ey=size(As,2);
else
    sy=1;
    ey =size(As,2)-Displacementy;
end
for i=sx:ex
    for j=sy:ey
        glcm(As(i,j),As(i+(1*Displacementx),j+(1*Displacementy)))=glcm(As(i,j),As(i+(1*Displacementx),j+(1*Displacementy)))+1;
    end
end



GLCMProb = glcm./sum(glcm(:));
[jj,ii]=meshgrid(1:size(glcm,1),1:size(glcm,2));
ij=ii-jj;

Con = sum(sum(GLCMProb.*(ij).^2));
Diss = sum(sum(GLCMProb.*abs(ij)));
Hom = sum(sum(GLCMProb./(1+(ij).^2)));
Asm = sum(sum(GLCMProb.^2));     
Meanx = sum(sum(GLCMProb.*ii)); 
Meany = sum(sum(GLCMProb.*jj));        

Energy = sqrt(Asm);
MaxGLCM = max(GLCMProb(:));
Entropy = sum(sum(-GLCMProb .* log10(GLCMProb+0.00001)));
GLCMMean = (Meanx+Meany)/2;

Varx = sum(sum(GLCMProb.*(ii-Meanx).^2));
Vary = sum(sum(GLCMProb.*(jj-Meany).^2));

GLCMVar = (Varx+Vary)/2;

GLCMCorrelation = sum(sum(GLCMProb.*(ii-Meanx).*(jj-Meany)/sqrt(Varx*Vary)));


CG=sprintf("Contrast Group:\nContrast:%f\nDissimilarity:%f\nHomogeneity:%f\n",Con,Diss,Hom);
OG=sprintf("Orderliness Group:\nAngular Second Moment :%f\nMax Probability:%f\nEntropy:%f\nEnergy:%f\n",Asm,MaxGLCM,Entropy,Energy);
SG=sprintf("Statistics Group:\nGLCM Mean:%f\nGLCM Variance:%f\nGLCM Correlation:%f\n",GLCMMean,GLCMVar,GLCMCorrelation);
display(CG);
display(OG);
display(SG);




EXPLANATION:
The above code works also for the diagonal directions.  In this example, the diagonal direction 45 is considered with displacement of 1 pixel. The minimum value of the chosen example image, ‘cameraman.tif’ is 7 and maximum is 253 and it is scaled between 1 and 247. The quantization level is chosen as 247 so the GLCM matrix will be of size 247x247.  

NOTE:
·       Some of the measures obtained will be different from the computing using ‘graycoprops’ in MATLAB due to some difference in the equations adopted. For more reference, cross check with the MATLAB documentation

·       The input image or matrix is scaled between 1 and the maximum pixel value of the image. And the Quantization level is set based on it.

like button Like "IMAGE PROCESSING" page

Grey Level Co-occurrence Matrix (GLCM) in MATLAB


Let’s see in these series of posts on how to extract the texture features from Grey Level Co-occurrence Matrix (GLCM) in MATLAB.  In simple terms, GLCM gives the spatial relationship between adjacent or neighbouring pixels. And from this GLCM Matrix, we will measure some texture features.

Let’s consider a simple example and start coding the steps in terms of algorithmic as well as programming in MATLAB.

Procedure to Generate GLCM Matrix:








MATLAB CODE:

clear all
%GLCM MATRIX

A=[1 2 3 2 3; 1 2 3 4 5; 4 4 1 1 4; 6 7 6 7 6; 1 8 1 8 8];

Displacement = 1; %NUMBER OF STEPS BETWEEN TWO PIXELS

NumQuantLevels = 8; %QUANTIZATION LEVEL
glcm = zeros([NumQuantLevels,NumQuantLevels]); %PREALLOCATE GLCM MATRIX

for i = 1:size(A,1)
    for j = 1 :size(A,2)-1
        glcm(A(i,j),A(i,j+1))=glcm(A(i,j),A(i,j+1))+1; %INCREMENT BY 1
    end

end


EXPLANATION:
The number of occurrences of a pixel and its neighboring pixel is calculated. The offset is 1 and it can be incremented based on the requirement. Here the direction of the pixel movement is towards the right. The maximum value of the pixel in the matrix A is 8 so the quantization level is fixed to 8. For instance, in the example given above, the pixel 2 and its neighbor 3 occurs 3 times in matrix A and it is updated in the GLCM matrix at the position (2,3) as 3.


Continue Reading: Texture Measures from GLCM - MATLAB CODE



like button Like "IMAGE PROCESSING" page
Next Post Home
Google ping Hypersmash.com