luispedro / mahotas

Computer Vision in Python
https://mahotas.rtfd.io
Other
843 stars 148 forks source link

Inconsistency with paper in Haralick Feature Computation #106

Closed databaaz closed 3 years ago

databaaz commented 5 years ago

Please consider the following code used for computation of 3rd haralick feature: feats[2] = (1. / sx / sy) * (np.dot(ij.ravel(), pravel) - ux * uy)

Assuming the variables used in the code are consistent with the ones used in the paper (given that the formula used here is same as the one used in paper), the variables ux and uy are supposed to store means of px and py respectively.
While the computations of px & py are consistent with paper (sums across axis 0 and 1), there is a difference in computation of ux and uy.
You have computed ux as: ux = np.dot(px, k) which doesn't represent the mean.
It is a dot product of px and its indices:
px[0]*0 + px[1]*1 + px[2]*2 +....+px[n-1]*(n-1) Could you please comment if this is a bug or you've done things differently purposely ?

luispedro commented 5 years ago

I could be wrong since I have not looked at this code in a long time, but isn't px[i] the frequency of pixels with value i? In that case, np.dot(px, k) would indeed correspond to the mean, no?

databaaz commented 5 years ago

No px[i] doesn't represent the frequency of pixels with value i.It is supposed to represent the sum of occurrences of pixel i in adjacent positions to other pixels. And ux is supposed to represent mean of px not the pixel values.

luispedro commented 5 years ago

OK. I remember this issue (and you're right, it's slightly different from the average across all pixels)

It's an explicit choice. I think the original paper is ambiguous in writing µ is the mean of p. I think the interpretation in the code, µ is the mean of i under the distribution p[i], is the most reasonable, but other packages have interpreted it as µ is the mean of the values of p[i]. Note how the equation for feature 4, variance, explicitly computes i - µ, which makes perfect sense if you interpret µ as an average pixel value (and is, in fact, the variance of this distribution), but is a weird measure if you interpret it as average of the px array (also, equation 3).