IMAGE PROCESSING

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
Next Post Home
Google ping Hypersmash.com