centroid_quadratic#
- photutils.centroids.centroid_quadratic(data, xpeak=None, ypeak=None, fit_boxsize=5, search_boxsize=None, mask=None)[source]#
Calculate the centroid of an n-dimensional array by fitting a 2D quadratic polynomial.
A second degree 2D polynomial is fit within a small region of the data defined by
fit_boxsize
to calculate the centroid position. The initial center of the fitting box can specified using thexpeak
andypeak
keywords. If bothxpeak
andypeak
areNone
, then the box will be centered at the position of the maximum value in the inputdata
.If
xpeak
andypeak
are specified, thesearch_boxsize
optional keyword can be used to further refine the initial center of the fitting box by searching for the position of the maximum pixel within a box of sizesearch_boxsize
.Vakili & Hogg (2016) demonstrate that 2D quadratic centroiding comes very close to saturating the Cramér-Rao lower bound in a wide range of conditions.
- Parameters:
- data2D
ndarray
The 2D image data. The image should be a background-subtracted cutout image containing a single source.
- xpeak, ypeakfloat or
None
, optional The initial guess of the position of the centroid. If either
xpeak
orypeak
isNone
then the position of the maximum value in the inputdata
will be used as the initial guess.- fit_boxsizeint or tuple of int, optional
The size (in pixels) of the box used to define the fitting region. If
fit_boxsize
has two elements, they must be in(ny, nx)
order. Iffit_boxsize
is a scalar then a square box of sizefit_boxsize
will be used.fit_boxsize
must have odd values for both axes.- search_boxsizeint or tuple of int, optional
The size (in pixels) of the box used to search for the maximum pixel value if
xpeak
andypeak
are both specified. Iffit_boxsize
has two elements, they must be in(ny, nx)
order. Ifsearch_boxsize
is a scalar then a square box of sizesearch_boxsize
will be used.search_boxsize
must have odd values for both axes. This parameter is ignored if eitherxpeak
orypeak
isNone
. In that case, the entire array is searched for the maximum value.- maskbool
ndarray
, optional A boolean mask, with the same shape as
data
, where aTrue
value indicates the corresponding element ofdata
is masked. Masked data are excluded from calculations.
- data2D
- Returns:
- centroid
ndarray
The
x, y
coordinates of the centroid.
- centroid
Notes
Use
fit_boxsize = (3, 3)
to match the work of Vakili & Hogg (2016) for their 2D second-order polynomial centroiding method.Because this centroid is based on fitting data, it can fail for many reasons, returning (np.nan, np.nan):
quadratic fit failed
quadratic fit does not have a maximum
quadratic fit maximum falls outside image
not enough unmasked data points (6 are required)
Also note that a fit is not performed if the maximum data value is at the edge of the data. In this case, the position of the maximum pixel will be returned.
References
[1]Vakili and Hogg 2016, “Do fast stellar centroiding methods saturate the Cramér-Rao lower bound?”, arXiv:1610.05873
Examples
>>> import numpy as np >>> from photutils.datasets import make_4gaussians_image >>> from photutils.centroids import centroid_quadratic >>> data = make_4gaussians_image() >>> data -= np.median(data[0:30, 0:125]) >>> data = data[40:80, 70:110] >>> x1, y1 = centroid_quadratic(data) >>> print(np.array((x1, y1))) [19.94009505 20.06884997]
(
Source code
,png
,hires.png
,pdf
,svg
)