Image Processing By NikSzymanek

NikSzymanek, 11 February, 2005

Part 1
Scientific Image Processing

Introduction
The images taken with astronomical telescopes are saved into the FITS (Flexible Image Transport System) format. This is the standardised file format adopted by astronomers worldwide and offers unprecedented compatibility between image processing software packages
The CCD detectors in modern CCD camera’s are a remarkable piece of electronic engineering but even the most expensive CCD requires the use of calibration frames. Typically, these consist of the “dark” (or thermal) frame and the “flat-field” and these will be taken at the same time as the “light” frame. The high quality of the CCD’s in Starlight Xpress camera’s means that the level of dark current or thermal noise generated by the detector is extremely small. Flat field frames, however, will always show defects within the optical system of the telescope, caused by uneven illumination across the CCD typically caused by vignetting, filters etc.

Calibration
The first step is to “dark subtract” the light frame image. All camera control software contains this routine which consists of nothing more than the subtraction of the dark frame from the light frame. Obviously both frames need to be of equal duration and taken at equal temperatures otherwise the amount of dark current recorded by the dark frame will be different. I always recommend that the dark-subtracted file is saved using a different name to the raw file. This is a good practice as it’s very easy to accidentally overwrite the raw files. The next step is to flat-field the dark-subtracted file. The flat field frame is divided from the light frame and should remove all the dust and illumination problems, although some artefacts may remain. Once again the file is saved as a calibrated image. One important point to remember is that all exposures taken through colour or narrowband filters need to have their own flat-field frame taken and must be applied to the appropriate light frame at the calibration stage.

Scaling
Once the image is calibrated it can then be processed to reveal hidden features. The following routine is demonstrated using the processing software Maxim DL by Diffraction Ltd, but all image processing packages will allow scaling features.

This screen shot from Maxim DL shows a calibrated image of the irregular galaxy M82 in Ursa Major. The floating histogram menu at upper right shows a graphical display of the pixel values contained in the image. The triangular sliders are dragged to change how the image is displayed on the screen. The image below shows how the appearance of the galaxy can be dramatically changed, in this case to accentuate the outer areas of the galaxy. Unfortunately, by doing this we tend to burn out the central core region of the galaxy. This is known as linear scaling and can be used to good effect to feature certain parts of the image. In the case of Maxim DL, altering the sliders only affects how the image looks on the screen and does not alter the pixel values of the image permanently.


Conversely, the image can be scaled to preserve the detail in the core (which is most often the interesting part of the image) but at the expense of the outer regions, which may be lost. What we need is a processing solution that, rather than scaling
the image globally, acts on certain pixel values to enhance specific features of the image. Maxim DL contains just such a routine known as Digital Development Processing (DDP). The graphic below shows how it works.


The DDP graphic shows a selection of “transfer functions” that are used when processing images. The x axis shows the original pixel values of the image and the y axis shows the new values once the transfer function is applied. Obviously, the shape of the transfer function will dictate the numbers of the new pixel values. The gamma curve is utilised by many graphic packages such as Adobe Photoshop, which we’ll use in the second part of this tutorial. If we intend to gently brighten or darken images (not necessarily astronomical) it’s generally done using the gamma curve. Of course, astronomical images are very different to daytime shots and although, as we’ve already seen, linear or gamma scaling is useful, what we need is a way to selectively brighten images…particularly the interesting bits. Logarithmic scaling is a step in the right direction. A glance at the previous diagram shows a much more aggressive curve than the gamma function. This equates to a withholding of the brighter pixels (for example in the cores of galaxies or globular clusters) but not at the expense of the mid-grey levels (i.e. the outer spiral arms). By far the best of these transfer functions is the Digital Development Process. The DDP transfer function is a hyperbolic curve and sits between the gamma and logarithmic curve. It can be divided into three sections, each of which affect the end result of the processed image: the steeply rising section to the lower left is responsible for holding back the sky brightness, although it’s not visible on the diagram the curve is steeper than the gamma function. Next the DDP curve rises above the gamma curve and here it acts to enhance the middle-grey pixels in the image. Finally the curve levels off and drops below the gamma curve. It’s here that the image is tonally compressed, offering smoother images than the more-aggressive logarithmic scaling function. Rather ironically, the DDP transfer function was created by a Japanese amateur astronomer who intended the DDP routine to produce more “photographic” images. It’s probably become the most important processing tool to the CCD astronomer, certainly within the amateur realm. The image on the next page shows Maxim DL’s DDP menu.

This menu shows the Maxim DDP setup. In the small preview box is a representation of how the image will look once the DDP algorithm has been applied. Initially, the auto-scale routine will probably burn out the core of the object (typically globular cluster or galaxy images), but the main floating histogram menu can be used as for linear scaling to get an idea how well the DDP algorithm will work. The Zoom button allow you to get a close view of the image; conversely, zooming out shows more of the image extremities. At middle left is the “Cutoff” option, which controls the strength of the filter. The Cutoff is entered as a radius in percent of the image dimensions. Entering a smaller value produces a stronger filter. A strong filter is useful for removing large-scale brightness variations, while a weak filter is useful for emphasizing fine detail.

There are two other parameters that need to be set. Background should be set to the average background level in the image. This can be calculated automatically by selecting the associated Auto check box. Normally this is sufficient; however, the level can also be set manually or by using the mouse. Turn off the Auto check box to edit manually. To use the mouse, turn off Auto and click on Mouse. Point the mouse at a background area in the image (not the preview) and click the left mouse button. The Mid-level setting is more critical. It controls the "break point" in the transfer curve, and should be set to a middle level in the image. It can be calculated automatically, but using the Mouse input method is recommended. The best procedure is to activate Auto Preview mode, and click the mouse in various foreground areas of the image buffer until you are satisfied with the preview image. Note that the Mid-level setting must be higher than the Background setting.

I usually find that the auto setting for Background and Mid-Level works well. The Cut-Off option usually works best at values of between 1% and 10%. It’s definitely worth experimenting with different values. Once you reach a good value apply the DDP algorithm and experiment with the floating histogram menu. This only changes the screen values and doesn’t change anything on the image. As

before, be careful not to overwrite the original raw image. When I apply the DDP algorithm I usually rename the file to something like M82 Red DDP.fit, so you can see at a glance which file is which. Of course there are many other filter and scaling options within Maxim DL (and remember that DDP is a feature of many other processing packages, such as Richard Berry’s AIP for Windows and SBIG’s CCDOPS, it just seems to work best in Maxim!).

FITS to TIFF Conversion
Once these have been applied we now have to convert the FITS images into TIFF files that desktop graphic packages, such as Adobe Photoshop, can work well with. Photoshop can handle 16-bit FITS files (by installing a FITS image plug-in) but in my opinion it’s best to convert the files to 8-bit TIFFs. We’ll use Maxim to open the processed image and the floating histogram menu to carefully scale the image on screen. When converting these images remember that we are going from 32- or 16-bit files down to 8-bit which means that a lot of information can be lost. However, by scaling the images well we’ll retain all the useful information from the original image. The features that we need to retain are as follows (assuming that we’re working with a spiral galaxy image). Obviously the main interest will be the spiral arms and by carefully using the histogram sliders we can show these to good effect. The nucleus of the galaxy should be fairly small and not burned out…it may even show some interesting structure that wasn’t visible in the original image. It’ll probably be a trade-off how much detail is shown in both these features and it’s well worth experimenting with the sliders to get a good result. One final thing to consider is the sky background. It might be tempting to scale the image to show a jet-black sky but that’s not a good idea. It’s best to lighten the sky slightly (and it well show more of the galaxy) because the sky can be darkened later on during the “cosmetic” stages as required. If the sky is darkened too much here it will be a permanent change and we won’t be able to get that detail back.

Once the image is successfully scaled on screen we need to save the image to the TIFF format.

The Save As menu on the previous page shows some of the options available. All we need to do is change the file filter from FITS Images to TIFF Images. Below that is the Size Format, this is changed from 16- or 32-bit to 8-bit Int. Just below this is a File Detail box and it may show “Largest Pixel Value Exceeded”. If so, just tick the Auto Stretch box at lower left. And then click “Save”. The original file name will be retained but now with a TIFF extension. This procedure is applied to all images that will be colour combined and cosmetically corrected in Photoshop. It’s worth labouring the point once more that the conversion from FITS to 8-bit TIFF will lose information so spend time getting the image looking good prior to the conversion. Now we’ll look at the dark art of image compositing and cosmetic correction!

Part 2
Cosmetic Image Enhancements

RGB Colour Combining
Or more appropriately, BRV combining. Each of the colour images that make up a BRV set will have to be converted to 8-bit TIFF files for Photoshop (PS) or another graphic package, such as Photoshop Elements or Paint Shop Pro. Once the three files are open it will be necessary to copy them into the Windows clipboard and then paste them as a new “channel”. Here’s the procedure in detail:
Open the three RGB (RVB) files. Make the Red file active by clicking on the blue bar at the top of the image. Click SELECT from the top menu bar of PS and then click ALL from the drop-down menu. A line of “marching ants” will be visible around the edge of the selected image. Now from the EDIT menu click on COPY. This places the file in the clipboard. We now need to create a blank file for the three images to be pasted into. Click FILE, NEW and a menu will appear requesting the new file size. Just accept the values offered because they will be identical to the file that you copied into the clipboard earlier. You’ll need to make one important change to this menu and that is the MODE option. It will be showing Grayscale and you need to change this to RGB Color. Click OK and a new blank file will be created. At this point you need to be able to view the three separate channels of this image. From the top line click on WINDOW and then SHOW CHANNELS. A small menu will now be visible (below).

Click on the Red channel from this menu and click EDIT> PASTE. This will place the Red component from the clipboard into the red channel of the RGB image. Repeat this procedure for the V component but select the Green channel from the above menu first and then the same for the Blue component. The Channels menu will now look like this (see below). It shows the RGB image in the top line and each of the RGB files in separate channels. By clicking on these individually it’s possible to see how each of the files looks on its own. If you need to apply any effects to the images here’s where you select each one independently of the rest.

At this point it’s a good idea to save your file using something like M82RGB1.TIFF.
Probably the image looks OK but there is much that we can do to improve it. The first thing to do is look at the star images. The chances are they’re not in perfect registration and probably look something like this:
The stars are not in registration because of minor tracking errors on the telescope. This is easily corrected using the following procedure. Click on the Red Channel (see left) and then click on the top right button on the PS Toolbox. This is the MOVE tool and will allow you to then click on the Red component star and move it around. The idea is to move it until it is aligned with the other channels. In the picture of the misaligned stars above you can see that all three are out of alignment. By selecting the three channels in conjunction with the MOVE tool you should be able to get them in registration. The next step is to examine the colour balance of the image. In theory, if the separate RVB files are matched in duration to the colour sensitivity of the FTN CCD detector then no further colour balancing should be required. In reality, because we have scaled the three files (before converting to 8-bit) and particularly because we have applied a non-linear stretch using the DDP algorithm, we will have to do some colour manipulation. The easiest way is to use the LEVELS feature in PS. This is accessed from the IMAGE} ADJUSTMENTS} LEVELS menu.

Here’s what we’ll see on the screen (left). The histogram shows the distribution of pixel values in the image. Below that are three sliders. The middle (grey) slider is used to brighten the midtones of the image (it’s just our old friend the gamma curve), and is always a good starting point for tonal correction. To its right is a white slider and this is used to brighten the lighter pixels. At far left is a black slider; as you might expect it’s used to darken the darker pixels (such as the sky background). At the top of the menu is an option called Channel. By clicking on this it will expand to show the three colour channels, each with their own histogram. The sliders for each channel are manipulated in the same way to brighten or darken the image.

Once the large-scale colour balancing is done another option is to use the actual Colour Balance tool in PS. This is accessed by IMAGE} ADJUSTMENTS} COLOR BALANCE. Here’s the actual menu.

The three sliders manipulate data from the three colour channels as shown on the right-hand side. Make sure that the “Preview” box is selected so you can monitor the changes. The Tone Balance option at the bottom allows you to work directly on the dark, mid and light pixels. The Shadows option is very good for removing colour casts from the sky background. As with the Levels menu, it’s worth a little experimentation to get good results.

Another way to improve the image is to boost the contrast. This is done by going to IMAGE> ADJUSTMENTS> CONTRAST

The Photoshop Toolbox
Now we’ll have a look at the PS Toolbox in more detail (left). This is a floating palette and can be moved around the screen as required. As I said, the top right (cross and arrow icon) button is for moving the images around. Another useful item is the ZOOM tool at bottom right (magnifying glass icon). Select this by clicking on it and then the screen icon will change from a small arrow to the magnifying glass. Click on the image to zoom in and ALT-click to zoom out. Double-clicking on the Toolbox Zoom icon will revert the picture back to its normal size. If you need to remove parts of the image (borders etc) then the Crop tool is useful (it’s the third down on the left and is highlighted on the graphic at left). Click on the edge of the image and, holding down the left mouse button, drag a frame around the edge of the image. When you let go the frame remains but each of the four sides can be moved individually to change the crop frame size. To apply the crop, click on the arrow to the upper right of the screen in the area below the main Photoshop commands. There are several other tools found here that will be indispensable for improving the image. The fourth down on the left shows a diagonal bar and is known as the Healing Brush. It’s an incredibly powerful feature of PS that will allow us to remove many of the cosmetic defects. Below that is the standard Clone tool, also useful for cosmetic corrections. The seventh down on the right is the desaturation tool, which is used to remove colour from objects, and is very useful for dealing with colour-saturated stars.

One thing to be aware of here. Virtually all of the processes we’ve looked at up to now involve “global” processing (i.e. lightening images, color combining and so on). The next batch of techniques involves cosmetic corrections that will invalidate any scientific data in the images insofar as what we are doing is correcting the image to give it an aesthetic quality that may not have been present in the raw frames, and nothing else. With care we can preserve all the relevant data of the target image but it will be necessary to replace defective pixels and make corrections using data not contained in the originals.

This picture of the spiral galaxy M82 shows some of the defects of a raw image that can be successfully removed. First of all let’s address the vertical lines or “column defects”. The first of these, the white line on the right of the image is quickly dealt with using the Crop tool. This defect lies outside the area of interest and can be easily removed. The next column defect is visible to the right of the bright star at the bottom of the frame and consists of a dark line of pixels. This can be removed in the following way using the Selection tool (the top left icon on the toolbar, see previous page). Click on the Selection tool and draw a very thin selection box along the column defect. It only needs to be big enough to cover the defective pixels.


The image above shows and expanded view of the selection line around the defective pixels. What we are about to do is move the selection box to an area of the M82 image where we can extract a blank piece of sky to use to cover the defect. Click within the selection box and drag it to an area where the sky background is a similar brightness and let go. Next, hold down the Ctrl and Alt keys on the keyboard and drag the selection box back over the column defect. You will see that this time we are copying a piece of the sky in its place! Let go and then go to SELECT>DESELECT, from the top line of commands and the job is done. To make this successful you must use an area of sky with the same tonal values as those around the defect.
Next, we’ll see how the Clone Tool (or to give it its correct name, the Rubber Stamp Tool) can be used to repair small pixel defects. Most telescopes placed at high-altitude sites will have images that contain cosmic-ray impacts on the CCD detector. These look similar to the line of pixels in the image at left but will probably be slightly wavy in appearance. There will also be “hot” pixels that saturate quickly and mimic the appearance of stars (as in the right-hand image). The Clone Tool allows us to “paint” over these by sampling from areas of similar tonal value. Click on the Clone tool (it’s the fifth button down on the left-hand side of the PS Toolbox). We will use different Brush sizes when we are sampling these areas; when working with minor pixel corrections we only need to use a small area or brush size. Once the Clone tool is selected you should see an empty circle on the screen as you move the cursor about (if not, go to EDIT>PREFERENCES> DISPLAYS & CURSORS and select “Brush Size” and “Precise” from the Brush options). The Brush size can be altered using the {[ or }] keys on the keyboard. Next, move the cursor circle to an area of sky that is similar in tonal value to the sky background around the hot pixel or cosmic ray defect and press the Alt key on the keyboard. The cursor will change to a kind of cross-hair and then click the left mouse button. From this point on when you click and drag the cursor the area under the circle will be replaced with pixels from where you clicked the cross hair. A very nice touch is that as you move the cursor the sampled area moves in the same way. In this way you can quickly paint out the defects leaving an unblemished sky background. Just carry out same procedure for all the cosmic ray or hot pixel defects, remembering to set a new sampling point for each one. The Healing Brush tool (fourth down on the left), works in exactly the same way except it’s a new feature to PS and can be thought of as an “intelligent” clone tool that can analyse the tonal values around the rogue pixels and replace them with a good representation of an average value. This tool should be used in preference to the standard clone tool but be careful because it can be fooled when working close to the edges of images. If this happens, switch back to the standard clone tool.

Now we’ll see how to remove unsightly haloes and blemishes around stars, using the Desaturate tool. This is the seventh button down on the right side of the PS Toolbox, with the icon looking like a sponge. At this point it’s worth mentioning that some of the buttons on the Toolbox have multiple options (this is shown by a small black arrow on the button). To choose the correct one just left-click on the button and drag the mouse to the side and the options will spring into view. Click on the correct one to make it active. Once the Desaturate tool is active the cursor will once again turn into a circle, indicating the size of the brush selected. As before this can be made bigger or smaller using the {[ or }] keys on the keyboard. Select a brush size that will just encompass the coloured halo around the star or the whole star if it is displaying an objectionable colour cast. By left-clicking on the star the colour can be made to desaturate. This is best done in gentle stages using multiple clicks rather than dragging the cursor around the star’s edge. If you make the brush size too big then you’ll drain away the colour from the sky background, which will look unsightly, so keep the brush size to a minimum for the job.

History States
One of Photoshop’s most powerful features is that it saves any changes you make to an image in a “History” step. If you go to WINDOW and click on History, a small menu will appear on screen that shows these steps in a kind of ladder arrangement. Every time a change is made to an image a copy is saved to the history state menu. What this means is that you now have the ability to go back to any of the previous history states and view them if you are unhappy with any of the changes you’ve made to an image. This is very useful but it gets much better.

Photoshop has a powerful tool called the History Brush (it’s the fifth button down on the Toolbox). It works in conjunction with the History states menu in the following way. Suppose we have been working through an image making corrections and decide that the image isn’t looking too good. We could just click on an earlier History state and revert the complete image to that. Supposing, though, we were very happy with part of the image and didn’t want to lose the work done on it (for this example we could say that we’ve processed a Faulkes Telescope image of a galaxy and are happy with how the spiral arms of the galaxy look but not the nucleus). Fortunately, this is where the History Brush is so useful. On the graphic at left, which shows the History Menu, you can see that several different processing stages have been applied. By clicking with the mouse on the left-hand column a brush symbol has appeared. Now, if we paint on our image the areas where the cursor moves will be replaced by pixels from this earlier state (or whichever state we choose). The work done from the Blur tool to the Line tool can be quickly and easily painted away. As before, the brush size can be altered, and there are several further refinements that go beyond the scope of this introduction, but the flexibility of this tool is incredible. To give another example, supposing we have opened our galaxy image and decided that we wish to improve the contrast. This is easily done using the IMAGE>ADJUSTMENTS>BRIGHTNESS&CONTRAST sliders. Once applied, the spiral arms look much better but the core of the galaxy now looks “over-exposed” or burnt out. No problem. All have to do is click back on the History Menu and select a state before the contrast boost was applied. Then use the History Brush to gently return the centre of the image (i.e. the nucleus) to its previous state. Of course, it takes a bit of care to gently blend in the correction as we don’t want to see a sharp cut-off, but generally it’s quite easy to achieve. Similarly, if you’ve processed the galaxy but the sky is now too bright / dark, you can use the above procedure to return to an earlier state to correct this. Once this process is mastered it will be one of the most valuable processing aids in Photoshop.

Bits & Pieces
Here are a few other routines that work well. Supposing you have just combined three BRV images and you notice that the sky background is a bit noisy. Examine each of the three colour channels (see the Channels Menu). You’ll probably find that one or more might have quite a noisy background (depending on the exposure time). A good way to correct this is FILTER>NOISE>DESPECKLE. There’s no control over the strength of the filter but it can be applied as many times as needed. It’s just a very gentle blurring filter that smoothes the appearance of “salt and pepper” pixel noise. It’s OK to apply it to the BRV image as a whole but I find it works best just to apply it to the individual colour channel that is most noisy.

Worth knowing is that Photoshop also has a very useful FADE command. Suppose you apply, for example, the despeckle routine explained above but feel that it’s just a bit too strong. Click on the EDIT menu and the fourth option down will be FADE despeckle. Whatever the last action you applied will be shown after the FADE command. If you adjusted the LEVELS sliders you would see FADE Levels in the EDIT menu. What’s happening is that you are being offered a chance to reduce the effect applied. The FADE menu will bring up a slider (see below). If you drag the slider to the far left the image will revert back to before you applied an effect. If you drag the slider to the right (i.e.100%) you get to see the image after the effect is applied. But now you have the option to move the slider anywhere between the two extremes and Photoshop will gently merge the before and afters to produce a gentler version of the effect. Remember, this can be applied to any change you make to an image (contrast, levels, colour balance etc). Also, make sure that the Preview box is ticked so you can see the change to the image as you drag the slider.

Another useful technique is to selectively work on part of an image. Once again, suppose we are processing an image of a galaxy. The nucleus and spiral arms are well processed but the background is slightly noisy. If we wanted to sharpen the image it would work well but perhaps would make the background a bit noisier. However, we can select just a part of the image using the Lasso tool (it’s the second one down on the left of the PS Toolbox). You can draw a freehand selection with the mouse to encompass the area of interest. You’ll see a line of “marching ants” around the selected part of the image. Now, any filters that you use will only be applied inside this selection, leaving the sky background untouched. We can improve this method by softening the edges of the selection as we don’t want a sharp line of demarcation where the filter has or hasn’t been applied. For this we need to go to SELECT>FEATHER, once the selection is made. The sub-menu will ask for a feather radius; this just dictates how gentle the feathering will be at the join. Small values will hardly be noticeable so it’s best to use values of around 30 to 50 but this will depend on the size of the selection. In this way, even if a fairly strong filter is used it will gently fall off around the selection edge and shouldn’t be too noticeable.
Probably the best filter to use with this method is the Unsharp Mask, FILTER>SHARPEN> UNSHARP MASK, a very sophisticated routine that is easily controlled by the user. The menu shows a preview of how the image will look with the effect applied (the Preview box must be ticked). The Amount slider dictates the strength of the sharpening filter. Over-sharpening will make the image noisy. The Radius slider is best left on a value of 1 for deep sky images. For planetary images it’s worth experimenting with higher values to bring forth details in the cloud belts of Jupiter and Saturn.