chi and h site logo

{ Practical astronomy | Optics and imaging | Image analysis }

Image analysis


Quantitative analysis

With the image reduced and brought into a good standard state, we can carry out analysis like measuring and comparing the brightness of small sources, or measuring their positions. Starlink's Gaia utility (Draper et al. 2014) provides a convenient interactive interface to other packages that carry out these tasks.

It should be noted that this sort of analysis should use relatively raw data, corrected for bias, dark current and flat field effects, and with noise suppressed by stacking. The more subjective processing we will come to next should not be done before such quantitative analysis takes place.

High dynamic range processing

Before we come to the more mundane cosmetic improvements, we look at the problem of a high dynamic range in our images. A good example is the lunar crescent and the faint Earth shine on the dark side of the Moon. The contrast between these two sides of the Moon is vast, presenting a challenge to taking images of it. What is required is to combine several images of different exposure into one data set, and processing this into something that can be made visible in a regular 8-bit image format. Although the method is highly mathematical and un-intuitive, the objective is largely cosmetic rather than one of analysis.

Data acquisition

To record high dynamic range data, you have to take raw frames. You often have to take a sequence of frames, doubling or halving the exposure time from one frame to the next. The shortest exposure must not saturate anywhere, the longest exposure must record the faint detail above the noise. If in doubt, take a longer sequence and discard meaningless frames at the start of the data reduction.

The frames must be dark-corrected. In each frame, disregard the pixel values that are in the lower or upper sixth or eight of the value range. An exception is the longest exposure, in which the lower sixth of the value range has to survive, as there is no longer exposure from which to fill those dark areas. Removing the faint areas from the short exposures prevents them dominating the noise in those areas. Removing the bright areas, of course, prevents using saturated pixels. Discarding these frame pixels should do no harm. Wherever one frame is marked as "do not use" a frame with longer or shorter exposure will fill the picture.

Each frame is divided by its exposure time. All frames then contain numbers telling the amount of light arriving per second rather than during the, varying, exposure time. The frames can then be combined into a single image. This has to be done by averaging in-range input pixels across the frame sequence and ignoring out-of-range pixels in any frame. The resulting image should have all pixels filled with good data. Many of its pixels stem from only one frame, some from two or three frames. It should go without saying that, similar to stacking frames, any shifts between the frames have to be determined and compensated.

Chicam has the utility hdrfill to carry out this combination of an exposure sequence into a single high dynamic range data array.

Tone mapping

local contrast data, highlighting the steep limb
The local contrast images for smoothing widths s = 4, 8 and 16. For a given region size s, the local contrast is high near the limb of the lunar crescent, and to a lesser degree near the terminator. In these areas, only small regions can be tone-mapped similarly; far away from these edges, medium-sized regions can be tone-mapped similarly.

We now consider the image an array of pixels (x,y) containing the luminance L(x,y). If the data have RGB channels, L should be calculated as a weighted average of 70% G, 20% R and 10% B. The following algorithm follows closely that described by Ashikhmin (2002).

In tone mapping, a clever algorithm reduces the global contrast so that it can fit in the dynamic range of an 8-bit image format, while maintaining local contrast. The first step in the algorithm presented here is to generate smoothed copies of the luminance image. A range of small to medium smoothing widths has to be used. We use Gauß smoothing Gs(L) of widths (full width at half maximum) of s = 1, 2, 3, 4 ..., 16 and 18, 20, 22, ..., 32 pixels. These smoothed images allow us to calculate the band-limited local contrast for the first 16 values of s:

lc(s,x,y) = [Gs(L)(x,y) − G2s(L)(x,y)] / Gs(L)(x,y)

neighbourhood size and adaptation level
On the left is the locally uniform neighbourhood size. Over most of the image this large (white), but near the limb and terminator it is small (black). On the right is the adaptation level shown with two linear stretches for the top and bottom half of the image. This image is somewhat similar to a smoothed version of the original image, but the smoothing width changes according to the size of the locally uniform neighbourhood.

The "local contrast" is the contrast over a region of characteristic radius s. For any given pixel (x,y) the local contrast tends to increase as the region grows. We are interested in the region size when the local contrast grows "too large", the locally uniform neighbourhood size:

slun(x,y) = max{s | |lc(s,x,y)| ≤ 0.5}

If a pixel has larger neighbourhood size, the adaptation level for the tone mapping can be taken from a wider smoothing of the given luminance L. The adaptation level for each pixel is calculated as

La(x,y) = Gslun(x,y)(L)(x,y)

From the input luminance L(x,y) and the local adaptation level La(x,y) we can now carry out the tone mapping and restoration of local contrast with

Ld(x,y) = [L(x,y)/La(x,y)] · [ln(La(x,y)) − ln(G4(L)min)]
             / [ln(G4(L)max) − ln(G4(L)min)]

This is the output image or display luminance. If the given raw data are in colour, we use Ld/L in each pixel to scale those data in proportion to the luminance scaling.

Chicam has the utility hdrtone to carry out this tone mapping. If the input is colour, it uses only the G channel as input luminance.

Gamma correction

comparison of different processing: linear stretch, gamma correction, unsharp mask, tone mapping
Comparison of different attempts to show the Moon and Earth shine. Left is a deep linear stretch increasing the brightness 80-fold; it shows the contrast in the dark areas, but saturates the bright areas. Next is an aggressive gamma correction; instead of the common value of 2.2, γ = 1000 is used here. Next is an unsharp mask; this can bring out the lunar detail in part of the dark side very well, but the artefacts around the lunar crescent are very distracting. Right is the tone-mapped result; it begins to show detail on the dark side, retains some detail on the bright side and suppresses the glare.

As input we use raw frames, where the pixel values are a linear measure of the light incident on the pixel. The result of the tone mapping is still equivalent to such raw data. Raw data are not directly suitable for human viewing. A dSLR will apply a gamma correction to generate the JPEG image and we should do the same:

J = Ld1/2.2

Even these numbers cannot be put directly into the 8-bit image value range from 0 to 255. They have to be scaled – or stretched linearly – to map suitable smallest and largest values to 0 and 255, resp.

Cosmetic improvement

Most astrophotographs are perhaps taken to have a nice picture to look at and to show to others, as much an object of art as a scientific record. Applying some of the improvements discussed here will make the image a better one. However, these methods can be driven to excess, making the result less pleasing to look at, and perhaps making them a dishonest record of what was being imaged. If you also want to carry out visual analysis of your images, then you need to be careful not to overdo the processing of this section.

The operations described here can be carried out easily with regular image processing software. On Linux, the standard utility is The Gimp (Natterer et al. 2017), which is also available for other operating systems. Chicam has a few non-interactive utilities as well, which at the very least can be used to convert from the 32-bit floating point FITS format to the 8-bit full-colour formats more suitable in the present context. ImageMagick (ImageMagick 2017) is handy to display images, although it can also do processing. Most people will prefer the interactive processing of The Gimp, but ImageMagick's command-line approach can be very powerful at times.

Crop to smaller pixel size

Cropping the image to a smaller size can focus the viewer's attention on the object you want to show. It can also enable you to use the full image resolution. Say, a 1500x1000-pixel image is too large to put on a web page. If most of it is wasted on black sky anyway, then cropping to a few hundred pixels keeps the relevant information, and in fact makes it more visible.

Incidentally, Chicam has a crop utility that operates on FITS format.

Linear and non-linear stretch

These operations change the brightness and contrast of the image. In a linear stretch, we move the black level up and the white level down. This makes the dark parts even darker and the bright parts even brighter, thereby improving contrast, and possibly also adjusting overall brightness. The tools to do this are interactive; trial and error is the best approach. This is called a linear stretch, because between the new black and white levels the new brightness values are a linear function of the old values; the contrast change is the same at all brightness values.

contrast stretch: before, linear, non-linear
The image on the left turned out too dark. A linear stretch (observe the stretched brightness histogram, centre) brings out the faint detail, but may cause saturation in bright objects. A non-linear stretch (right) can brighten dark areas while retaining some amount of contrast in bright areas.

A nonlinear stretch adds more freedom to the method. The interactive tool allows us to create a curved relationship between old and new brightness values. Often this is used to give an increased contrast at low brightness – boosting faint detail – while giving up some contrast at high brightness. In that case, the advantage over a linear stretch is that the bright parts are not pushed into saturation, but still retain some contrast. Conversely, when curved the other way, the nonlinear stretch can also selectively increase contrast at high brightness at the cost of less contrast at low brightness.

Chicam has two utilities to convert FITS data into a common graphics format. One utility will combine three grey FITS images into a single colour one; this will apply a linear or logarithmic stretch. The other utility works on a single grey FITS image. It will apply two separate linear stretches: The first stretch, operating on the fainter parts of the input, gives a grey image; the second stretch, operating on the brighter parts of the input, gives a false-colour image. The first or second stretch can also be applied on their own rather than in combination with the other.

Unsharp mask

What the unsharp-masking process does, is to make a smoothed version of the image and then to subtract a fraction of that from the original. While small-scale structure is fully preserved, large-scale structure is partially suppressed. When done well, the result looks much crisper. Unsharp masking is not pure cosmetics. When imaging planets, unsharp masking is all but necessary to make visible the surface detail that has been recorded but remains hidden in the limb darkening of the planetary disc.

There are at least two parameters to tweak in unsharp masking. One is the width of the smoothing function, the other the amount of smoothed image to subtract. How exactly these two parameters affect the outcome is a bit hard to predict. It is best if you play around and get a feel for unsharp masking.

Chicam also includes a utility to apply an unsharp mask, working on FITS images.

Re-scale to different pixel size

Some images benefit from re-scaling down to a smaller size. This can make them appear crisper, such as hide the fact they are a bit out of focus. If there is no valuable information in the higher resolution, this also makes for a smaller file on disc and uses up less screen space, say in a web page. Sometimes the opposite is the case. An image of a planet has only 100 or 200 pixels, which may be only a tiny patch of colour on a big computer screen. Scaling it up can help. It may show detail that the eye loses when looking at the small image. Of course, in reality, no further real detail is created in the image.

You can of course scale any pixel size to any other pixel size. However, if you want to reduce the effects from interpolation of pixels, then you probably want to use only odd integer scaling factors when scaling up (i.e. 3, 5, 7, 9, etc.). Only with these factors will the new image contain all the original pixel values with new interpolated values added between. Choose other scaling factors and all new pixels are the results of interpolation of more than one old pixel.

When scaling down these factors are also best (1/3, 1/5, 1/7, etc.); even integers (1/2, 1/4, 1/6, etc.) are almost as good. With any integer scaling factor, the new pixels will be simple averages of several old pixels and no interpolation is necessary.

Visual analysis

Finally, the image is finished, and people will admire it on your website or when presented to your local astronomy club. But perhaps there is some information of interest in them as well, like how may spots the Sun has that day, or whether there is an aurora developing at the time. Such visual analysis is best carried out on a more or less final image, optimised for human viewing. This is different from quantitative analysis, which is better carried out on data after reduction but before cosmetic processing.