centroid_quadratic#
- photutils.centroids.centroid_quadratic(data, mask=None, fit_boxsize=5, xpeak=None, ypeak=None, search_boxsize=None)[source]#
Calculate the centroid of a 2D array by fitting a 2D quadratic polynomial.
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.A second degree 2D polynomial is fit within a small region of the data defined by
fit_boxsizeto calculate the centroid position. The initial center of the fitting box can be specified using thexpeakandypeakkeywords. If bothxpeakandypeakareNone, then the box will be centered at the position of the maximum value in the inputdata.If
xpeakandypeakare specified, thesearch_boxsizeoptional 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 array_like
The 2D image data.
datacan be aMaskedArray. The image should be a background-subtracted cutout image containing a single source.- mask2D bool
ndarray, optional A boolean mask, with the same shape as
data, where aTruevalue indicates the corresponding element ofdatais masked. Masked data are excluded from calculations. Ifdatais aMaskedArray, its mask will be combined (using bitwise OR) with the inputmask.- fit_boxsizeint or tuple of int, optional
The size (in pixels) of the box used to define the fitting region. If
fit_boxsizehas two elements, they must be in(ny, nx)order. Iffit_boxsizeis a scalar then a square box of sizefit_boxsizewill be used.fit_boxsizemust have odd values for both axes.- xpeak, ypeakfloat or
None, optional The initial guess of the position of the centroid. If either
xpeakorypeakisNonethen the position of the maximum value in the inputdatawill be used as the initial guess.Deprecated since version 3.0: The
xpeakandypeakkeywords are deprecated and will be removed in a future version. Usecentroid_sourcesto centroid sources at specific positions.- search_boxsizeint or tuple of int, optional
The size (in pixels) of the box used to search for the maximum pixel value if
xpeakandypeakare both specified. Ifsearch_boxsizehas two elements, they must be in(ny, nx)order. Ifsearch_boxsizeis a scalar then a square box of sizesearch_boxsizewill be used.search_boxsizemust have odd values for both axes. This parameter is ignored if eitherxpeakorypeakisNone. In that case, the entire array is searched for the maximum value.Deprecated since version 3.0: The
search_boxsizekeyword is deprecated and will be removed in a future version. Usecentroid_sourcesto centroid sources at specific positions.
- Returns:
- centroid
ndarray The
x, ycoordinates 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)