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. If the filtering option is used, then the threshold is applied to the filtered image. 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.

data2D ndarray

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

thresholdfloat or 2D ndarray

The data value or pixel-wise data values to be used for the detection threshold. A 2D threshold array must have the same shape and units as data.


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.

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.


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 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',
ax2.imshow(, origin='lower', interpolation='nearest',

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