find_peaks#
- photutils.detection.find_peaks(data, threshold, *, box_size=3, footprint=None, mask=None, border_width=None, n_peaks=inf, min_separation=None, centroid_func=None, error=None, wcs=None)[source]#
Find local peaks in an image that are above a specified threshold value.
Peaks are the maxima above the
thresholdwithin a local region. The local regions are defined by either thebox_sizeorfootprintparameters.box_sizedefines the local region around each pixel as a square box.footprintis a boolean array whereTruevalues specify the region shape.If multiple pixels within a local region have identical intensities, then the coordinates of all such pixels are returned. Otherwise, there will be only one peak pixel per local region. Thus, the defined region effectively imposes a minimum separation between peaks unless there are identical peaks within the region.
When
min_separationis set, a fast algorithm is used that produces results equivalent to using a circularfootprintof the given radius formaximum_filter, but is typically ~10-400x faster (depending on the radius). When set,box_sizeandfootprintare not used for peak detection.If
centroid_funcis input, then it will be used to calculate a centroid within the defined local region centered on each detected peak pixel. In this case, the centroid will also be returned in the output table.- Parameters:
- dataarray_like
The 2D array of the image.
- thresholdfloat, scalar
Quantityor array_like The data value or pixel-wise data values to be used for the detection threshold. A peak is detected only if it is strictly greater than the
threshold. Ifdatais aQuantityarray, thenthresholdmust have the same units asdata. A 2Dthresholdmust have the same shape asdata. Seedetect_thresholdfor one way to create athresholdimage.- box_sizescalar or tuple, optional
The size of the local region to search for peaks at every point in
data. Ifbox_sizeis a scalar, then the region shape will be(box_size, box_size). Eitherbox_sizeorfootprintmust be defined. If they are both defined, thenfootprintoverridesbox_size.- footprint
ndarrayof bools, optional A boolean array where
Truevalues describe the local footprint region within which to search for peaks at every point indata.box_size=(n, m)is equivalent tofootprint=np.ones((n, m)). Eitherbox_sizeorfootprintmust be defined. If they are both defined, thenfootprintoverridesbox_size.- maskarray_like, bool, optional
A boolean mask with the same shape as
data, where aTruevalue indicates the corresponding element ofdatais masked.- border_widthint, array_like of int, or None, optional
The width in pixels to exclude around the border of the
data. Ifborder_widthis a scalar thenborder_widthwill be applied to all sides. Ifborder_widthhas two elements, they must be in(ny, nx)order. IfNone, then no border is excluded. The border width values must be non-negative integers.- n_peaksint, optional
The maximum number of peaks to return. When the number of detected peaks exceeds
n_peaks, the peaks with the highest peak intensities will be returned.- min_separationfloat or None, optional
The minimum allowed separation (in pixels) between detected peaks, enforced using a circular region of this radius. Each detected peak must be the maximum value (or tied for the maximum) within a circle of this radius. This is equivalent to using a circular
footprintof the given radius but uses a fast algorithm that is typically ~10-400x faster (depending on the radius). When set,box_sizeandfootprintare not used for peak detection. IfNone(default), the peak detection usesbox_sizeorfootprintas specified.- centroid_funccallable, optional
A callable object (e.g., function or class) that is used to calculate the centroid of a 2D array. The
centroid_funcmust accept a 2Dndarray, have amaskkeyword, and optionally anerrorkeyword. The callable object must return a tuple of two 1Dndarrayobjects, representing the x and y centroids, respectively.- errorarray_like, optional
The 2D array of the 1-sigma errors of the input
data.erroris used only ifcentroid_funcis input (theerrorarray is passed directly to thecentroid_func). Ifdatais aQuantityarray, thenerrormust have the same units asdata.- wcs
Noneor WCS object, optional A world coordinate system (WCS) transformation that supports the astropy shared interface for WCS (e.g.,
astropy.wcs.WCS,gwcs.wcs.WCS). IfNone, then the sky coordinates will not be returned in the outputTable.
- Returns:
Notes
By default, the returned pixel coordinates are the integer indices of the maximum pixel value within the input
box_sizeorfootprint(i.e., only the peak pixel is identified).When
min_separationis given, peaks are detected using a fast algorithm that is mathematically equivalent to a circularfootprintof the given radius formaximum_filter. The algorithm uses two fast O(N) separable box filters (inscribed and circumscribed squares of the circle) to classify most candidates, then verifies only the remaining few against the exact circular region.A centroiding function can be input via the
centroid_funckeyword to compute centroid coordinates with subpixel precision within the inputbox_sizeorfootprint. Note that whenmin_separationis used, the centroid region size is determined bybox_size(default 3), not bymin_separation.The peak detection uses
mode='constant'withcval=0.0formaximum_filter, which means pixels outside the image boundary are treated as zero. For images with all-negative values, this may suppress legitimate peaks near the borders.Any NaN values in the input
dataare replaced with the minimum finite value before peak detection, and the corresponding pixels are automatically excluded from the results.The output column names (
x_peak,y_peak,peak_value) differ from the star finder classes (e.g.,DAOStarFinder), which usex_centroid,y_centroid, andflux.