File Exchange

## haralickTextureFeat​ures

version 1.3.1.0 (3.75 KB) by
This function calculates all Haralick Features in an effective way without for-loops.

Updated 15 Oct 2018

View Version History

It only uses matrix products and makes calculating the GLCM fast. In version 1.x are only two foor-loops.
For better legacy you decide with the optional variable xFeature which Haralick Feature you want use.
All Haralick Features were take from the original paper. See (http://haralick.org/journals/TexturalFeatures.pdf) for more information.

### Cite As

Rune Monzel (2020). haralickTextureFeatures (https://www.mathworks.com/matlabcentral/fileexchange/58769-haralicktexturefeatures), MATLAB Central File Exchange. Retrieved .

Basim Khalid

Hi
i am trying to calculate Haralic features so i download this file and just changed the image name , i have got many of errors can anyone help me please
Error using graycomatrix
Expected input number 1, I, to be two-dimensional.

Error in graycomatrix>ParseInputs (line 260)
validateattributes(I,{'logical','numeric'},{'2d','real','nonsparse'}, ...

Error in graycomatrix (line 167)
[I, Offset, NL, GL, makeSymmetric] = ParseInputs(varargin{:});

Error in haralickTextureFeatures (line 46)
glcm = graycomatrix(I, 'offset', [0 1], 'Symmetric', true);

>>

Rune Monzel

please take a look into the original paper of Haralick (http://haralick.org/journals/TexturalFeatures.pdf), page 10 of 12, equation 4) describes the Variance. The equation 4) consists of (i - μ). i describes the row entry. μ describes the mean of the gray level co occurrence matrix.

Thank you for the function, super helpful!
It's already almost 2 years old, but I noticed that you take, for the Variance, a normal mean. Should this not be a row or column mean?

Rune Monzel

Hi Rehan,

their mathematical formulae be found here: https://de.mathworks.com/help/images/ref/graycoprops.html
A good and short description can be found here: https://de.mathworks.com/help/images/texture-analysis-using-the-gray-level-co-occurrence-matrix-glcm.html
If you want more information about correlation and homogenity, you should read the original texture feature paper by Haralick (see link in my source code) and further literature. It is very important to understand the principle of a gray level occurence matrix before starting to understand the Textural Feaeture.

It should be noticed that the haralick features provides features which "measures" a structure, so different structures can be seperated by algorithms like a support vector machine.

Hi Rune
Hope you are fine.
Many thanks for code on GLCM. Can you please send some information about below two features

correlationmatlab (corrm)

homogeneitymatlab (homom)

Please do send info on these two features, like what they signify, their mathematical formulae, and some other relevant information.

Thanks.
Take care.

Rune Monzel

Hi Mobin Mhb,

the problem is in the definition of the Haralick Features "Correlation" and "Information Measures of Correlation I": Calculating those equations with a zero matrix leads to a divison 0 with 0 and is mathematically not allowed. This leads in Matlab to "NaN" ("Not a Number").
It does not look like I have an error in my code. My code just returns those NaNs.

It guess you are using just Haralick Features which are not 0!? Thus you may be add this line after the feature extraction:

x = haralickTextureFeatures(glcm); % feature extraction
x(isnan(x)) = 0; % all NaNs to zero

Mobin Mhb

hi
i need to use haralick for an image , but i have to define 5*5 mask and then use haralickTextureFeature function . but some of 5*5 mask has zero value and i got error "NaN's cannot be converted to logicals." how can i fix this??

Rune Monzel

the function has already an example.
I need further information of the Matlab Error: the Error in Line 278 is not specified.

Example (commented in function):

%Get the co occurence matrix (in Matlab called GLCM: Gray Level Co
%Occurence Matrix)
glcm = graycomatrix(I, 'offset', [0 1], 'Symmetric', true);

%calculate feature 4 (Variance), 5 (Inverse Difference Moment) and 6
%(Sum Average)
xFeatures = 4:6;
x = haralickTextureFeatures(glcm, xFeatures );

%Get only the features you want
x = x( xFeatures )

% Calculate all features
xall = haralickTextureFeatures(glcm)

Error in haralickTextureFeatures (line 278)
for i = 1 : sizecoOcMat(2)
Q(i,:) = sum( ...
(repmat(coOcMat(i,:),sizecoOcMat(1),1) .* coOcMat ) ./ ...
repmat( rowCoOcMat(i) .* colCoOcMat, sizecoOcMat(1),1),...
2,'omitnan');
end
please give an example to clear this error

Rune Monzel

thank you very much for your comment lingxuan kong, it is now fixed

lingxuan kong

hey, your code is awesome, but some mistakes here:
case7:
x(7) = sum( ((2:(2*sizecoOcMat(1)))' -...
x(8)).^2 .* p_XplusY );

should be x(8) not x(12).

Javaid Iqbal

Save feature values in the form of .mat and than save into another format, it's working.

Anjali Acharya

I used this function to extract 14 Glcm features and while I try to copy values from matlab to excel file I get some of the values in the form ##### for variance, sum average, sum variance and entropy
What is wrong with this?

Samar Qassir

Hi Rune

Rune Monzel

Hi Javaid,

use the "save" function from Matlab, use my example and then type in command line:

save( 'haralickFeatures.mat', 'x');

Take a look into the Matlab documentation, it is very helpful:
mathworks.com/help/matlab/ref/save.html

Javaid Iqbal

How can we save these feature values in the .mat file using MATLAB?

nie wei

good resources

Javaid Iqbal

Hi Rune Monzel
Thanks for helping I try this and working good. :)

Rune Monzel

Hi Javaid,

can you send me your code so I can check this? (runemonzel@googlemail.com) For me it seems like you are not using the function correctly and give wrong inputs into the function. Try the example which is also written in the function description, it will work and maybe you will get an understanding of using the function:

%Get the co occurence matrix (in Matlab called GLCM: Gray Level Co
%Occurence Matrix)
glcm = graycomatrix(I, 'offset', [0 1], 'Symmetric', true);

%calculate feature 4 (Variance), 5 (Inverse Difference Moment) and 6
%(Sum Average)
xFeatures = 4:6;
x = haralickTextureFeatures(glcm, xFeatures);

%Get only the features you want
x = x( xFeatures )

Javaid Iqbal

Hanbrot
javaid.ciit@gmail.com

Javaid Iqbal

Not enough input arguments.

Error in haralickTextureFeatures (line 77)
if ~(ismatrix(coOcMat))

77 if ~(ismatrix(coOcMat))

Hanbrot

Actually I had debug it earlier and i find what the problem is. I can send you my code and explain my problem. I couldnot identify why is this happening. either because of the values in the glcm or something else. can I have your email please to connect with you?

Javaid Iqbal

which steps required to run this function in MATLAB please tell me
Thanks

Rune Monzel

There are two possibilities: Either eigenvec is an empty array or eigenvec is a matrix. I do not find any explanation why the variable "eigenvec" is empty or a matrix.

I appreciate it, if you write me a private message with your code or if you would debug the code for yourself and see whats happening so that I can fix this in a newer version.

Hanbrot

In an assignment A(:) = B, the number of elements in A and B must be the same.

Error in haralickTextureFeatures (line 273)
x(14) = sqrt( max(eigenvec) );
I am getting this error while finding harlick features for every pixel in the image.

Rune Monzel

Hi Chinmay Jog,

there may are severel reasons for this behavior.
sum( isnan( inputTexture(:) ) ) > 0
If yes ("1") you should try to fix this (for example setting all NaNs to zero: inputTexture( isnan(inputTexture) ) = 0;).

Also you can check if the following happens (source: https://de.mathworks.com/help/matlab/ref/nan.html):
- Any arithmetic operation on a NaN, such as sqrt(NaN)
- Addition or subtraction, such as magnitude subtraction of infinities as (+Inf)+(-Inf)
- Multiplication, such as 0*Inf
- Division, such as 0/0 and Inf/Inf
- Remainder, such as rem(x,y) where y is zero or x is infinity

I reviewed my code again and I do not think, that the NaNs are results of x3 calculations. If you still have problems, I would appreciate it, if you give me one of your texture images so I can check this.

Chinmay Jog

Hello,
x3 correlation value is coming out NaN for some rough texture images. Why so?

Rune Monzel

@Jihang Wang:
Yes, it is slightly different because of the zero cut out technique: log2(0) leads to -inf, this is the reason why I just use values which are not 0. Another possibillity would be using the smallest number in Matlab: eps, which leads to log2(eps)=-52. However, I decided to cut it out instead of using the smallest number.

@Ihsan Yassin:
The difference between correlation I and correlation II is shown in the Haralick paper: f12 and f13.

@Yuzhen Lu:
Please note that the code fits to Haralick original paper, which I used to program the code. I did not noticed that there is new literature which corrected the error in the paper. Changing the code would be lead to missunderstanding and would change the results of scripts.

Jihang Wang

Yuzhen Lu, I think the code for calculating f4 sum of squares is correct. I have compared the output of this code with other code and it should be Ok. However, the output for entropy looks slightly different, I guess the reason is the zero cut technique using in the code?

Ihsan Yassin

what is information measure of correlation I and II actually?what's the difference?

Yuzhen Lu

It should be pointed out that in calculating f7 (sum variance), the original paper incorrectly use f8 in the definition, which should be f6. This is an error that has been recognized in the literature.

Yuzhen Lu

In calculating f4, the mean value is not well defined the original paper, but it should be not calculated in that way in the code

Yuzhen Lu

works fine with Matlab 2016b

Dominique KATALA

I've found the solution. I was using MATLAB R2016a but when I tried the same function on R2016b it worked. Maybe there are some problems about compatibility. Thanks!

Dominique KATALA

I can't use it as a normal function. When I try, I recieve the parse error message. I can't even try the given example. Thank you!

Rune Monzel

Fixed the problems Ihsan Yassin and Nicolas Yu reported in version 1.0.

Thank you both.

New version is 1.1.

Rune Monzel

Thank you very much for your feedbacks!

I am going to upload a new version 2.0 with the fixes. Please give me time for fixing also the coOcMat size problem (comment 27.12.2016).

Thank you!

Krisztián

Ihsan Yassin

found the solution,I just removed 'if..end' command at line 175 and 178 only.worked fine if you want to observe all features,but if need only x12,that ''if..end''need to be removed.

Ihsan Yassin

Hi, thanks for the code.helped me a lot. Just recently, I need to analyse the features one-by-one.The results seem fine from x1 to x11.but when I picked x12,all of the calculated values are -2.0000 only.but when when I picked all features(x1:14),the values for x12 are different.Why is this happen?

Nicolas Yu

Code is wrong here "Error in haralickTextureFeatures (line 68)
rowMean = sum( row(:).*coOcMat(:) );". The row(:) is [8 8], while coOcMat is [8 8 4]. The dimension is mismatched.

Rune Monzel

Hi Diana,

use this code as a normal function.

Inputs of the function:
% input: coOcMat - Co-Occurence-Matrix. see matlab documentation glcm
% xFeatures (optional) - Feature, which should be
% calculated

example for a function call is:

x = haralickTextureFeatures(glcm);

If you got any errors in the function itselfs please let me now.

Diana Rahim

Hi good source a newbie like me.
Since I'm new in Matlab programming
How can I run this code successfully?

Rune Monzel

There is no value glcmin and pairs.

GLCM is just a graylevelcooccurence matrix. You can also use another channel, this is why I call it coOcMa (co-Occurence-Matrix).

The coOcMa should be calculated with the matlab function graycomatrix option 'symmetric' set to 'true'.

Medical Imaging

what should be the value of glcmin,pairs in this code?

##### MATLAB Release Compatibility
Created with R2006a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux