def fitEllipse(data):
    """
    Returns the length of the long and short axis and the angle measure
    of the long axis to the horizontal of the best fit ellipsebased on
    image moments.
    usage: longAxis, shortAxis, angle = fitEllipse(N_by_M_image_as_array)
    """
m = moments(data, 2)
xc = m[1, 0] / m[0, 0]
yc = m[0, 1] / m[0, 0]
a = m[2, 0] / m[0, 0] - xc ** 2
b = 2 * (m[1, 1] / m[0, 0] - xc * yc)
c = m[0, 2] / m[0, 0] - yc ** 2
theta = 0.5 * np.arctan2(b, a - c)
w = np.sqrt(6 * (a + c - np.sqrt(b ** 2 + (a - c) ** 2)))
l = np.sqrt(6 * (a + c + np.sqrt(b ** 2 + (a - c) ** 2)))
return l, w, theta
Counting pixels within an Ellipse from a numpy array