Postermaestro
Postermaestro

Reputation: 85

Calculating entropy from co-occurence matrix in Matlab

I am trying to extract the entropy from co-occurence matrices with zero entries in Matlab. From the definition of entropy of a co-occurence matrix:

definition of entropy
has to be calculated, where cij stands for the (i,j) entry of the co-occurence matrix. Thus it seems to me that if there is a single zero entry, the entropy will be undefined. Do you set some sort of lower limit to log(x) when x = 0, or how do you deal with it?

Link to a pdf with the definition of entropy for the GLCM: http://www.code.ucsd.edu/pcosman/glcm.pdf

EDIT: Thanks for the suggestions on how to deal with log(0), but the equation actually calls for evaluating 0*log(0) which is 0 anyway. It would have been easier to explain if I could use formulas, but maybe my question was more mathematical anyway, and thus on the wrong forum.

Upvotes: 4

Views: 1346

Answers (2)

Tonechas
Tonechas

Reputation: 13743

I generally use the following workaround to avoid this issue:

X = C .* log2(C + (C == 0));
entropy = -sum(X(:));

For those entries of C (the co-occurrence matrix) that are 0, the argument of the logarithm function is 1 since the expression (C == 0) is evaluated as 1.

Upvotes: 2

Hunter Jiang
Hunter Jiang

Reputation: 1300

I always do this if I don't want a -Inf when I log something.

set an epsilon which is very, very little and deal your matrix C like

e = 1e-99;
C = C + e;

then you could run your old code and the answer will not be -Inf.

Thank for @CrisLuengo's useful advice in comment

Upvotes: 4

Related Questions