Light Vortex Astronomy
  • Blog
  • Astrophotographs
  • Articles
  • Tutorials
  • Links
  • About Me
  • About e-EyE
 ​Send a Gift via PayPal:

Remote Hosting in Spain:
Picture

 

Tutorial (PixInsight):

Stretching Linear Images to Non-linear


As they are captured and even after pre-processing, astrophotographs are generally very underwhelming. The highly faint nature of our targets makes the images almost entirely black, bringing to question why we even bothered to begin with. As a rule of thumb, all astrophotographs receive some post-processing at least and the major step in this procedure is stretching the images. What begins as a linear image (called as such as it is fully representative of what was registered at the camera's sensor) is pulled apart to strongly enhance the contrast between neighbouring regions of brightness. This produces a non-linear image as it destroys the original representation of what was registered at the camera's sensor, but produces what we deem a viable astrophotograph worthy of our time spent exposing. 

PixInsight has a number of processes for stretching linear images to a non-linear state, which can then be post-processed further. These are AdaptiveStretch, AutoHistogram, HistogramTransformation and MaskedStretch. HistogramTransformation is considered PixInsight's main process for this task, but the others are covered as well for completeness. 

Assumed for this tutorial:
  • Knowledge of operating PixInsight, related to dealing with images and processes (read this, sections 3 and 4).
  • Your images have already been pre-processed fully (read this).

Please feel free to ask questions or leave comments via the comments section on the bottom of this page. 

Contents
1. Stretching using AdaptiveStretch
2. Stretching using AutoHistogram
3. Stretching using HistogramTransformation
4. Stretching using MaskedStretch
Comments

 

1. Stretching using AdaptiveStretch

​​AdaptiveStretch is a very simple-looking process in PixInsight, which aims to enhance contrast in areas of an image above a defined noise threshold. Below shows the process open alongside a currently-linear colour RGB image. 
Picture
To get started with AdaptiveStretch, we open up the Real-Time Preview window for it, which gives us a real-time view of what us changing settings is going to do to the image if we click Apply. We also enable Real-time curve graph to see what the stretch looks like for the parameters set. 
Picture
​For the stretch to be appropriate, we should enable Contrast protection, which will aim to protect bright areas of the image from losing contrast when they are stretched. This is done by clicking to enable the checkbox next to Contrast protection​. 
Picture
​Enabling Contrast protection will likely change your real-time preview view of AdaptiveStretch ​(as it did for me, shown above), since the parameter is now introduced into the stretch. We start by setting an appropriate Exponent for Noise threshold, which defines the magnitude of the noise. For most images, it appears -4 is a good starting point. Following that, we can adjust the slider to set the precise value we want. The real-time preview is monitored closely while we adjust. 
Picture
We have now defined where the noise lives within our image, which tells AdaptiveStretch not to stretch it, but rather to attenuate it somewhat. This appears to make the background darker while bringing out the detail in the stars and nebulosity in the image. The current stretch may not look ideal, so we now adjust Contrast protection similarly. With the Exponent at its default of -2, we adjust the slider to see how changing it makes changes to our image. 
Picture
Clearly increasing the value of Contrast protection slightly has added extra contrast protection (as expected), which yields a better contrast on bright areas such as stars and nebulosity. If we increase this value too far, the stretch appears to be lost entirely. 
Picture
Remembering that Exponent is basically a power of ten, we note that reducing the Exponent to say -3 will give us more fine adjustment. The larger the Exponent, the more abrupt the changes made given the value set on the slider. Therefore, too low an Exponent may yield unnoticeable changes when you adjust the slider. I find the default of -2 gives ample adjustment to the resulting stretch, but you may wish to back down the Contrast protection Exponent to -3 (or go for -1 if you need more severe Contrast protection​). 

The values of Noise threshold and Contrast protection currently use the entire image as a metric to define how the stretch will be applied (according to the values set). We can however define a small area within the image to act as the metric for AdaptiveStretch. This is done by creating a preview box around any desired area around the image, enabling Region of Interest, clicking the From Preview button, selecting the preview box created from the list and clicking OK​. 
Picture
Picture
It obviously helps to use PixInsight's autostretch function to see where you should define your preview box. It is generally a good idea to define the preview box around an area containing diffuse nebulosity alongside bright nebulosity to about the same extent. Enabling the Real-Time Preview now yields a slightly different stretch preview to what we had before, despite having the same parameters. This is simply due to the fact that the preview box area is now the metric for defining what kind of stretch will be applied. 
Picture
The task is now again to balance a choice of Noise threshold and Contrast protection that yields a good stretch that keeps the background at bay but stretches your image's interesting features adequately without overdoing it. Noise threshold is generally adjusted first in order to give a good background and then Contrast protection is adjusted afterwards to yield good contrast in the stars and nebulosity. 

Remember that defining a preview box may not necessarily be a good idea as it could restrict your stretch in a particular way that it does not yield a good stretch for your image as a whole. Redefining the preview box used as Region of Interest may be necessary, or using the entire image as a metric (disabling Region of Interest), as was default. In my particular image's case, I got the best results by not using a preview box at all, so I disabled Region of Interest and proceeded to adjust Noise threshold and Contrast protection from there. 

Below shows the best result I could achieve using AdaptiveStretch​ with the entire image used as metric:
Picture
My personal opinion of AdaptiveStretch is that it does not achieve anywhere near as good results as other processes, or even PixInsight's autostretch, which generally looks a lot more natural. Still, if you achieve an acceptable result with AdaptiveStretch, simply click Apply and you may then close both the Real-Time Preview window and the process window itself. 
 

2. Stretching using AutoHistogram

​AutoHistogram is a very capable process, though it lacks a Real-Time Preview function to monitor what the adjustments are really doing to the final result (before applying its parameters, that is). Below is the same image in its linear state, with AutoHistogram open next to it. 
Picture
​The AutoHistogram process aims to produce a specific Median pixel value across the entire image. If the user is dealing with a colour RGB image, as I am above, you may select Individual RGB/K channels to adjust each colour channel separately, though doing this is not common when the image has already gone through a colour calibration procedure (which neutralises the background as part of it). By default, AutoHistogram works in Joint RGB/K channels mode, to adjust the entire image by the same stretch (much more commonplace). 

​By default, most images will have a Median value lower than AutoHistogram's default 0.12000000 setting. We can check this by opening the Statistics process and selecting the image from the list, with Normalized Real [0,1] selected as well. 
Picture
​In fact, if we click the Set As Active Image button on AutoHistogram while our image is selected, it will take the current Median values as read by the Statistics process. Since the image is detected as a colour RGB image, Individual RGB/K channels​ mode is set automatically as well. 
Picture
​We can then force ​AutoHistogram to work with only one value by simply selecting Joint RGB/K channels mode ourselves. 
Picture
​To stretch the image with AutoHistogram, all we need to do is set a higher value here so that the process stretches the image enough to achieve the Median value you set. Remember that these values are not pixel values - they are normalised values (0 meaning black and 1 meaning white). In order to determine what pixel value this refers to, simply multiply by 65,535 (for 16-bit bit depth). Let us try the default value entered by AutoHistogram​ of 0.12000000​. We simply set this and click Apply to see our stretched image. 
Picture
​Though not a bad initial stretch, it is a little weak. We therefore undo the stretch and increase the RGB/K value to say, ​0.20000000. We then click Apply ​again. 
Picture
​We notice that though the image is a little brighter, there is little contrast gained in having increased the RGB/K value. This is because ultimately, the image is mostly dominated by background and the background is stretched alongside the stars and nebulosity (to achieve the desired Median value). Rather than increase RGB/K further, we can see what happens when instead of using AutoHistogram's default mode of Gamma Exponent, we use Logarithmic Transformation and then Rational Interpolation (MTF). These are shown below for the same RGB/K values of 0.20000000. 
Picture
Picture
We very clearly observe that Rational Interpolation (MTF) produces the most contrast out of all three, whilst Gamma Exponent produces the least contrast and Logarithmic Transformation​ is in-between the two. It is therefore recommended you use Rational Interpolation (MTF) in most, if not all cases. Having determined this, we can increase RGB/K further with Rational Interpolation (MTF)​ selected. Let us try a value of 0.30000000. 
Picture
This has achieved a very good level of detail in the stars and nebulosity, but unfortunately we return to the same problem of the background having been made a little too bright. In order to fix this, we enable the Histogram Clipping parameter at the top of AutoHistogram. 
Picture
Histogram Clipping will essentially adjust the black-point on the histogram during the stretch. We only need to pay attention to the section Shadows Clipping as we want to adjust the black-point, not the white-point (Highlights Clipping). Try with the default value of 0.010 on RGB/K under Shadows Clipping before adjusting (if required). 
Picture
The end result looks much better with higher contrast in the nebulosity as well as a darker background, though it can still do with some tweaking. To darken the background further, I simply increase the RGB/K value under Shadows Clipping, to 0.100​ as a trial. 
Picture
​Though this has improved the background and therefore the contrast somewhat, we can clearly increase it further. As the nebulosity is a little too bright as well, we can afford to decrease RGB/K under Target Median Values. As a trial, I set this to 0.30000000 and increase RGB/K under Shadows Clipping to 0.300​. 
Picture
​The above stretch gives the image a much more natural look, not over-doing the contrast and keeping the background at bay at the same time. Once you are happy with the stretch applied, you may simply close the AutoHistogram process window. 
 

3. Stretching using HistogramTransformation

​HistogramTransformation is PixInsight's main process for stretching images, and for good reason. HistogramTransformation affords the user tremendous control over the stretch that is applied, with simultaneous adjustments made to the black-point and white-point. It also has a graphical user interface showing the user what the histogram itself would look like after the stretch is applied, plus Real-Time Preview​ function. 
Picture
​The first step in using HistogramTransformation is to select the image you want to stretch from the list and then enable the Real-Time Preview window so we can see what is happening while we adjust the stretch parameters. 
Picture
​The numbers on the top four boxes (by default, all 1's) are horizontal and vertical zoom levels for both histogram previews (the top and bottom ones). Increasing these numbers will simply zoom in on either of these previews, horizontally, vertically or both (depending on which ones you increase). Note that the top histogram preview is what the histogram would look like if we apply the stretch we set up. The bottom histogram preview is a preview of the histogram's current state in the selected image. 

On the bottom histogram preview you will find three sliders. One on the very left, which is black. Another on the very right, which is white. And finally another in the middle, which is grey. 
Picture
​The left-most slider is the black-point slider. The right-most slider is the white-point slider. Finally, the middle slider is the mid-tones slider. To stretch an image, we simply have to adjust the mid-tones slider by moving it towards the left. This will curve the straight line displayed by default. Moving the mid-tones slider towards the left increases contrast. Conversely, moving the mid-tones slider towards the right decreases​ contrast. This can actually be useful for decreasing contrast in the background alone, if you are protecting the stars and nebulosity with a mask. 
Picture
As you do this, you will notice a couple of things. First of all, the top histogram preview changes dramatically. This is showing you what the image's histogram would look like if we apply the stretch we have set up. Second of all. the bottom histogram preview does not change (since the image's histogram is still what it is originally) but the straight line curves. This curve defines the non-linear stretch. Third of all, adjusting the mid-tones slider modifies the Midtones value at the bottom accordingly. You may actually enter a value manually and the mid-tones slider will move accordingly (producing a curve for a stretch). Lastly, the Real-Time Preview window is quickly showing stars and nebulosity popping up. 

Before we apply the stretch, we note that by default, RGB/K mode is selected. This is generally what you will want to stretch as it stretches the entire image as is (whether it is a colour RGB image or a monochrome image). For colour RGB images, you may stretch individual colour channels differently by selecting each one individually (via the R, G and B modes) and adjusting the mid-tones slider for each one individually. 

When you are happy with your stretch, be it in RGB/K mode to the entire image or a set of individual colour channel stretches, click the Apply button to apply the stretch. Please note however that as soon as you do this, the Real-Time Preview will turn mostly white. 
Picture
This happens because we still have the same stretch set up in HistorgamTransformation so the Real-Time Preview is showing you what it would be if we applied it again (a second time). Simply click the Reset button in HistogramTransformation to see what the image actually looks like now, after our initial stretch. 
Picture
​Notice that the bottom histogram preview is now very different - it is what the image's histogram now looks like after the initial stretch. Generally you may need to perform two or three stretches to get your image stretched to its best end result, though the first stretch is by far the most aggressive. The following stretch or two are very minor, with some adjustments made to the black-point to keep the background at bay. 

Let us now perform a second, less aggressive stretch. We again move the mid-tones slider to the left to increase contrast, but not as much as we did earlier. 
Picture
With higher contrast comes a brighter background, so we will now adjust the black-point before applying the stretch. To do this, simply move the black-point slider towards the right. This will have the net effect of moving the entire histogram back towards the left, since we are essentially redefining what constitutes black. We are redefining it to be a larger pixel value such that the brighter background becomes darker as it gets set closer to black. As we move the black-point slider to the right however, we pay close attention to the numbers displayed in Shadows​. 
Picture
Not only does the Shadows text box modify its value according to where you place the black-point slider (you can enter a value manually here if you wish!), but we also see some numbers to the right of this text box. This tells us how many pixels are being clipped. It is generally considered bad practice to clip pixels as this is making them pure black (or pure white if we white clip) and thus information from the image is being lost (contrast is destroyed at those pixels). However, you will generally not be able to avoid clipping a few pixels here and there, if you want to achieve a nice background. Above shows I am clipping 63 pixels in total, which constitutes a minute 0.0003% of the entire image. Given the much nicer background, I accept the loss and Apply​ the stretch. Again, once applied, I click Reset so I can see how the image looks now. 
Picture
​The question inevitably arises - up to what point do we consider the image stretched and not have to repeat this process again? Well, the general rule of thumb is that the image's histogram peak should be placed roughly 1/4 of the way up towards the far right (white side). This generally ends up giving the best overall contrast in features while maintaining a nice background. As from the second stretch onwards, the stretches applied are minor and will normally be accompanied by a black-point adjustment (if seen as necessary or beneficial​). The following is my third and final stretch. 
Picture
You may notice there are some pixels being black-clipped once again, but this is an acceptable loss to maintain the background at bay while boosting contrast in the stars and nebulosity to a desirable level. There are limits to everything. Moonlight as well as light pollution present in the images will brighten the background and you will lose faint nebulosity in the brighter background. No amount of fancy stretching will recover this without making the background look very artificially bright or very artificially black-clipped. At that point it is best to simply capture images when Moonlight is not a factor (or as much a factor) and from a dark sky site (or using a light pollution suppression filter in the optical train). 

Once you are happy with your image's non-linear state (after two or three stretches, most likely), you can close the HistogramTransformation process window and its Real-Time Preview​ window. I cannot stress enough how much nicer an image looks when the background is left at some minor brightness, instead of attempting to completely black-clip it. I think we can all agree that the above end result looks much better than the following atrocity:
Picture
No doubt the more experience you gain with post-processing images, the more you will cringe at the above. As with noise reduction, less is more. One final point is that it should be clear to you at this point that HistogramTransformation is by far the most versatile of PixInsight's processes for stretching linear images to a non-linear state. Though AutoHistogram proves fairly successful, it is no replacement for HistogramTransformation​. 
 

4. Stretching using MaskedStretch

​The final process in PixInsight's arsenal for stretching linear images is MaskedStretch. This process works by applying small stretches iteratively with the help of masks. The idea is that it produces contrast while respecting bright elements such as stars. The following is the same linear image we have been working with thus far, alongside the MaskedStretch process. 
Picture
​​First and foremost, we should define a small preview box over background so that MaskedStretch is able to work effectively, by providing it with a Background reference. PixInsight's autostretch is key to finding a suitable area of the image. It sometimes helps to zoom out to find a spot where there is a lack of stars. Pick an area that has nothing except background - no stars or diffuse nebulosity. 
Picture
With the preview box prepared, we remove the autostretch and go back to MaskedStretch. Here, click the small button next to the text box for Background reference. Select the preview box from the list and click OK​. 
Picture
With the Background reference now defined in MaskedStretch​, we proceed to look at the other settings. Iterations is 100 by default, which usually more than suffices to produce a smooth end result. You should not really need to touch this, though if you want to speed up the application, lower this to something like 50. The maximum value allowed is 1000, but it is considered very excessive. 100 is more than sufficient in the vast majority of cases. Target background is the main value we tweak to get an acceptable stretch. Remember these are normalised values (0 to 1) so multiply by 65,535 if you want to know what pixel value this refers to. Target background values between 0.10000000 and 0.20000000 tend to work very well. Let us leave the default value of 0.12500000 for now and test out the stretch. 
Picture
​We note that the stretch has worked very well. The background is kept at bay very effectively, the star colours look natural and unsaturated and the nebulosity has some good contrast. However, the overall image is a bit dark and lacks the punchy contrast produced by the likes of HistogramTransformation. We can undo the stretch and try a higher value of Target background to get a stronger stretch. For example, 0.20000000​. 
Picture
​The stretch looks much better, if not a little flat. Again, though the overall stretch produced a brighter image, it is still lacking in punchy contrast. However, one very high point is that because of the very conservative stretching, the colour information here is fantastic. All star colours are preserved and the nebulosity also displays good, unsaturated colour. 

In order to produce a brighter overall image along with a controlled background, we now tweak the value for Clipping fraction. This defines what proportion of the pixels to black-clip. The default value of 0.00050000 works exceptionally well in almost every case. However, we can try a value ten times larger, 0.00500000​. Target background is kept at 0.20000000​. 
Picture
​One should not go too far increasing Clipping fraction as the background ends up being too black-clipped and appears noisier as well. The above result is already excellent. After all, this need not be your only method to stretching the image. You could follow up MaskedStretch with a touch of HistogramTransformation. You can also take advantage of the fact that MaskedStretch respects colour fidelity so much and use it to stretch your colour RGB image. You can then use HistogramTransformation to stretch your Luminance image and later combine them together. Having used MaskedStretch for the colour RGB image, you would be guaranteeing that the stretched image is rich in colour including in stars all the way up to their cores. In fact, the following is the LRGB combination of this (Luminance image stretched with HistogramTransformation and the colour RGB image stretched with MaskedStretch, as above):
Picture
​It is certainly something to think about. There is no reason why you need to use HistogramTransformation for all your stretching if MaskedStretch does such a good job with colour. Besides, MaskedStretch also works with non-linear images so you can also use it to give a bit of an extra boost to your image. Once you use MaskedStretch, you are free to close its process window and also delete any preview box in your image (such as the one used as Background reference). 

 
Comment Box is loading comments...