def make_background_image():
    """Estimate background image.

    See the `KernelBackgroundEstimator` tutorial and
    documentation how it works, or the SciNeGHe 2014 proceeding
    by Ellis Owen et al.
    """
    radius = Angle(0.2, 'deg')
    r_in = Angle(0.3, 'deg')
    r_out = Angle(0.7, 'deg')
    significance_threshold = 5
    mask_dilation_radius = Angle(0.1, 'deg')
    max_iterations = 3

    hdu = fits.open(COUNTS_IMAGE)['COUNTS']
    binsz = hdu.header['CDELT2']
    images = KernelBackgroundEstimatorData(counts=hdu.data, header=hdu.header)

    # TODO: we should have utility functions to initialise
    # kernels with angles so that we don't have to convert to pix here.
    source_kernel = binary_disk(radius=radius.deg/binsz)
    background_kernel = binary_ring(r_in=r_in.deg/binsz,
                                    r_out=r_out.deg/binsz)

    estimator = KernelBackgroundEstimator(
        images=images, source_kernel=source_kernel, background_kernel=background_kernel,
        significance_threshold=significance_threshold,
        mask_dilation_radius=mask_dilation_radius.deg/binsz,
    )
    print('Running background estimation ...')
    estimator.run(max_iterations=max_iterations)

    print('Writing {}'.format(MASK_IMAGE))
    estimator.mask_image_hdu.writeto(MASK_IMAGE, clobber=True)

    print('Writing {}'.format(BACKGROUND_IMAGE))
    estimator.background_image_hdu.writeto(BACKGROUND_IMAGE, clobber=True)
from gammapy.detect import KernelBackgroundEstimator

# Parameters
CORRELATION_RADIUS = 10  # Pixels
SIGNIFICANCE_THRESHOLD = 5  # Sigma
MASK_DILATION_RADIUS = 0.5 * u.deg

# Load example images.
filename = ('$GAMMAPY_EXTRA/datasets/source_diffuse_separation/'
            'galactic_simulations/fermi_counts.fits')
counts = SkyImage.read(filename)
center = SkyCoord(0, 0, frame='galactic', unit='deg')

images = SkyImageList()
images['counts'] = counts.cutout(center, (10 * u.deg, 80 * u.deg))

kernel_src = Tophat2DKernel(CORRELATION_RADIUS).array
kernel_bkg = np.ones((10, 150))

kbe = KernelBackgroundEstimator(
    kernel_src=kernel_src,
    kernel_bkg=kernel_bkg,
    significance_threshold=SIGNIFICANCE_THRESHOLD,
    mask_dilation_radius=MASK_DILATION_RADIUS,
)

result = kbe.run(images)
kbe.images_stack_show()
plt.show()

# Counts must be provided as an ImageHDU
counts = fits.open(filename)[0].data
header = fits.open(filename)[0].header
images = KernelBackgroundEstimatorData(counts=counts, header=header)

source_kernel = binary_disk(CORRELATION_RADIUS)

background_kernel = np.ones((10, 100))

# *** ITERATOR ***

kbe = KernelBackgroundEstimator(
    images=images,
    source_kernel=source_kernel,
    background_kernel=background_kernel,
    significance_threshold=SIGNIFICANCE_THRESHOLD,
    mask_dilation_radius=MASK_DILATION_RADIUS,
)

n_iterations = 4

# *** RUN & PLOT ***
plt.figure(figsize=(8, 4))

for iteration in range(n_iterations):
    kbe.run_iteration()
    mask_hdu = kbe.mask_image_hdu
    mask = mask_hdu.data[:, 1400:2000]

    plt.subplot(n_iterations, 2, 2 * iteration + 1)
# Counts must be provided as an ImageHDU
counts = fits.open(filename)[0].data
header = fits.open(filename)[0].header
images = KernelBackgroundEstimatorData(counts=counts, header=header)

source_kernel = binary_disk(CORRELATION_RADIUS)

background_kernel = np.ones((10, 100))

# *** ITERATOR ***

kbe = KernelBackgroundEstimator(
    images=images,
    source_kernel=source_kernel,
    background_kernel=background_kernel,
    significance_threshold=SIGNIFICANCE_THRESHOLD,
    mask_dilation_radius=MASK_DILATION_RADIUS,
)

n_iterations = 4

# *** RUN & PLOT ***
plt.figure(figsize=(8, 4))

for iteration in range(n_iterations):
    kbe.run_iteration()
    mask_hdu = kbe.mask_image_hdu
    mask = mask_hdu.data[:, 1400:2000]

    plt.subplot(n_iterations, 2, 2 * iteration + 1)