Code covered by the BSD License  

Highlights from
GLCM texture features

3.875

3.9 | 8 ratings Rate this file 174 Downloads (last 30 days) File Size: 6.14 KB File ID: #22187

GLCM texture features

by

 

20 Nov 2008 (Updated )

Calculates texture features from the input GLCMs

| Watch this File

File Information
Description

The GLCMs are stored in a i x j x n matrix, where n is the number of GLCMs calculated usually due to the different orientation and displacements used in the algorithm. Usually the values i and j are equal to 'NumLevels' parameter of the GLCM computing function graycomatrix(). Note that matlab quantization values belong to the set {1,..., NumLevels} and not from {0,...,(NumLevels-1)} as provided in some references
http://www.mathworks.com/help/images/ref/graycomatrix.html

Although there is a function graycoprops() in Matlab Image Processing Toolbox that computes four parameters Contrast, Correlation, Energy, and Homogeneity. The paper by Haralick suggests a few more parameters that are also computed here. The code is not vectorized and hence is not an efficient implementation but it is easy to add new features based on the GLCM using this code. The code takes care of 3 dimensional glcms (multiple glcms in a single 3D array)

If you find that the values obtained are different from what you expect or if you think there is a different formula that needs to be used from the ones used in this code please let me know. A few questions which I have are listed in the link http://www.mathworks.com/matlabcentral/newsreader/view_thread/239608

I plan to submit a vectorized version of the code later and provide updates based on replies to the above link and this initial code.

% Features computed
% Autocorrelation: [2] (out.autoc)
% Contrast: matlab/[1,2] (out.contr)
% Correlation: matlab (out.corrm)
% Correlation: [1,2] (out.corrp)
% Cluster Prominence: [2] (out.cprom)
% Cluster Shade: [2] (out.cshad)
% Dissimilarity: [2] (out.dissi)
% Energy: matlab / [1,2] (out.energ)
% Entropy: [2] (out.entro)
% Homogeneity: matlab (out.homom)
% Homogeneity: [2] (out.homop)
% Maximum probability: [2] (out.maxpr)
% Sum of sqaures: Variance [1] (out.sosvh)
% Sum average [1] (out.savgh)
% Sum variance [1] (out.svarh)
% Sum entropy [1] (out.senth)
% Difference variance [1] (out.dvarh)
% Difference entropy [1] (out.denth)
% Information measure of correlation1 [1] (out.inf1h)
% Informaiton measure of correlation2 [1] (out.inf2h)
% Inverse difference (INV) is homom [3] (out.homom)
% Inverse difference normalized (INN) [3] (out.indnc)
% Inverse difference moment normalized [3](out.idmnc)

Haralick uses 'Symmetric' = true in computing the glcm. There is no Symmetric flag in the Matlab version I use hence I add the diagonally opposite pairs to obtain the Haralick glcm. Here it is assumed that the diagonally opposite orientations are paired one after the other in the matrix. If the above assumption is true with respect to the input glcm then setting the flag 'pairs' to 1 will compute the final glcms that would result by setting 'Symmetric' to true. If your glcm is computed using the
Matlab version with 'Symmetric' flag you can set the flag 'pairs' to 0

% References:
1. R. M. Haralick, K. Shanmugam, and I. Dinstein, Textural Features of Image Classification, IEEE Transactions on Systems, Man and Cybernetics, vol. SMC-3, no. 6, Nov. 1973
2. L. Soh and C. Tsatsoulis, Texture Analysis of SAR Sea Ice Imagery Using Gray Level Co-Occurrence Matrices, IEEE Transactions on Geoscience and Remote Sensing, vol. 37, no. 2, March 1999.
3. D A. Clausi, An analysis of co-occurrence texture statistics as a
function of grey level quantization, Can. J. Remote Sensing, vol. 28, no.1, pp. 45-62, 2002
4. http://murphylab.web.cmu.edu/publications/boland/boland_node26.html

% Example:
% Usage is similar to graycoprops() but needs extra parameter 'pairs' apart from the GLCM as input

>I = imread('circuit.tif');
>GLCM2 = graycomatrix(I,'Offset',[2 0;0 2]);
>stats = GLCM_features1(GLCM2,0)

The output is a structure containing all the features calculated from the different GLCMs

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (44)
27 Apr 2014 ali Hasan

Thank you for this codes but I have a question about Inverse Difference Normalized and Inverse Difference Moment Normalized, could someone tell me in which reference are mentioned these two equations?

17 Mar 2014 Shaila parvin

This code shows me the error:

>> GLCM_Features1
Error using GLCM_Features1 (line 118)
Too many or too few input arguments. Enter
GLCM and pairs.

>>

How to solve it?

I like it.

01 Apr 2013 subbmdee

Hi,
I am trying to extract these features for different region of interests(ROIs) of size 32x32 from different images, but I am getting same(wrong) value for particular feature. If I extract these features for the whole image, then the values are different(right) for different images.
Please help
thanks

16 Jan 2013 jenifer

please send me the matlab codes for glcm

03 Nov 2012 Karthik

@avinash & @ will,
Hi,

I used your code and got set of Features.
But the thing is I have applied for a lung image which contains nodules.
So when i execute this program and get result i want to know whether this output is for a NODULE which i want....
(i.e)Instead of applying for an entire image, i want it for a PARTICULAR PART OF AN IMAGE WHICH I WANT..
Hope i ve made my point clear.
So any suggestions or ideas would be of great help.

Thank you,
karthik.

03 Nov 2012 Karthik

Hi Avinash,

Actually am working in lung nodule detection.
I ve to extract the GLCM features of the lung nodule.
So how to apply your code for my nodule in a image. What input should i give.
How should i give input for your code & do i need to change anything if i need to apply it for a image.

Thank you,
karthik.

29 Aug 2012 Preeti

i am not able to send the computed GLCM to this vectorised version by Will , it is not accepting the double input. Please help!

14 May 2012 jassu kumar

plz give me code 4 feature extracion of glcm

17 Apr 2012 Avinash Uppuluri

Hi Stephanie,

The number of sets of outputs will depend on the number of GLCMs input into the function for both graycoprops() and GLCM_features().

In using GLCM_features() please note the function of the 'pairs' flag as explained in the intro. Also make sure how your version of graycomatrix() works with regards to the 'Symmetric' flag.

"Haralick uses 'Symmetric' = true in computing the glcm. There is no Symmetric flag in the Matlab version I use hence I add the diagonally opposite pairs to obtain the Haralick glcm. Here it is assumed that the diagonally opposite orientations are paired one after the other in the matrix. If the above assumption is true with respect to the input glcm then setting the flag 'pairs' to 1 will compute the final glcms that would result by setting 'Symmetric' to true. If your glcm is computed using the
Matlab version with 'Symmetric' flag you can set the flag 'pairs' to 0".

Please do get back if there is still an issue.

Thanks

16 Apr 2012 Stephanie

Hi,

I'm a little confused as to why the output text has 2 numbers per function when applied to a single image. I read through the intro but missed it. When I do just graycoprops(), the outputs are single numbers. Could you explain why there are two number for each output and why they vary from each other? Thanks!

11 Apr 2012 erma emira

hi,
i want to use glcm for crown segmentation but i got this error. n i'm very new to matlab and programmg.can anyone help?

??? Maximum recursion limit of 500 reached. Use
set(0,'RecursionLimit',N)
to change the limit.

need a lot of guidance.

29 Mar 2012 Avinash Uppuluri

Hi Ujwala,

"I am working on MRI of brain for my Ph.D. now I am using your GLCM_Features4 program for feature extraction, but I can not understant this GLCM_Features4 program calculate 22 feature or 44 feature."

The output is a structure called 'out' which has 22 features for each of the GLCMs that are input. The function takes multiple GLCMs as input. Please go through the description for the program and let me know if further explanation is needed.

"when I was passing MRI img to this it will give 2 values that belong to 22 feature or 44. like contrast = 22.22 33.33. tell me this is 2 diff feature or same only for contrast."

Please note that the function does NOT take images as input rather it takes GLCMS ( calculated using a function like graycomatrix() ) as input. And depending on the number of GLCMs there are so main sets of 22 features. So for n GLCMs you will have n sets of 22 features as output.

Hope that helps.

28 Mar 2012 Avinash Uppuluri

Hi Gomathi,

"to this GLCM program, I gave the tumor segmented image as input. Was I correct?"

Please make sure that you are giving the GLCM(s) as input to the GLCM_Features function. ( You can use help graycomatrix to know more on how to input your image into this matlab function ).

Please go through the initial description on this page and in the code and if there is still a problem please do get back.

Thanks!

22 Mar 2012 Gomathi C

Hi Avinash
I'm doing a project in liver tumor classification. I used your program and it gave some output. I don't know whether I'm correct. Actually I initially used Region Growing method for liver segmentation and from that I segmented tumor using FCM. So, to this GLCM program, I gave the tumor segmented image as input. Was I correct? If so, I think, then, my output will also be correct. I gave the parameters exactly as in your example. Actually what do they mean? Do I need to change them for different images? If so, how to give the parameters? I'm completely new to this. So, kindly guide me.

I got this output. Am I correct?

stats =

autoc: [1.857855266614132e+000 1.857955341199538e+000]
contr: [5.103143332457753e-002 5.030548650257343e-002]
corrm: [9.512661919561399e-001 9.519459060378332e-001]
corrp: [9.512661919561385e-001 9.519459060378338e-001]
cprom: [7.885631654779597e+001 7.905268525471267e+001]
cshad: [1.219440700252286e+001 1.220659371449108e+001]
dissi: [2.037387269065756e-002 1.935418927908687e-002]
energ: [8.987753042491253e-001 8.988459843719526e-001]
entro: [2.759187341212805e-001 2.743152140681436e-001]
homom: [9.930016927881388e-001 9.935307908219834e-001]
homop: [9.925660617240367e-001 9.930960070222014e-001]
maxpr: [9.474275457490587e-001 9.474466930429607e-001]
sosvh: [1.847174384255155e+000 1.846913030238459e+000]
savgh: [2.332207337361002e+000 2.332108469591401e+000]
svarh: [6.311174784234007e+000 6.314794324825067e+000]
senth: [2.663144677055123e-001 2.653725436772341e-001]
dvarh: [5.103143332457753e-002 5.030548650257344e-002]
denth: [7.573115918713391e-002 7.073380266499811e-002]
inf1h: [-8.199645492654247e-001 -8.265514568489666e-001]
inf2h: [5.643539051044213e-001 5.661543271625117e-001]
indnc: [9.980238521073823e-001 9.981394883569174e-001]
idmnc: [9.993275086521848e-001 9.993404634013308e-001]

Awaiting your reply.

21 Mar 2012 aditi killedar

thanks....

02 Feb 2012 giet  
12 Dec 2011 ram m

Hi all

Can you explain the physical interpretation of the Inverse difference moment , Information measures of correlation , cluster prominence and shade in a image.
I was trying to get the physical meaning of them , but could not find properly.

Thanks

23 Jul 2011 Avinash Uppuluri

Will, Thanks for the update.

22 Jul 2011 Will

function [out] = cad_glcm_features(glcm)

% VECTORIZED CODE: FASTER

size_glcm_1 = size(glcm,1);
size_glcm_2 = size(glcm,2);
size_glcm_3 = size(glcm,3);

% checked
out.autoc = zeros(1,size_glcm_3); % Autocorrelation: [2]
out.contr = zeros(1,size_glcm_3); % Contrast: matlab/[1,2]
out.corrm = zeros(1,size_glcm_3); % Correlation: matlab
out.corrp = zeros(1,size_glcm_3); % Correlation: [1,2]
out.cprom = zeros(1,size_glcm_3); % Cluster Prominence: [2]
out.cshad = zeros(1,size_glcm_3); % Cluster Shade: [2]
out.dissi = zeros(1,size_glcm_3); % Dissimilarity: [2]
out.energ = zeros(1,size_glcm_3); % Energy: matlab / [1,2]
out.entro = zeros(1,size_glcm_3); % Entropy: [2]
out.homom = zeros(1,size_glcm_3); % Homogeneity: matlab
out.homop = zeros(1,size_glcm_3); % Homogeneity: [2]
out.maxpr = zeros(1,size_glcm_3); % Maximum probability: [2]
out.sosvh = zeros(1,size_glcm_3); % Sum of sqaures: Variance [1]
out.savgh = zeros(1,size_glcm_3); % Sum average [1]
out.svarh = zeros(1,size_glcm_3); % Sum variance [1]
out.senth = zeros(1,size_glcm_3); % Sum entropy [1]
out.dvarh = zeros(1,size_glcm_3); % Difference variance [4]
out.denth = zeros(1,size_glcm_3); % Difference entropy [1]
out.inf1h = zeros(1,size_glcm_3); % Information measure of correlation1 [1]
out.inf2h = zeros(1,size_glcm_3); % Informaiton measure of correlation2 [1]
out.indnc = zeros(1,size_glcm_3); % Inverse difference normalized (INN) [3]
out.idmnc = zeros(1,size_glcm_3); % Inverse difference moment normalized [3]

% Indices
[i,j] = meshgrid(1:size_glcm_1,1:size_glcm_2);
idx1 = (i+j)-1;
idx2 = abs(i-j)+1;
ii = (1:(2*size_glcm_1-1))';
jj = (0:size_glcm_1-1)';

for k = 1:size_glcm_3 % number glcms
% Normalize GLCM
glcm_sum = sum(sum(glcm(:,:,k)));
Pij = glcm(:,:,k)./glcm_sum; % Normalize each glcm
glcm_mean = mean(Pij(:)); % compute mean after norm
%
p_x = squeeze(sum(Pij,2));
p_y = squeeze(sum(Pij,1))';
%
u_x = sum(sum(i.*Pij));
u_y = sum(sum(j.*Pij));
%
p_xplusy = zeros((2*size_glcm_1 - 1),1); %[1]
p_xminusy = zeros((size_glcm_1),1); %[1]
for aux = 1:max(idx1(:))
p_xplusy(aux) = sum(Pij(idx1==aux));
end
for aux = 1:max(idx2(:))
p_xminusy(aux) = sum(Pij(idx2==aux));
end

% Contrast
out.contr(k) = sum(sum((abs(i-j).^2).*Pij));
% Dissimilarity
out.dissi(k) = sum(sum(abs(i-j).*Pij));
% Energy
out.energ(k) = sum(sum(Pij.^2));
% Entropy
out.entro(k) = -sum(sum(Pij.*log(Pij+eps)));
% Homogeneity Matlab
out.homom(k) = sum(sum(Pij./(1+abs(i-j))));
% Homogeneity Paper
out.homop(k) = sum(sum(Pij./(1+abs(i-j).^2)));
% Sum of squares: Variance
out.sosvh(k) = sum(sum(Pij.*((j-glcm_mean).^2)));
% Inverse difference normalized
out.indnc(k) = sum(sum(Pij./(1+(abs(i-j)./size_glcm_1))));
% Inverse difference moment normalized
out.idmnc(k) = sum(sum(Pij./(1+((i-j)./size_glcm_1).^2)));
% Maximum probability
out.maxpr(k) = max(Pij(:));
% Sum average
out.savgh(k) = sum((ii+1).*p_xplusy);
% Sum entropy
out.senth(k) = -sum(p_xplusy.*log(p_xplusy+eps));
% Sum variance
out.svarh(k) = sum((((ii+1) - out.senth(k)).^2).*p_xplusy);
% Difference entropy
out.denth(k) = -sum(p_xminusy.*log(p_xminusy+eps));
% Difference variance
out.dvarh(k) = sum((jj.^2).*p_xminusy);
% Computes correlation
hxy1 = -sum(sum(Pij.*log(p_x*p_y' + eps)));
hxy2 = -sum(sum((p_x*p_y').*log(p_x*p_y' + eps)));
hx = -sum(p_x.*log(p_x+eps));
hy = -sum(p_y.*log(p_y+eps));
hxy = out.entro(k);
% Information measure of correlation 1
out.inf1h(k) = (hxy-hxy1)/(max([hx,hy]));
% Information measure of correlation 2
out.inf2h(k) = (1-exp(-2*(hxy2-hxy)))^0.5;
% Cluster Prominence
out.cprom(k) = sum(sum(Pij.*((i+j-u_x-u_y).^4)));
% Cluster Shade
out.cshad(k) = sum(sum(Pij.*((i+j-u_x-u_y).^3)));
%
s_x = sum(sum(Pij.*((i-u_x).^2)))^0.5;
s_y = sum(sum(Pij.*((j-u_y).^2)))^0.5;
corp = sum(sum(Pij.*(i.*j)));
corm = sum(sum(Pij.*(i-u_x).*(j-u_y)));
% Autocorrelation
out.autoc(k) = corp;
% Correlation paper
out.corrp(k) = (corp-u_x*u_y)/(s_x*s_y);
% Correlation Matlab
out.corrm(k) = corm/(s_x*s_y);
end

18 Jul 2011 leila  
04 May 2011 Hervé Chubaka Bagalwa

Thank you. I have not checked if the output values are correct, but I'm just going to go and plug it in my classifier.

I will have more comments for you when i'm done with my project.

Thanks again

18 Apr 2011 adzlan

hi Avinash

im new in matlab. can you tell me how can i call only this 13 features from your code
• Angular Second Moment
• Contrast
• Correlation
• Variance
• Inverse Second Differential Moment
• Sum Average
• Sum Variance
• Sum Entropy
• Entropy
• Difference Variance
• Difference Entropy
• Measure of Correlation 1
• Measure of Correlation 2

thanks,
adzlan

29 Mar 2011 Avinash Uppuluri

Hi bala,

Please try to explain further. From what I understand it looks like you want to know how to generate the E I S H for each image in your database and use the same parameters of the input image to find the closest ( euclidean ) image to it in the database.

You can generate the GLCM for each image using the in built matlab function graycomatrix() and then pass the generated GLCM to the function you downloaded here GLCM_featuresX()

Hope that helps,
Avinash

29 Mar 2011 Reem Md

Hi Avinash
Good job ;)
Can you please help me, I'm trying to move a window, say 9X9 on a matrix, and create for every region (9X9) the glcm, and then move this 9X9 window until I reach the end of the matrix. How can I do that?

29 Mar 2011 Reem Md  
16 Mar 2011 bala k

avinash uppuluri,

i'm doing image processing using GLCM function energy, contrast, entropy, inverse matrix, can you give how i generate for these function code.
my process is
query_image = imread('145.jpg');
grayimage = rgb2gray(query_image);
i need to calculate gray level co-occurrence matrix,
energy E,
entropy I,
contrast S,
inverse difference H,
F_query = [E I S H];

image database also used above process.

F_data = [E1 I1 S1 H1];

euclidean distance

d = norm(F_query - F_data);

i will get the best match images.

thanks,
bala

25 Feb 2011 Avinash Uppuluri

Kapil,

I would appreciate if you can leave a short comment on how you think this work can be improved so that I can make it more useful for yourself and others.

Thanks,
Avinash

25 Feb 2011 Avinash Uppuluri

Hi Kavitha,

Can you please provide more information on how you generate your GLCM matrix (if possible also provide the image used) so that I can reproduce the error on my end.

Thanks,
Avinash

Hi
I tried the above code and got an error stating "Function undefined for input of type double".
What type should i convert my GLCM matrix for this function to work ?
Thanks for the help.

Kavitha

14 Feb 2011 Kavitha Ravi

Hi
I tried the above code and got an error stating "Function undefined for input of type double".
What type should i convert my GLCM matrix for this function to work ?
Thanks for the help.

Kavitha

10 Jan 2011 kapil veera  
12 Apr 2010 Avinash Uppuluri

There have been a few questions on how to compute features for each pixel in a image. The link below provides further explanation into such details. See "Creating a texture image"

http://www.fp.ucalgary.ca/mhallbey/texture_calculations.htm

Main points being:
+ Texture for a pixel needs the definition of a "window" around the pixel
+ Once such a window is defined; you can calculate the GLCM for each pixel (taking the respective window into consideration);
+ These GLCMs for each pixel should later be fed into the GLCM_features function to extract the feature vector for each pixel (and related window)
+ If needed, the values can later be put together to form a texture feature image

Hope this helps.

10 Apr 2010 Avinash Uppuluri

http://www.mathworks.com/matlabcentral/fileexchange/22354-glcmfeatures4-m-vectorized-version-of-glcmfeatures1-m-with-code-changes

The above link has a faster version of this code (named GLCM_features4.m). They should give the same outputs. Do let me know if you find any difference in the outputs.

Thanks,
Avinash

29 Nov 2009 gasmi karim

I work on the MRI image but I do not know how I can use to calculate your classification is for the I told you that the feature is calculated for each pixel I await your response

29 Nov 2009 gasmi karim

Please I want known is that in calculating the Haralick feature of such energy to the entire image or for each pixel because in your code I think you calculate for the whole image a single value for each feature so how it can be used for classification is for this I see the need to calculate for each pixel the feature

13 Oct 2009 Preeti

Thanks for reply. Actually i am not able to load the data from mat file. can you please help me in using the data in .mat file? what is the code to import data from .mat file one by one , then only i can form GLCM for every dicom image. Please also tell that how to save these features in a database

Looking forward to your reply.

12 Oct 2009 Avinash Uppuluri

Hi Preeti,

Try something like this:
% Form GLCMs for each of the 354 images using the required offsets (and other parameters as required) using the MATLAB function graycomatrix()

e.g., GLCM2 = graycomatrix(Dicom_images(:,:,1),'Offset',[0 2]);

% Then compute the features for each GLCM/image. You can also have multiple GLCMs per image depending on the parameters you input to graycomatrix().

stats = GLCM_features1(GLCM2,0)

Hope this helps,
Avinash

Hi
I have read your code GLCM texture features. I have a mat file of size 512*512*354. in which 354 DICOM images are saved.
I want to use your code to extract texture features of all images in .mat file. please help.
Preeti

29 Jul 2009 Avinash Uppuluri

Jonathan,
You are right! Haralick coefficients can be computed on GLCMs calculated in different directions. The matlab function graycomatrix() gives option to change this direction as required. http://www.mathworks.com/access/helpdesk/help/toolbox/images/graycomatrix.html

A good read for this topic: Image Processing: Dealing with texture by Maria Petrou and Pedro Garcia Sevilla. Let me know if you need any specifics about changing the orientation. Thanks.

29 Jul 2009 Jonathan Pedron

Good job ;)
It works perfectly well.
I am not an expert on Haralick coefficient but it can be done in different direction, am I right?
If it is right would be interesting to be able to set this parameter...

16 Apr 2009 Bilwaj Gaonkar  
29 Dec 2008 Avinash Uppuluri

It looks like the version of Image Processing Toolbox of matlab that you are working with does not have graycomatrix().

You can write a function to compute the GLCM for a image using the details provided in http://www.mathworks.com/access/helpdesk/help/toolbox/images/graycomatrix.html and the references listed in the description.

29 Dec 2008 matlab res

>> help graycomatrix()

graycomatrix().m not found.

I get this error . What should i do?

23 Dec 2008 Avinash Uppuluri

>I = imread('circuit.tif');
>GLCM2 = graycomatrix(I,'Offset',[2 0;0 2]);
>stats = GLCM_features1(GLCM2,0)

The above code should work fine if you have Matlab Image Processing Toolbox with the function graycomatrix() to compute the Gray Level Cooccurrence Matrix (GLCM). [ Try > help graycomatrix and see what if the function is present in your version]

If you are using any other method to compute the GLCM (e.g., a self written function) you can use GLCM_Features1() with the first input as the GLCM (i x j x n matrix) and a parameter pairs which I use to indicate if a symmetric GLCM needs to be computed from opposite direction pairs. Go through the comments in the code to understand better. Please let me know if you still have a problem.

For the error

"??? Error using ==> GLCM_Features1
Too many or too few input arguments. Enter GLCM and pairs."

Let me know what kind of input parameters you gave the function and I will try to help.

Thanks

23 Dec 2008 matlab res

I get this error . What should be done ?
GLCM2 = graycomatrix(I,'Offset',[2 0;0 2]);
??? Undefined command/function 'graycomatrix'.

23 Dec 2008 matlab res

I am new to this environment.I dont know what parameters should be given & how to give.
I get the following error.Give me the step wise details of how to arrive at the output.

??? Error using ==> GLCM_Features1
Too many or too few input arguments. Enter GLCM and pairs.

Updates
20 Nov 2008

Code change

25 Nov 2008

Change of formula for s_x and s_y to obtain correct value of 'Correlation'

Contact us