Image processing: Gradient removal

Why are there gradients in already flat-fielded images?

Flat fields are not perfect, nor is the process of imaging. There are many reasons why flat-fielded images are not flat:

  • scattered light that is otherwise absent at night time enters the telescope during the flats
  • domeflats: the flat screen / flat box is not homogeneously illuminated
  • twilight flats: an unevenly illuminated patch of sky was selected, or thin cirrus was present
  • scattered light (moon, street lights) entered the telescope at night time
  • the night sky is of uneven brightness (airglow, light pollution, cirrus, clouds, moon)
  • when imaging in a clear filter without IR-cutoff, the instable near-IR emission of the atmosphere is seen with some cameras. It can vary within a few minutes and on scales of a few arcminutes. See the discussion about luminance and clear filters
  • galactic cirrus (this is not a problem of the data but actually very faint interstellar gas and dust that shows up in very deep exposures, and thus is real)

A word of warning for those of you who do photometry: Some of these effects are multiplicative in nature and should be divided out, others are additive in nature and should be subtracted. If the wrong approach is chosen, the resulting image will still be flat (and thus nice-looking), but the photometric zeropoints will change across the image. Hence, when you need to correct for uneven residual sky background, choose your approach carefully. However, hereafter I assume that you just want to produce a nice looking image and don't care about such scientific subtleties.

The following method is simple and very effective. It will remove even very ugly gradients reliably. The procedure reads a bit complicated, but if you tried it once you will find it quite easy.

My gradient removal method

In the following we flatten an image of the Antennae galaxies that was taken at low elevation in RGB in a good and clear night. Differential airglow and other atmospheric instabilities resulted in a significantly inhomogeneous background. The contrast in the images below has been much increased for better visibility.
The advantage of this method is that it does not only remove gradients, but it also creates a neutrally coloured background. The latter will always have a brightness level of 20 in RGB after the application, which means that all images processed in this way will look very homogeneous.

Step 1

The sky background level cannot be measured at the position of an object. We need to guess it. To this end, determine the average RGB value of the background sky with the eye dropper tool. The sampling size can be set to 5x5 average, the values can be found in the Info window. Move the tool a bit around to get a feeling for the best value. It does not need to be very accurate.

Step 2

The gradient model is created in a new window. Create a new window with the same dimensions as the original image (hereafter: target image). It has to be in RGB format. The easiest way of doing this is to select everything in the target image and click copy. When you then create a new image, the correct dimensions will already be filled in.

Step 3

Set the foreground colour to the value you estimated for the background in the target image. Then click on the Paint Bucket Tool and fill the newly created gradient model image with this colour.

Step 4

In the target image, we now have to select the background which we want to model. This is done by clicking into the background with the Magic Wand, using a tolerance of 2 or 3. Depending on your image, you need to click several times until all background is selected, or choose a larger tolerance.

Once done, copy the selected pixels and paste them into the gradient model image. You will now see something like this:

This is an image like the target image, but with all objects being replaced by the average background value (which is why we had to do step 1). If you still see for example some flux from the faint extended halo of a galaxy, select it with the Lasso tool and remove it.

Step 5

Flatten the two layers of the gradient model image. Then do Filter -> Blur -> Gaussian Blur to wipe out all the noise. The width of the Gaussian filter can be between 10 and 50 pixels, depending how small the effects are that you want to remove. A value of 30 should usually work fine. The gradient model will then look like this, and at this point deserves its name for the first time:

Step 6

If we subtract the gradient model from the image at this stage, the background in the image will be zero on average. Due to noise, a large amount of pixels will have negative values. As the integer format of the data does not support negative values, the histogram would be severly clipped. Therefore, we have to subtract a pedestial from the gradient model, so that the difference remains positive. This can be achieved by lowering the brightness of the gradient model by 10 (Image -> Adjustments -> Brightness / Contrast, assuming the model has a brightness of around 20).

Step 7

Now the gradient model is copied and pasted back onto the image. The blending mode of the new layer containing the gradient model is set to Difference in the Layers window.

Step 8

In the last step, the target image is flattened, and its brightness is increased by 10 (if desired). The background has then a uniform average brightness level of 20 in each of RGB:

Before flattening After flattening