centroid_com#

photutils.centroids.centroid_com(data, mask=None)[source]#

Calculate the centroid of an array as the flux-weighted center of mass derived from image moments.

Non-finite values (e.g., NaN or inf) in the data array are automatically masked. The final mask is a logical OR combination of the input mask, the automatically generated mask for non-finite values, and the mask of the input data if it is a MaskedArray. The centroid is calculated using only the unmasked data values.

Parameters:
dataarray_like

The input n-dimensional array. data can be a MaskedArray. The image should be a background-subtracted cutout image containing a single source. The source should be significantly stronger than the background noise. If the data contains nearly equal positive and negative values (i.e., the sum is close to zero), the centroid calculation will be numerically unstable and may produce undefined results that fall outside the array bounds.

maskbool ndarray, optional

A boolean mask, with the same shape as data, where a True value indicates the corresponding element of data is masked. If data is a MaskedArray, its mask will be combined (using bitwise OR) with the input mask.

Returns:
centroidndarray

The coordinates of the centroid in pixel order (e.g., (x, y) or (x, y, z)), not numpy axis order. If the sum of the (unmasked) data is zero, then a ndarray of NaN values will be returned. If the sum is close to zero, the centroid may be poorly defined and fall outside the array bounds.

Notes

The centroid is calculated as:

\[x_c = \frac{\sum x_i I_i}{\sum I_i}, \quad y_c = \frac{\sum y_i I_i}{\sum I_i}\]

where \(I_i\) is the intensity at pixel \((x_i, y_i)\).

Examples

>>> import numpy as np
>>> from photutils.datasets import make_4gaussians_image
>>> from photutils.centroids import centroid_com
>>> data = make_4gaussians_image()
>>> data -= np.median(data[0:30, 0:125])
>>> data = data[40:80, 70:110]
>>> x1, y1 = centroid_com(data)
>>> print(np.array((x1, y1)))
[19.9796724  20.00992593]

(Source code, png, hires.png, pdf, svg)

../_images/photutils-centroids-centroid_com-1.png