detect_sources#

photutils.segmentation.detect_sources(data, threshold, npixels, *, connectivity=8, mask=None)[source]#

Detect sources above a specified threshold value in an image.

Detected sources must have npixels connected pixels that are each greater than the threshold value in the input data. The input mask can be used to mask pixels in the input data. Masked pixels will not be included in any source.

This function does not deblend overlapping sources. First use this function to detect sources followed by deblend_sources() to deblend sources. Alternatively, use the SourceFinder class to detect and deblend sources in a single step.

Parameters:
data2D ndarray

The 2D array of the image. If filtering is desired, please input a convolved image.

thresholdfloat or 2D ndarray

The data value or pixel-wise data values to be used for the detection threshold. If data is a Quantity array, then threshold must have the same units as data. A 2D threshold array must have the same shape as data.

npixelsint

The minimum number of connected pixels, each greater than threshold, that an object must have to be detected. npixels must be a positive integer.

connectivity{4, 8}, optional

The type of pixel connectivity used in determining how pixels are grouped into a detected source. The options are 4 or 8 (default). 4-connected pixels touch along their edges. 8-connected pixels touch along their edges or corners.

mask2D bool ndarray, optional

A boolean mask, with the same shape as the input data, where True values indicate masked pixels. Masked pixels will not be included in any source.

Returns:
segment_imageSegmentationImage or None

A 2D segmentation image, with the same shape as data, where sources are marked by different positive integer values. A value of zero is reserved for the background. If no sources are found then None is returned.

Raises:
NoDetectionsWarning

If no sources are found.

Examples

import matplotlib.pyplot as plt
from astropy.convolution import convolve
from astropy.stats import sigma_clipped_stats
from astropy.visualization import simple_norm
from photutils.datasets import make_100gaussians_image
from photutils.segmentation import (detect_sources,
                                    make_2dgaussian_kernel)

# make a simulated image
data = make_100gaussians_image()

# use sigma-clipped statistics to (roughly) estimate the background
# background noise levels
mean, _, std = sigma_clipped_stats(data)

# subtract the background
data -= mean

# detect the sources
threshold = 3. * std
kernel = make_2dgaussian_kernel(3.0, size=3)  # FWHM = 3.
convolved_data = convolve(data, kernel)
segm = detect_sources(convolved_data, threshold, npixels=5)

# plot the image and the segmentation image
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 10))
norm = simple_norm(data, 'sqrt', percent=99.)
ax1.imshow(data, origin='lower', interpolation='nearest',
           norm=norm)
ax2.imshow(segm.data, origin='lower', interpolation='nearest',
           cmap=segm.make_cmap(seed=1234))
plt.tight_layout()

(Source code, png, hires.png, pdf, svg)

../_images/photutils-segmentation-detect_sources-1.png