Chapter 3, Aligning and Stacking Images




Hopefully you've had the patience to read the first two chapters in this series...so you have a handle on getting your equipment set up and polar aligned, you've got a feel for dark frame and flat-field calibration, and you're ready to make some great images. The next step in going from raw to finished images is aligning them, and stacking them. First, though, let me make a brief foray into the logic behind doing a bunch of images and stacking them together in the first place...
Let's start with an example. You want to take an image of the Horsehead Nebula region. You've got a monochrome CCD camera, and you're shooting from a moderately light-polluted home backyard, so you've decided (quite logically) that you'll minimize the light pollution by doing an LRGB image -- that's an image composed of four seperate parts, a luminance image to define the detail and brightness levels, and three seperate filtered images each shot through Red, Green and Blue filters to give the color component of the final image -- and you're going to shoot the L image through a narrow-band H-Alpha filter. To assemble the final color image, you'll need to have those four component images, one each for L,R,G, and B. You've seen other people's pictures on the web of this area, and the information they provided with their images usually says something like, "LRGB image, 120:60:60:60 minutes, luminance through Ha filter." And, since you know that the longer you expose the better your signal-to-noise ratio will be, logically you might conclude that you should just shoot one 120-minute exposure for the luminance part of your image. Is this a good idea?
Probably not. A much better approach would be to take 12 10-minute expsoures, and align and stack them. Why? There are lots of reasons. Here are a few:
-- If a satellite passes through your 120-minute image, it may be ruined. If one passes through one 10-minute image, you've only possibly lost 10 minutes (and probably haven't lost even that, I'll explain later).
-- Your CCD camera will probably COMPLETELY saturate most of the stars in the image during a 120-minute exposure. Remember the histogram discussions in Chapter 1? All of the stars in your image would hit the 16-bit 65,535 value maximum, and they'd all be completely white. You would not see any star color in your final image.
-- A cloud could sneak by, an airplane could pass through, your cat (or you!) could bump the mount...and just like with the satellite, the entire 120-minute exposure would be ruined. Shorter ones mean you only lose a little of the total time. -- You may have to go even shorter than 10 minutes...if the mount you're using can only accurately track for 2 minutes before star trailing shows up in your images, then you should do a bunch of 2-minute exposures and stack *those* together. That will give you a bunch of well-tracked images to work with, rather than longer images with more signal but poor tracking.

There are other reasons, but I'm sure you get the picture! Taking a series of shorter exposures and putting them together later into one "master" image is standard procedure for CCD imaging. Pretty much all of the software used for capturing or processing images has built-in functionality to make it easy to align and stack images together. So what you should be aiming for in the exposure time for any single image of a set that you'll combine later is simply:

Expose for as long as you can without saturating bright stars and within your mount's ability to track accurately.
Do as many of those exposures for each component (LRGB) of your image as you can within your time constraints.

Remember, the more total exposure time you have, the better your signal-to-noise ratio will be, and the better your images will look. Also, keep in mind the object you're imaging: objects will both very bright parts and faint details (like nebula M42's core, many galaxies with bright centers and faint arms) can be problematic, as an exposure long enough to show the faint details will saturate the bright parts, and you'll lose all the detail there. In Chapter 8 I'll show you some neat tricks you can use to get both the bright parts and faint parts in one image, and to do that you'll need two completely seperate exposures, long ones for the faint detail and short ones for the bright parts. Worry about that later, for now expose as long as you can without saturating the brighter stars in the image and within your mount's tracking abilities, and do as many of these individual exposures as you can. Your time constraints may impose additional restrictions -- an object you want to image might only be visible in your yard through the trees for an hour -- in which case you will need to adjust your total times. You also might want to hope for a few clear nights in a row, and do an hour's worth of luminance one night, an hour of red the next night, and so on. Splitting the total exposure over multiple nights is just fine, as long as you can keep your scope set up all that time to make aligning the images easier!

Aligning Images

So you've worked out all the variables mentioned above, and you've shot a hard drive full of LRGB images. The first step in putting together each of the individual component final images is to align them. Even with a wonderful mount that tracks well, near-perfect polar alignment, and great seeing there will still be a bit of variation in the position of stars and objects in your images. With less than perfect conditions and equipment (where most of us usually find ourselves), there may be considerable drift from image to image. In order to stack the individual images so that they sum up properly, the set of images has to be aligned so that they're in exactly the same pixel space. For example, here's a set of images from a typical night for me:


On the left is the first image of 20, each 3 minutes, of M42. In the middle, the 20th image.
Although I had good polar alignment and excellent tracking (no trailing visible in either
of the individual images), there was still drift from the first to the last image. The
image on the right shows the first and last images overlaid, with no alignment, showing
how much drift there was between the first and last images in the set -- and why the image
set needs to be aligned!


If I simply stacked all 20 of these images together without aligning them, this is what I would wind up with:


The same section as above, with all 20 3-minute exposures of M42 summed
together with no alignment...Can you say "oval stars"? I knew you could.

OK, you probably get the point -- you need to align your images. HOW you align them depends mainly on the software package you're using. I'll go through the steps to align a set of images using MaximDL/CCD, the package that I use to capture, align, and stack images. The exact steps will vary depending on your software, but the general steps will be the same.
First you need to load all the images that make up the set you want to align. Experienced imagers at this point are probably muttering to themselves, "no you don't, my software (including MaximDL/CCD) can align them without loading them." Yes, you can do that. However, especially for beginning imagers, I'd advise loading all of the files into the program you're using to align. This lets you visually inspect each of the images, and if there's too much trailing in one, or a cloud that passed through, or some other problem, you can reject that image and not include it in the set. DON'T reject images because of satellite or airplane trails, though -- I'll show you down below in the stacking section how you can include those images without the problem parts! So, load the images, check them out to see if they're good images, and toss out any unusable ones. DO NOT apply any permanent histogram stretching, though. Leave the histograms alone! If you've calibrated your images with darks and flats, be sure you're using the saved calibrated images rather than the raw images. Once you have the set you want to align loaded and ready to go, click on the "Align" menu option in MaximDL/CCD (under the "Process" menu. You'll get a dialog box that looks like this:

MaximDL/CCD's dialog box for adding images to a set to be aligned

If you've done as I suggested, and loaded all the images then closed the "bad" files, you can hit the "Add All" button to add all of the images in memory to the set to be aligned. If you kept the bad images open, then click on the files you want to add on the left to select them, then click on the ">>" button to add them to the set. Once you've added all of the images to be aligned, click on "OK", and you'll get the next dialog box:

MaximDL/CCD's image alignment dialog box

There are lots of options here, and the options in other software are similar, so let me go over a few of them:
Align Mode: this lets you choose how the software will align the images. There are "auto" options, where the software attempts to automatically figure out how to align the images, and "manual" options, where you have to do some of the work yourself. In Maxim the full list of options are:
-- None: not very useful for this purpose, but there because this dialog box is shared as the "combine" dialog later.
-- Auto - correlation: this only works if the images are close to being aligned already. Shifts only, no rotation.
-- Auto - star matching: the best method for deep-sky images with stars in them. Matches up stars in the images, and will both shift the images and rotate them to align them. It can fail if the images are wildly different, but usually does a great job.
-- Manual 1 stars: Maxim will show you each image in the set in turn, and you click on one star (the same star in all the images) to align them all on that star. Does shifting only, no rotation.
-- Manual 2 star - as with the previous method, you get each image in turn and click on a star, but you'll do this twice -- once for the first star, and once for the second star. This method will both shift and rotate to align the images. Sometimes your only option if Auto Star Matching fails.
-- Overlay: This mode colors the reference image green, and the next image to be aligned purple, then shows them both at the same time. You use the arrow buttons in the dialog box to shift and rotate the image until the two colored images line up perfectly. I personally use this method for aligning planetary images where there are no stars, where it works very well. Not nearly as good as other methods for deep-sky images with stars in them.

Set as Reference: This lets you choose which image is used as the "reference" image that all of the others are aligned to. I like to use the last image in the set. No real reason :)
Reject Image: Hitting this button while a particular image is displayed removes it from the set of images to be aligned. You can browse through each image in the set using the "Next Image" and "Previous Image" buttons.
Overlay All Images: once you've chosen the images to be combined, and the alignment method, hit this button and Maxim will give you a "preview" of the alignment of all the images. It's a good idea to do this before hitting the "OK" button, since occasionally the auto methods will fail, and if you see a misaligned preview after hitting this button, you can change your alignment method to one of the manual modes, and do the alignment yourself.
Bicubic Resample: When an image is shifted and/or rotated in order to be aligned, the software has to resample the image so it can be properly placed at the correct 2D spatial location in the pixel grid. Maxim can do this using a simple bi-linear interpolation, which is faster, or a more computationally expensive bicubic resampling algorithm. Bicubic takes longer, but produces better results. Unless you're in a really big hurry, leave this box checked.

That's basically it: choose the images to be aligned, set a reference image, choose the alignment method, preview it to make sure the alignment is good, then hit OK. Each of the individual images will then be shifted and/or rotated so that they're all aligned with the reference image. Once the alignment is complete, you should once again save each of the individual images back to disk with a new name, indicating that they've been aligned. If you're going to combine them right away you can skip the re-save, but if for some reason you want to go back and start over it's nice to have the aligned files ready to go.
Maxim and most other programs can align images at the same time they're stacking (combining) them. I personally think it's a good idea to do the alignment step seperately, save the aligned images, and give yourself a set of aligned images that you can try different stacking methods with (see below). If you know in advance you're going to do a median combine, you can save yourself some time by doing the align/combine at once. However, if you do decide to try another stacking method, you'll have to repeat the load, align, combine, etc. steps all over again. Since I love to experiment a bit with all my image sets, in the long run doing the align seperately makes sense. It also is the only way to go if you're going to use one of the Sigma combination programs or plug ins, which I highly recommend (again, see below!). In that case you MUST align the images first, and you're probably going to have to save them to disk. Yes, this all takes lots and lots of disk space for the groups of raw images, the calibrated images, the aligned images, etc. Think big hard drives for your computer.
There's one final point about aligning your images that needs to be discussed. Since the images have to be shifted and/or rotated in order to be aligned, it's possible that the resampling to move them around could result in some unwanted, slightly noisy artifacts. Features in the image that had their own pixel to themselves before aligning could wind up spread over two pixels, in which case the software has to do the best it can to resample across different pixel boundaries. One way you can minimize the impact that all this shifting, rotating, and resampling has on your aligned images is to resample them manually before aligning and give the alignment routines more pixels to work with. This means resizing the images before alinging them so that they're twice as big as the raw images, doing the alignment (and stacking), and then resizing them back down to their original size. Some imagers report greatly improved results with a variety of software packages when doing the alignment on resized, double-size images. I've done some tests myself, and there's no doubt that there are sometimes subtle differences that show the benefits of this approach. However, if you have a megapixel or greater CCD camera, and are shooting 20 or 30 images per set to be aligned and combined, these double-sized image sets are going to be HUGE. They'll take up all of your computer's memory and then some, they'll take 4 times as long or more to align and stack, and they'll use up lots more hard drive space. Whether or not that extra time and hassle is worth it to you is your decision. I'd encourage you to experiment and decide if it's something you want to do. For myself, I do re-sample the raw images to twice their size occasionally -- ALWAYS on planetary images, and occasionally on deep-sky images when I've got a set taken under really good seeing with very fine detail. On most normal sets, however, I just leave 'em as is.

Stacking Images

It's finally time to stack all of those lovely calibrated, aligned images together to make one "master" image that will be used for the processing to come. Before we get to the options for stacking images, let me briefly cover *why* stacking images is a good idea in the first place...
The goal we're aiming for is to have images with as much signal and as little noise as possible. Noise in the images comes from the electronics in the camera (dark current, which can be dealt with by dark frames, readout noise which is always present), variations in the sky background and light pollution, and from the statistics of capturing photons using CCD detectors. Longer exposures have more signal and less noise than shorter exposures in ALL cases, but for reasons discussed at the top of this page doing longer exposures is not always a good idea. By combining (or stacking) a series of shorter exposures, we can get nearly the same result.
One of the reasons this is possible is thanks to the mathematics of noise. The noise component of images is, by nature, mostly random. You can see this in your raw images if you (temporarily) stretch them so you can see parts of the background or other dim parts of the image. Look at two different images of the same object with the same exposure time, and the grainy background will seem to shift and shimmer from image to image. That fluctuation shows up because noise is random -- if it were the same in each image, it would be *signal* and not noise! When images are combined, the mathematical rules say that the noise adds linearly, while the noise adds as the square root of the number of images. More simply, if you combine 10 images you get 10 times the signal and only 3.16 (the square root of 10) times the noise. Combine 20 images, and you get 20 times the signal and only 4.5 times the noise. As you can see, the more images you add together, the faster the signal increases while the noise component increases much more slowly. That's the magic of image combining. While a combination of 20 one-minute images will never have as good a signal-to-noise ratio as a single 20-minute image, it'll have nearly identical signal and only about 4 times the noise. For all the benefits shorter exposures give you, outlined above, it's a really good tradeoff!

There are four methods you can use to stack a set of images. Each of them have the same mathematical properties described above with regards to signal versus noise. However, each of them produces slightly different results. The four methods are:
Sum: Adds the pixel values linearly for the pixel value in the combined image.
Average: Takes the straight average (sum of all pixels divided by number of images) for the pixel value in the combined image.
Median:Takes the median value of the pixels in the set for the pixel value in the final image.
Sigma:An adaptive method; Finds the Median value for each pixel as above. It then goes through image by image, and if a particular image's pixel exceeds the median by an amount you specify, that pixel is replaced by the mean of a group of surrounding pixels before contributing to the final image. Combines the best of the Sum and Median methods.

Why four different methods and what do they to do your images? Consider the four images below. These are stacks of the 20 M42 images referenced above, combined using each of the four methods. I purposely did NOT calibrate these images so that some hot pixels in the raw CCD images would be in the stack for combining. These are 200% magnified views of a small portion of the image:


Clockwise from top-left: Sum, Average, Median, Sigma combined

Each of the combined images was loaded into PhotoShop, had the levels set exactly the same, magnified, and then saved out. No other processing was done. Remember the slight drift from first to last image in this set that I showed in the alignment section above? The hot pixels from the CCD chip show, because of this drift, as streaks going from top right to bottom left in the combined images.
Those hot pixel streaks are plainly obvious in the Sum and Average combinations -- they stand out like a sore thumb. The same would be true of those artifacts if they were satellite trails, airplane trails, etc. Both of these combination methods simply lump all of the data from all of the images, including the hot pixels, into the final combined image. In the bottom-left image, however, they're hardly visible at all. This is the median-combined image, and this combination method did a great job of recognizing these anomalies that were at slightly different positions in each of the images in the set, and eliminating them from the final image. Now look closely at the bottom-right image, using the Sigma combine method. The hot pixel streaks are much less visible than in the sum or average images, though they are still visible. Compared to the median image, however (which completely eliminated the streaks), the Sigma image shows sharper detail -- especially in brigher/darker edge boundaries. Because these areas didn't have anomalous pixels, they didn't get "smoothed" as they did in the median image. The Sigma image retains nearly all of the sharpness of the sum method, yet considerably reduces artifacts as with the median method. In truth I *could* have eliminated the hot pixels completely from the Sigma image by choosing a different Sigma value (I used 2.5 for this combine), but for this example I wanted to show that this method gives you control over how much anomalies are reduced in the final image.
So if Sigma is so great, what's the downside? The biggest downside right now is that none of the popular astro-imaging software has this combination method built-in. That includes MaximDL, AstroArt, CCDOps, etc. There are two ways to do Sigma combining right now: one is to download Ray Gralak's free Sigma program (which you can get at this link). Ray's program requires you to align the images and save them to disk, then combine them in his Sigma program (see why I advised doing an align-only above? ). The other way is, if you have MaximDL/CCD, to get Russ Croman's free Sigma-combine plugin (which you can get from this link). Like Ray's program, Russ' requires that the images be aligned first, but if you have MaximDL/CCD it runs as a plug-in to that program, and you can load the images, align them and keep them in memory, then do the Sigma combine. Both produce excellent results, and from every test I've done produce combined images with better signal, less noise, and fewer artifacts than any of the other three methods.
Once you have your combined "master" image, no matter which method you used to combine the image set, save the image to disk with a new descriptive filename (I usually use something like "m42sigsumraw.fit"), WITHOUT TOUCHING THE HISTOGRAM. Yep, here I go again, harping on that histogram thing. Feel free to use a temporary visual stretch (like MaximDL's "Screen Stretch") to visualize how the data in the combined image looks, but SAVE THE RAW UNSTRETCHED IMAGE NOW. You'll be really glad you did later.


It may seem counterintuitive, but the image you want to save after combining is the one on the left...NOT the one
on the right! The unstretched image on the left still contains ALL of the data of the combined images, and you'll
be able to bring it out later. If you stretch the image now, like the one on the right, and save it you will have
lost data that you'll need later. You've come this far leaving the histogram alone, don't give up now!

A couple of additional notes on combining that are relevant...
Remember that the Sum method literally adds the pixel values from each image at a certain pixel to get that pixel's value in the final image. If you're working with 16-bit images, and there's a bright area in each image that already has a pixel value of, say, 40,000 then you may have a problem. Adding just two of these pixels together gives a value of 80,000 -- which is more than 16-bits can represent with its maximum value of 65,535. To properly handle summing 16-bit images, a software package has to use either 32-bit integer values (which have their own, but much higher, number limits) or use floating-point math to do the sum. MaximDL/CCD and AstroArt 3.0, as well as CCDSoft, do floating point arithmetic for sums. Earlier versions of AstroArt before 3.0, AIP4WIN, and some other packages still use 16-bit integer math to do image sums. What happens in those programs is that when a pixel's value while doing the sum goes over 65,535 it's "clipped" to that number -- no matter how many more pixels you add, the value stays at 65,535. This has the effect of throwing away lots of your hard-earned data, and is a very bad thing. If you have a software package that does NOT do floating point sums, you should NOT use the sum method. Use median combines, or align the files, save them out, and let Ray's stand-alone Sigma program do the job for you. The same applies to the average method in those packages -- the final, averaged image in every package will have values at or below 65,535 for the pixels (since it's the average of pixels at or below that value), but if the software does the sum it needs to do to compute the average in 16-bit integers the same clipping results. 800,000 divided by 20 (the sum of 20 40,000 value pixels) is 40,000. 65,535 divided by 20 is 3,276. The first value would be correct for an average of 20 pixels each having a value of 40,000...the second value is what you'll get from software that does the background sums as 16-bit integers with clipping. Beware.
A final point is that even if your software DOES do floating-point sums, be careful how you save the image to disk after finishing the combine. The FITS format supports lots of data types, and if you save a FITS summed image that's in floating point format as a 16-bit FITS file, say goodbye to a bunch of your data. MaximDL (and possibly other programs) warns you about this by telling you that the format you've chosen can't support the range of data in the image, but it also allows you to stretch the image so the data will fit. DON'T DO IT. Save summed 16-bit images with the IEEE floating-point data type, or you'll lose important data for later on!

Next chapter you'll finally get to play with the histograms and stretch the images, I promise!

Tutorial Introduction
Chapter 1: Gathering and Preparing the Image Data
Chapter 2: Calibration -- Darks and Flats
Chapter 3: Aligning and Stacking Images
Chapter 4: Maximizing Your Data -- Histograms, Stretching, Contrast, Brightness, Gamma
Chapter 5: Dealing with Imperfections and Artifacts
Chapter 6: Basics of Color Images
Chapter 7: Advanced Color Image Processing
Chapter 8: Advanced Techniques -- Masking and other tricks
Chapter 9: Summary and Final Thoughts











All text and images Copyright (c) 2003, Paul LeFevre
Mail me with comments & criticisms!