centroid_sources¶
- photutils.centroids.centroid_sources(data, xpos, ypos, box_size=11, footprint=None, mask=None, centroid_func=<function centroid_com>, **kwargs)[source]¶
Calculate the centroid of sources at the defined positions.
A cutout image centered on each input position will be used to calculate the centroid position. The cutout image is defined either using the
box_size
orfootprint
keyword. Thefootprint
keyword can be used to create a non-rectangular cutout image.- Parameters:
- data2D
ndarray
The 2D image data. The image should be background-subtracted.
- xpos, yposfloat or array_like of float
The initial
x
andy
pixel position(s) of the center position. A cutout image centered on this position be used to calculate the centroid.- box_sizeint or array_like of int, optional
The size of the cutout image along each axis. If
box_size
is a number, then a square cutout ofbox_size
will be created. Ifbox_size
has two elements, they must be in(ny, nx)
order.box_size
must have odd values for both axes. Eitherbox_size
orfootprint
must be defined. If they are both defined, thenfootprint
overridesbox_size
.- footprintbool
ndarray
, optional A 2D boolean array where
True
values describe the local footprint region to cutout.footprint
can be used to create a non-rectangular cutout image, in which case the inputxpos
andypos
represent the center of the minimal bounding box for the inputfootprint
.box_size=(n, m)
is equivalent tofootprint=np.ones((n, m))
. Eitherbox_size
orfootprint
must be defined. If they are both defined, thenfootprint
overridesbox_size
.- mask2D bool
ndarray
, optional A 2D boolean array with the same shape as
data
, where aTrue
value indicates the corresponding element ofdata
is masked.- error2D
ndarray
, optional The 2D array of the 1-sigma errors of the input
data
.error
must have the same shape asdata
.error
will be used only if supported by the inputcentroid_func
.- centroid_funccallable, optional
A callable object (e.g., function or class) that is used to calculate the centroid of a 2D array. The
centroid_func
must accept a 2Dndarray
, have amask
keyword and optionally anerror
keyword. The callable object must return a tuple of two 1Dndarray
, representing the x and y centroids. The default iscentroid_com
.- **kwargs
dict
Any additional keyword arguments accepted by the
centroid_func
.
- data2D
- Returns:
- xcentroid, ycentroid
ndarray
The
x
andy
pixel position(s) of the centroids. NaNs will be returned where the centroid failed. This is usually due abox_size
that is too small when using a fitting-based centroid function (e.g.,centroid_1dg
,centroid_2dg
, orcentroid_quadratic
.
- xcentroid, ycentroid
Examples
>>> from photutils.centroids import centroid_com, centroid_sources >>> from photutils.datasets import make_4gaussians_image >>> from photutils.utils import circular_footprint >>> data = make_4gaussians_image()
>>> x_init = (25, 91, 151, 160) >>> y_init = (40, 61, 24, 71) >>> footprint = circular_footprint(5.0) >>> x, y = centroid_sources(data, x_init, y_init, footprint=footprint, ... centroid_func=centroid_com) >>> print(x) [ 24.9865905 90.84751557 150.50228056 159.74319544] >>> print(y) [40.01096547 60.92509086 24.52986889 70.57971148]
import matplotlib.pyplot as plt from photutils.centroids import centroid_com, centroid_sources from photutils.datasets import make_4gaussians_image from photutils.utils import circular_footprint data = make_4gaussians_image() x_init = (25, 91, 151, 160) y_init = (40, 61, 24, 71) footprint = circular_footprint(5.0) x, y = centroid_sources(data, x_init, y_init, footprint=footprint, centroid_func=centroid_com) plt.figure(figsize=(8, 4)) plt.imshow(data, origin='lower', interpolation='nearest') plt.scatter(x, y, marker='+', s=80, color='red', label='Centroids') plt.legend() plt.tight_layout()
(
Source code
,png
,hires.png
,pdf
,svg
)