deblend_sources#

photutils.segmentation.deblend_sources(data, segmentation_image, n_pixels, *, labels=None, n_levels=32, contrast=0.001, contrast_method=None, mode='exponential', connectivity=8, relabel=True, n_threads=1, nproc=1, n_processes=1, progress_bar=True)[source]#

Deblend overlapping sources labeled in a segmentation image.

Sources are deblended using a combination of multithresholding and watershed segmentation. In order to deblend sources, they must be separated enough that there is a saddle point between them.

Parameters:
data2D ndarray or Quantity

The 2D array of the image. If filtering is desired, please input a convolved image here. This array should be the same array used in detect_sources. NaN pixels within a source segment are excluded from the multithreshold levels and the source flux, and are assigned to a neighboring deblended source after all finite pixels have been assigned.

segmentation_imageSegmentationImage

The segmentation image to deblend.

n_pixelsint

The minimum number of connected pixels, each greater than a given multithresholding level, that a local peak must have at that level to be considered a separate object. n_pixels must be a positive integer. Sources with fewer than 2 * n_pixels pixels are not deblended.

labelsint or array_like of int, optional

The label numbers to deblend. If None (default), then all labels in the segmentation image will be deblended.

n_levelsint, optional

The number of multithresholding levels to use for deblending. Each source will be rethresholded at n_levels levels spaced between its minimum and maximum values (noninclusive). The mode keyword determines how the levels are spaced.

contrastfloat, optional

The fraction of the total source flux that a local peak must have (at any one of the multithresholds) to be deblended as a separate object. contrast must be between 0 and 1, inclusive. If contrast=0 then every local peak will be made a separate object (maximum deblending). If contrast=1 then no deblending will occur. The default is 0.001, which will deblend sources with a 7.5 magnitude difference. The contrast_method keyword selects the flux to which the fraction refers.

contrast_method{None, ‘basin’, ‘saddle’}, optional

The flux used by the contrast criterion. For 'basin', the fraction is the total flux in the source’s watershed basin, which includes the share of the surrounding envelope territory assigned to the source. Basins below the contrast are removed iteratively (faintest first, in batches when provably equivalent) and their territory is reflooded. For 'saddle', the fraction is the flux the source holds above the saddle level where it separates from its neighbors, evaluated once during marker construction with no iteration. This measures the significance of the peak itself, independently of how much envelope territory it would inherit. Because 'saddle' needs only a single watershed pass, it is never slower than 'basin' and is substantially faster when many below-contrast basins would otherwise be removed iteratively. Note that the two methods measure different fluxes, so the same contrast value selects sources differently. The saddle flux excludes all regions below the saddle and is therefore smaller than the basin flux, making 'saddle' stricter for faint peaks on bright envelopes. If None (default), the 'basin' method is currently used. The default may change to 'saddle' in version 4.0.

mode{‘exponential’, ‘linear’, ‘sinh’}, optional

The mode used in defining the spacing between the multithresholding levels (see the n_levels keyword) during deblending. The 'exponential' and 'sinh' modes have more threshold levels near the source minimum and fewer near the source maximum. The 'linear' mode evenly spaces the threshold levels between the source minimum and maximum. The 'exponential' and 'sinh' modes differ in that the 'exponential' levels are dependent on the source maximum/minimum ratio (smaller ratios are more linear and larger ratios are more exponential), while the 'sinh' levels are not. Unlike the 'exponential' mode, the 'sinh' and 'linear' modes are well defined for sources with nonpositive minimum data values. For such sources, the 'exponential' mode will be changed to 'sinh'. In addition, with the 'basin' contrast method, a source that has more than 200 potential deblended sources (markers) in the 'exponential' or 'sinh' mode is deblended again in the 'linear' mode, because that many markers make the deblending very slow.

connectivity{8, 4}, optional

The type of pixel connectivity used in determining how pixels are grouped into a detected source. The options are 8 (default) or 4. 8-connected pixels touch along their edges or corners. 4-connected pixels touch along their edges. The connectivity must be the same as that used to create the input segmentation image. A ValueError is raised if a source cannot be deblended because the connectivities differ.

relabelbool, optional

If True (default), then the segmentation image will be relabeled such that the labels are in consecutive order starting from 1. If False, the labels of the sources that are not deblended are unchanged, and each deblended source is replaced by new labels that are larger than the maximum label of the input segmentation image.

n_threadsint, optional

The number of threads to use to deblend the sources. The default is 1 (no multithreading). When n_threads > 1, the sources are divided into chunks and processed concurrently. The per-source results are independent and are assembled in the input-label order, so they are identical to the single-threaded computation. Each chunk is deblended by a few compiled calls that release the Python global interpreter lock (GIL), so multithreading speeds up the deblending of fields of many small sources as well as of large sources.

nprocint, optional

This keyword is deprecated and has no effect. It was the name of the n_processes keyword before version 3.0.

Deprecated since version 3.0: The nproc keyword is deprecated and will be removed in version 4.0.

n_processesint, optional

This keyword is deprecated and has no effect. Multiprocessing no longer provides any benefit. The deblending computation is now dominated by compiled code, and the process startup and data-pickling overheads of multiprocessing made it slower than the serial implementation. Use the n_threads keyword instead.

Deprecated since version 3.1: The n_processes keyword is deprecated and will be removed in version 4.0.

progress_barbool, optional

This keyword is deprecated and has no effect. Deblending no longer displays a progress bar.

Deprecated since version 3.1: The progress_bar keyword is deprecated and will be removed in version 4.0.

Returns:
segment_imageSegmentationImage

A 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. The info attribute of the returned segmentation image is a dictionary that stores the input labels for which the deblending mode was changed to a fallback mode as arrays under 'non_positive_min_labels' (nonpositive minimum data values, changed to “sinh”) and 'too_many_markers_labels' (too many potential deblended sources, changed to “linear”) keys. The dictionary is empty if no mode fallbacks occurred. The flags attribute of the returned segmentation image records per-source deblending provenance. See decode_segmentation_flags.

Warns:
DeblendWarning

If the deblending mode for one or more sources was changed from mode to “sinh” due to nonpositive minimum data values or to “linear” due to too many potential deblended sources.