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
dataarray are automatically masked. The final mask is a logical OR combination of the inputmask, the automatically generated mask for non-finite values, and the mask of the inputdataif it is aMaskedArray. The centroid is calculated using only the unmasked data values.- Parameters:
- dataarray_like
The input n-dimensional array.
datacan be aMaskedArray. 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 aTruevalue indicates the corresponding element ofdatais masked. Ifdatais aMaskedArray, its mask will be combined (using bitwise OR) with the inputmask.
- Returns:
- centroid
ndarray 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 andarrayof NaN values will be returned. If the sum is close to zero, the centroid may be poorly defined and fall outside the array bounds.
- centroid
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)