I make following Python Code to calculate center and size of Gaussian-like distribution basis of moment method. But, I can\'t make the code to calculate the angle of gaussian.>
As Paul Panzer said, the flaw of your approach is that you look for "weight" and "angle" instead of covariance matrix. The covariance matrix fits perfectly in your approach: just compute one more moment, mixed xy.
The function get_weight
should be replaced with
def get_covariance (mesh, func, dxy):
g_mesh = [mesh[0]-dxy[0], mesh[1]-dxy[1]]
Mxx = moment (g_mesh, func, (2, 0))
Myy = moment (g_mesh, func, (0, 2))
Mxy = moment (g_mesh, func, (1, 1))
return np.array([[Mxx, Mxy], [Mxy, Myy]])
Add one more import,
from scipy.stats import multivariate_normal
for reconstruction purpose. Still using your make_gauss function to create the original PDF, this is how it now gets reconstructed:
s0xy = get_centroid (mesh, fxy0)
w0xy = get_covariance (mesh, fxy0, s0xy)
fxy1 = multivariate_normal.pdf(np.stack(mesh, -1), mean=s0xy, cov=w0xy)
That's it; reconstruction works fine now.
Units on the color bar are not the same, because your make_gauss
formula does not normalize the PDF.