def center_of_mass(input, labels=None, index=None):
normalizer = sum(input, labels, index)<EOL>grids = numpy.ogrid[[slice(<NUM_LIT:0>, i) for i in input.shape]]<EOL>results = [sum(input * grids[dir].astype(float), labels, index) / normalizer<EOL>for dir in range(input.ndim)]<EOL>if numpy.isscalar(results[<NUM_LIT:0>]):<EOL><INDENT>return tuple(results)<EOL><DEDENT>return [tuple(v) for v in numpy.array(results).T]<EOL>
Calculate the center of mass of the values of an array at labels.

Parameters
----------
input : ndarray
    Data from which to calculate center-of-mass.
labels : ndarray, optional
    Labels for objects in `input`, as generated by `ndimage.label`.
    Only used with `index`.  Dimensions must be the same as `input`.
index : int or sequence of ints, optional
    Labels for which to calculate centers-of-mass. If not specified,
    all labels greater than zero are used.  Only used with `labels`.

Returns
-------
center_of_mass : tuple, or list of tuples
    Coordinates of centers-of-mass.

Examples
--------
>>> a = np.array(([0,0,0,0],
                  [0,1,1,0],
                  [0,1,1,0],
                  [0,1,1,0]))
>>> from scipy import ndimage
>>> ndimage.measurements.center_of_mass(a)
(2.0, 1.5)

Calculation of multiple objects in an image

>>> b = np.array(([0,1,1,0],
                  [0,1,0,0],
                  [0,0,0,0],
                  [0,0,1,1],
                  [0,0,1,1]))
>>> lbl = ndimage.label(b)[0]
>>> ndimage.measurements.center_of_mass(b, lbl, [1,2])
[(0.33333333333333331, 1.3333333333333333), (3.5, 2.5)]