Search code examples
pythonnumpycomputer-visiontexturesmahotas

Haralick, how does Mahotas calculate correlation?


I'm currently comparing two implementations of Haralick texture feature calculators, one implemented in Python (Mahotas) and the other in C++. Currently trying to find out why some features return the same result, whereas others don't – one such being correlation.

Could somebody explain how ux = np.dot(px, k) in texture.py equates to the mean of the marginal-probability matrix?

https://github.com/luispedro/mahotas

The relevant code to see how Mahotas computes ux is:

# cmat is the square co-ocurrence matrix
T = cmat.sum()
maxv = len(cmat)
k = np.arange(maxv)
p = cmat / float(T)
px = p.sum(0)
ux = np.dot(px, k)

Solution

  • Given that px is the marginal probability distribution (it sums to 1), then its 1st order moment is equal to its 1st order normalized moment. The 1st order normalized moment of any distribution corresponds to the mean of the distribution (see the Wikipedia page on moments to learn more).

    np.dot(px, k) is the same as np.sum(px * k), which is a literal translation of the equation for the 1st order moment.