bundles / scipy latest / scipy / ndimage / _measurements / center_of_mass
function
scipy.ndimage._measurements:center_of_mass
Signature
def center_of_mass ( input , labels = None , index = None ) Summary
Calculate the center of mass of the values of an array at labels.
Parameters
input: ndarrayData from which to calculate center-of-mass. The masses can either be positive or negative.
labels: ndarray, optionalLabels for objects in
input, as generated byndimage.label. Only used withindex. Dimensions must be the same asinput.index: int or sequence of ints, optionalLabels for which to calculate centers-of-mass. If not specified, the combined center of mass of all labels greater than zero will be calculated. Only used with
labels.
Returns
center_of_mass: tuple, or list of tuplesCoordinates of centers-of-mass.
Notes
Array API Standard Support
center_of_mass has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
import numpy as np a = np.array(([0,0,0,0], [0,1,1,0], [0,1,1,0], [0,1,1,0])) from scipy import ndimage✓
ndimage.center_of_mass(a)
✗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.center_of_mass(b, lbl, [1,2])
✗c = np.array(([-1,0,0,0], [0,-1,-1,0], [0,1,-1,0], [0,1,1,0]))✓
ndimage.center_of_mass(c)
✗d = np.array([-1, 1])
✓ndimage.center_of_mass(d)
✗Aliases
-
scipy.ndimage.center_of_mass