Background correction
Sections in this page
The problem
Here is a simple photograph of the night sky (30s f/1.8 ISO800 CanonEOS5D 50mm lens).
The background is not black because of light pollution. Notice how the corners are darker than the centre. This occurs for at least two reasons:
- Vignetting - the lens does not provide uniform illumination across the detector or film. This is more noticeable with the more expensive SLR cameras where the detector is the full 35mm standard size (36 x 24mm). Cheaper cameras have smaller detectors. However, watch out for special lenses for such cameras (eg, Canon's EF-S lenses) which will produce vignetting again because they are designed to cover only the smaller detector area, to use less glass and so be lighter and perhaps cheaper.
- Light pollution - scattering of light from street lamps off dust in the atmosphere. This generally will not be uniform and is likely to produce a less symmetrical effect than vignetting, particularly if there are street lamps nearby.
Adobe Photoshop has facilities for correcting vignetting but it is unable to deal with asymmetrical effects.
How significant is all this? Using GRIP's measurement menu and selecting the straight line option we can set two points near the corners of the image. On releasing the mouse from setting the second point, up pop the measurements which include a graphical profile along the straight line between the points:
The horizontal axis of this graph is distance along the line from the first point set, which in this case is near the top left corner of the image. The vertical axis is the detected level. This image has 16 bits per channel so the scale of the vertical axis is from 0 to 65535 (that is up to 216 - 1). There are four traces in the graph: one for each channel (each drawn in the appropriate colour) and a black trace which is the average of the other three.
Above the graph is a redrawn image which shows the values sampled along the line but smeared out vertically so they can be seen. Notice how bright and dark lines in the redrawn profile correspond to peaks and troughs on the graph. In particular, notice what happens when the line goes over or near a star.
Notice also that the background levels jitter up and down. That is noise, which we discuss elswhere.
If we want software to detect stars automatically, differentiating them from the background, it needs to detect pixels which are brighter than a certain level. But how do we say what that level should be when the background level is so un-flat?
GRIP's solution
The approach used in GRIP (and other software for image analysis) is first to correct the image, so the background is flatter. Here is the result of the background correction option from GRIP's levels menu, for the image we have been studying:
Measuring the diagonal now shows that the background is much flatter and we can envisage setting a level just above the background such that pixels brighter than that level can be deemed to belong to stars.
The method
GRIP's code for background correction can be found in the class ImProcess, in the static method correctBackground (BufferedImage im). The steps are as follows.
- Divide the image up into 16 x 16 rectangular cells, by dividing the image width and height by 16.
- In each cell, for each channel, build the frequency histogram and find its mode (ie, the level which has the greatest frequency). This is taken to be the background level for this cell and channel.
- Redraw the image. For each pixel determine how far it is from the centres of the cell in which it lies and neighbouring cells. For each channel, interpolate the histogram modes between cells, for this pixel position. Subtract 7/8 of the interpolated mode from the pixel value for this channel. Draw the result.
The interpolation formula is very similar to that described for pixel interpolation except that here we interpolate between the centres of cells rather than the centres of pixels.
The factor of 7/8 when subtracting the mode is so that the background is not lost from the bottom of the profile, so we can still estimate noise. This a configuration option for GRIP, so it can be changed in the configuration dialogue.