PixInsight Forum

PixInsight => Release Information => Topic started by: Juan Conejero on 2014 January 23 11:42:15

Title: New MaskedStretch Tool
Post by: Juan Conejero on 2014 January 23 11:42:15
Hi all,

As we already have announced (http://pixinsight.com/forum/index.php?topic=6631.0), the latest version 1.8.1 of PixInsight introduces the new MaskedStretch tool.

The original MaskedStretch technique was devised by Carlos Sonnenstein, and a first JavaScript implementation (http://pixinsight.com/forum/index.php?topic=281.0) was published by David Serrano as long back as September of 2007. Since then, MaskedStretch has been available as an official script in all PixInsight distributions. It has seen also several important improvements, especially a faster implementation (http://pixinsight.com/forum/index.php?topic=1737.0) by Andrés del Pozo in 2010.

The idea behind MaskedStretch is conceptually simple: instead of stretching a linear image with a strong nonlinear transformation (or a set of similar transformations), such as the midtones transfer function (MTF) with the HistogramTransformation tool, apply a series of weaker MTFs iteratively, masking the image successively with the result of the previous iteration. As often happens in the algorithmic arena, this is one case of a pretty simple idea leading to something powerful and useful: MaskedStretch has proven one of the best ways to perform the initial stretch of linear deep-sky images. The iterative stretching/masking process prevents saturation of the highlights efficiently. At the beginning of the process the masks are very weak, so the stretching task really boosts the whole image. Once the image has reached good levels, the masks become stronger to protect bright structures selectively. The neat result is no saturated bright stars and nebular structures, which in turn means much better stellar profiles, less invasive stars—especially in wide field images—, and much better color saturation.

The new MaskedStretch tool is the result of a complete rethinking of the above idea. In the original script implementation there is a mask blurring feature that we have removed from our implementation. The reason is that such a blurring feature is a wrong concept in our opinion: MaskedStretch must be implemented strictly as a point operation; any low-pass filter applied to the masks turns the whole process into a neighborhood operator, with the consequent risk of artifact generation. Other than this, the tool implements a different algorithm to compute the MTF curves applied at each iteration. We have implemented also a user-selectable region of interest (ROI), similar to other ROI features in tools such as BackgroundNeutralization and ColorCalibration. Finally, the MaskedStretch process is fully parallelized, as is customary in PixInsight. MaskedStretch is part of the standard IntensityTransformations module, so it is open-source and its source code is available in the latest PixInsight Class Library (PCL) distribution.

Rather than a parameter-level description—which isn't very useful for relatively simple tools like this one in my opinion—, I'm going to show you how MS works through practical examples. In this post I'll work with an H-alpha image by Antonio Pérez (http://www.datsi.fi.upm.es/~aperez/astronomia_ing.html), who kindly allowed me to use it in an example of StarAlignment's mosaic generation capabilities (http://pixinsight.com/forum/index.php?topic=2011), back in 2010. The image is part of a six-frame mosaic of the Sadr region in Cygnus.

(http://forum-images.pixinsight.com/20140123/MS/01-tn.jpg) (http://forum-images.pixinsight.com/20140123/MS/01.jpg)
Click for full-size image (http://forum-images.pixinsight.com/20140123/MS/01.jpg)

With images covered by extended nebulosity, the ROI feature of MaskedStretch allows us to define the mean value of a sky background area on the final image. The easiest way to do this is by defining a preview roughly covering a background region, as I am doing in the screenshot above. An alternative would be to not use a ROI and define an overall "background" value for the whole image—normally quite high; something in the range from 0.2 to 0.4 for example—, but in these cases working on a true background region is much more intuitive and controllable, in my opinion.

After selecting the preview as a region of interest, the screenshot below shows the result of applying the tool with a target background value of 0.08.

(http://forum-images.pixinsight.com/20140123/MS/02-tn.jpg) (http://forum-images.pixinsight.com/20140123/MS/02.jpg)
Click for full-size image (http://forum-images.pixinsight.com/20140123/MS/02.jpg)

This target background value has been chosen to mimic the result of STF AutoStretch, which can be seen on the first screenshot. Now let's apply an equivalent—in terms of overall contrast and brightness—histogram transformation for comparison purposes.

(http://forum-images.pixinsight.com/20140123/MS/03-tn.jpg) (http://forum-images.pixinsight.com/20140123/MS/03.jpg)
Click for full-size image (http://forum-images.pixinsight.com/20140123/MS/03.jpg)

and without more preambles, let's compare both results side by side on a region with some relatively bright stars.

(http://forum-images.pixinsight.com/20140123/MS/04-tn.jpg) (http://forum-images.pixinsight.com/20140123/MS/04.jpg)
Click for full-size image (http://forum-images.pixinsight.com/20140123/MS/04.jpg)

The images speak loud and clear. These are the processed images and their histograms:

MaskedStretch
(http://forum-images.pixinsight.com/20140123/MS/MS-small.jpg)
(http://forum-images.pixinsight.com/20140123/MS/MS-histogram.png)

HistogramTransformation
(http://forum-images.pixinsight.com/20140123/MS/HT-small.jpg)
(http://forum-images.pixinsight.com/20140123/MS/HT-histogram.png)

It's clear that the HT version has better contrast—although this is always a matter of taste, and hence discussable—, but the lacking contrast can usually be recovered after MS with a careful application of CurvesTransformation or, much better, by mixing both results selectively through masks and/or multiscale techniques. This advanced example with NGC 7023 (http://pixinsight.com/tutorials/NGC7023-HDR/en.html) implements this technique.

For a more exhaustive comparison, I'll show the results achieved on two small crops covering bright stars. The three-dimensional representations have been generated with the 3DPlot script in PixInsight.

First crop - MaskedStretch
(http://forum-images.pixinsight.com/20140123/MS/MS-crop-1.png)
(http://forum-images.pixinsight.com/20140123/MS/MS-crop-1-3d-tn.png) (http://forum-images.pixinsight.com/20140123/MS/MS-crop-1-3d.png)
Click for full-size image (http://forum-images.pixinsight.com/20140123/MS/MS-crop-1-3d.png)

First crop - HistogramTransformation
(http://forum-images.pixinsight.com/20140123/MS/HT-crop-1.png)
(http://forum-images.pixinsight.com/20140123/MS/HT-crop-1-3d-tn.png) (http://forum-images.pixinsight.com/20140123/MS/HT-crop-1-3d.png)
Click for full-size image (http://forum-images.pixinsight.com/20140123/MS/HT-crop-1-3d.png)

Second crop - MaskedStretch
(http://forum-images.pixinsight.com/20140123/MS/MS-crop-2.png)
(http://forum-images.pixinsight.com/20140123/MS/MS-crop-2-3d-tn.png) (http://forum-images.pixinsight.com/20140123/MS/MS-crop-2-3d.png)
Click for full-size image (http://forum-images.pixinsight.com/20140123/MS/MS-crop-2-3d.png)

Second crop - HistogramTransformation
(http://forum-images.pixinsight.com/20140123/MS/HT-crop-2.png)
(http://forum-images.pixinsight.com/20140123/MS/HT-crop-2-3d-tn.png) (http://forum-images.pixinsight.com/20140123/MS/HT-crop-2-3d.png)
Click for full-size image (http://forum-images.pixinsight.com/20140123/MS/HT-crop-2-3d.png)

In the next examples, I'll show you how MaskedStretch works for a completely different deep-sky target: a galaxy image where the field is mostly free sky background. I'll show you also the kind of results that can be achieved with color images.
Title: Re: New MaskedStretch Tool
Post by: sreilly on 2014 January 23 12:07:23
I'm curious the difference in scripts. Mine is shown below:
Title: Re: New MaskedStretch Tool
Post by: Juan Conejero on 2014 January 23 12:10:31
The MaskedStretch script has been left as part of the official distribution for historical reasons, and also to show some due respect and love to their developers. However, it should no longer be used.

We are talking here about the new MaskedStretch *tool*, which you can find under the IntensityTransformations category on the Process Explorer window. Scripts and tools are different beasts :)
Title: Re: New MaskedStretch Tool
Post by: sreilly on 2014 January 23 12:15:19
Not to disagree but.....I looked under each as well and have checked all for updates
Title: Re: New MaskedStretch Tool
Post by: sreilly on 2014 January 23 12:18:43
Hello Juan,

I should also mention that it isn't listed under all processes. Below is how my updates are checked. Am I missing something important?

Thanks,

Steve

Title: Re: New MaskedStretch Tool
Post by: Juan Conejero on 2014 January 23 12:24:03
Yes, a new version of PixInsight (http://pixinsight.com/forum/index.php?topic=6631.0) :)

Don't you receive our notification messages?
Title: Re: New MaskedStretch Tool
Post by: sreilly on 2014 January 23 12:51:08
I have the forum setup so I should be receiving messages but find that I seem to get some for some of the groups but not all. I am not using any spam filtering so there aren't any going there. Often I'll check the forum and see new messages that I haven't received notifications on but have been so involved in EMT classes and church building projects I haven't checked near as often. Seems I downloaded and installed version 1.8 just recently. I have just downloaded version 1.8.1. Sorry for my confusion.

Thanks Again,

Steve
Title: Re: New MaskedStretch Tool
Post by: Andres.Pozo on 2014 January 23 15:07:26
The MaskedStretch script has been left as part of the official distribution for historical reasons, and also to show some due respect and love to their developers. However, it should no longer be used.

As one of the developers, I don't mind (and even prefer) that you remove the script from the distribution.
Title: Re: New MaskedStretch Tool
Post by: niteman1946 on 2014 January 24 05:09:00
Here are two mask stretched images of the same file.  The left one is stretched after dynamic crop and DBE.  The right one is stretched after dynamic crop, DBE, TGVDenoise and deconvolution.  The effect on the second image is the same whether I use mask stretch defaults or use a background preview.  Does the use of TGVDenoise and/or decon cause problems (as apparent here) with the mask stretch?

Thanks,
Mark
Title: Re: New MaskedStretch Tool
Post by: sreilly on 2014 January 24 07:55:10
I'd be curious to see what the left image looks like with about the same amount of stretching showing the galaxy as in the right image. It looks like a lot is still hid by the dark background.

-Steve
Title: Re: New MaskedStretch Tool
Post by: niteman1946 on 2014 January 24 08:29:26
Hi Steve, thanks for your response.
This image was shot with the object fairly low and setting directly over the lights of our town.  So there's quite a bit of LP.
I think it's fair to say that the left image can be stretched (using the Histogram Transformation) to look like the right image.  One noticeable difference is the right image has had noise reduction.

I've not used mask stretch for any processing as yet and have always relied on the HT.  So with the discussion, I'm trying to get my head around the how, when where and why.  At first glance, it looks like I need to tweak some parameter settings (i.e.Target Background) to keep the background under control.  And at that point, why not just continue to use HT?

FWIW, my skies are pretty bad and LRGB is a struggle to get right.  This one (NGC2403) was/is particularly problematic.  NB on the other hand comes out pretty good.

Mark
Title: Re: New MaskedStretch Tool
Post by: dgbarar on 2014 January 25 18:06:30
Hi,

I have begun to experiment with the new Masked Stretch Tool.  From Juan's example I developed a preview of a background area, used it as an "region of interest", and performed a masked stretch.  A "nice" result, but with not much contrast as expected.

I then thought I would not use the preview as the "region of interest" but as the "background reference".  Again, I performed the new masked stretch and got what looks like the same result.  Even the histograms of the stretched images look identical.

Since the result appears the same, why would I use one approach vs the other?  My guess is that if the I use the background reference approach I can specify the pixel values that are to be included in the background to the exclusion of pixels that have values outside the range.  Is this correct?

Don Barar
Title: Re: New MaskedStretch Tool
Post by: rockyraccoon on 2014 January 26 19:45:05
What's the role of the "region of interest" parameter in this tool?
Title: Re: New MaskedStretch Tool
Post by: MikeWiles on 2014 January 28 08:01:43
In the next examples, I'll show you how MaskedStretch works for a completely different deep-sky target: a galaxy image where the field is mostly free sky background. I'll show you also the kind of results that can be achieved with color images.

Nothing would make me happier than to see these other examples.  I'm pretty excited about this tool - thanks again Juan.

Mike
Title: Re: New MaskedStretch Tool
Post by: ajbarr on 2014 January 30 21:02:47
Harry we desperately need you to do a tutorial on this that's easy for neophytes to understand.
Title: Re: New MaskedStretch Tool
Post by: Israel Gil on 2014 January 31 03:23:06
I also would like to see other examples as I'm not getting how to use it properly...     :blank:

It looks great though.

Greetings.
Title: Re: New MaskedStretch Tool
Post by: Torsinadoc on 2014 February 01 03:05:25
I would agree on a tutorial.  It seems like a pretty dang simple tool

Questions:
1.  How do you determine the correct number for the target background and number of interations
2. Do you just leave the rest as default?


As you can see below, I get an odd coloration of a star.  I tried several settings but not sure how to avoid this?
Title: Re: New MaskedStretch Tool
Post by: Geoff on 2014 February 03 21:14:38
One thing I have noticed is that using the masked stretch can soften and slightly blur the edges of the stars.  Is there a way to avoid this?  See this image http://www.astrobin.com/76328/ for an example.
Geoff
Title: Re: New MaskedStretch Tool
Post by: ajbarr on 2014 February 03 23:51:32
Agreed. Great tool but we need some help with the settings.
Title: Re: New MaskedStretch Tool
Post by: cdesselles on 2014 February 04 13:29:38
Geoff:  Have you tried sstretching without using the new tool to see the difference?  Just curious.
Title: Re: New MaskedStretch Tool
Post by: Geoff on 2014 February 04 13:50:29
Geoff:  Have you tried sstretching without using the new tool to see the difference?  Just curious.
Yes. While I get a better stretch in the main feature (galaxy, nebulosity), the stars do seem sharper with a more conventional stretch.
Title: Re: New MaskedStretch Tool
Post by: Juan Conejero on 2014 February 05 00:58:53
One thing I have noticed is that using the masked stretch can soften and slightly blur the edges of the stars.

The MaskedStretch algorithm is a point operator. This means that neighbor pixels are not correlated by MS, so there's no way it can blur the image.

IMO, we are all heavily accustomed to nearly-saturated stars. This is just because of the tools and algorithms that we have been applying to stretch images for many years. However, hard star edges should be considered artifacts because they denote data losses caused by deficient handling of bright image features, just as ringing artifacts for example.

The MS algorithm generates much better star profiles because it protects the highlights from excessive amplification. Your image is an excellent example:

http://www.astrobin.com/76328/

The stars are very nice in this image. They have soft profiles, which allows them to show different star colors with good color saturation. Since the stars have been protected by MS, they have moderate lightness levels that leave room for chroma components to vary across a wide range of hues.
Title: Re: New MaskedStretch Tool
Post by: Geoff on 2014 February 05 02:24:30
Thanks Juan for your nice comments about the stars. On rethinking, i can appreciate that this is the way stars should look. A hard edge doesn't fit well with the usual bell-shaped curve expected of a typical star profile. As you say, we come to accept wrong things as normal because we see so many examples.
Geoff
Title: Re: New MaskedStretch Tool
Post by: Israel Gil on 2014 March 31 10:42:43
Thanks Juan for clarifying this....   really good to know...   :)
Title: Re: New MaskedStretch Tool
Post by: topboxman on 2014 June 09 09:45:36
When is the best time to use Masked Stretch. This is my typical flow:

1) Dynamic Crop
2) DBE
3) BN
4) CC
5) MMT to reduce noise while image is still linear
6) HT
7) HDR
8: LHE
9) ACDNR
10) Curves Transformation
11) UnSharp Mask

Would Masked Stretch be used after step 5? Is Masked Stretch best use on linear images?

Thanks,
Peter
Title: Re: New MaskedStretch Tool
Post by: sreilly on 2014 June 09 12:56:57
According to your routine now I would sub it for the HT and see how that works. I don't do any noise reduction until the end of my images. maybe I need to rethink this.

-Steve
Title: Re: New MaskedStretch Tool
Post by: topboxman on 2014 June 09 13:20:51
How about tweak with HT after Masked Stretch or anywhere else during post processing of non-linear image if necessary?

So, Masked Stretch is supposed to be some kind of alternative to HT?

I will play with Masked Stretch on some of my old images and compare with HT.

Thanks,
Peter
Title: Re: New MaskedStretch Tool
Post by: sreilly on 2014 June 09 14:01:29
What I've found for my images, and this is personal taste, is usually using a background image and a setting of 0.09. If this seems too different than what I'd like I experiment between that and 0.14. Regardless I usually find that I'll followup with a curves stretch to increase contrast which is what I usually do after using the Histogram Stretch tool as well. For my luminance after calibration, alignment, stacking, crop, dbe as needed (almost always), I'll do a masked stretch with a sampled background (preview) void of stars and object, HDRW, maybe an unsharp mask, masked Local histogram stretch, and then a final curves adjustment. If I have enough data I usually don't need any noise reduction (in my opinion) but even still I would combine with my RGB image before and see how the result looks. What I have found in PI is usually to get an appealing L+RGB image (again personal taste) I usually need a darker than I would normally do luminance background. If and when we get layering in PI this would be a bit easier by adjusting the opacity of each layer. After the final image is made I'll look at possible noise reduction. I try to stay away from this process as I usually see the final image blurring the sharper details but then it may be my use of noise reduction and I'm doing it wrong.

The only real difference when creating the RGB image is I add the background neutralization and color calibration after DBE. Most everything else follows except the HDRW on the RGB data unless there is no luminance for that image. Then a dynamic align (I bin my RGB 2x2 and Lum 1x1) and use LRGB Combination to create the L+RGB image. Then maybe noise reduction if needed.

But then again there are far better PI processors here than me but this is my basic routine.

-Steve
Title: Re: New MaskedStretch Tool
Post by: Warhen on 2014 July 20 16:38:38
Yes, yes Torsinadoc! This is what I see consistently. I'd talked privately with Carlos some about this but never did show you an example Carlos. I Do like MaskedStretch. I Do now agree that loss of contrast (compared w/ HT) can be recovered sufficiently w/ Curves or LHE. What I Can't seem to make work is combining L w/ RGB, without that star artifact. Any thoughts Juan/Carlos?

If I use HT for L but use MaskedStretch on RGB it occurs at LRGBC. If I MS both and then combine, I don't like the result. If I try to combine while still linear, doing one MS afterward, I get an usable result. Any suggestions? Thanks all!
Title: Re: New MaskedStretch Tool
Post by: Carlos Milovic on 2014 July 21 18:23:15
Have you tried the ImportL process in my development set? It uses a different strategy to import lightness, modifying the RGB channels depending on both the old and the new lightness. This is used in HDR processing, to account for the strong data compression, to retain original colors.
Title: Re: New MaskedStretch Tool
Post by: Alejandro Tombolini on 2014 July 25 14:24:55
As you can see below, I get an odd coloration of a star.  I tried several settings but not sure how to avoid this?

Hi Torsinadoc,

I think that there are two alternative to solve it. The main is to have data that allow make a HDR, and the second is to repair the stars.
I do not have a good set of image to make an example with HDR but I am just processing an Antares that is useful to show the reparation.

(http://3.bp.blogspot.com/-d1f3wX42p9M/U9LFctefpCI/AAAAAAAAA_w/lAx7uAD5gC0/s1600/stars01.png)

First I have applied Masked Stretch tool to a crop of the image with Antares using the default values. As expected the center of the star does not look fine.

(http://4.bp.blogspot.com/-6EzfpOy186I/U9LFfEQTPsI/AAAAAAAABAA/rOEo9dk2HXw/s1600/stars02.png)

Over a clone of the linear image apply the Repaired HSV Separation script. Set the desired levels to Clip Shadows and Repair level and check V- no repairs for the scrip to generate also the Unrepaired V

(http://2.bp.blogspot.com/-rn0FE9ethZ0/U9LFet4bPtI/AAAAAAAAA_4/53vs66iutsc/s1600/stars03.png)

You will get four new images:
Unrepaired V
V
Sv
H

(http://2.bp.blogspot.com/-Xhl9t8MllyU/U9LFqcROKGI/AAAAAAAABAM/ozGIX40c1uY/s1600/stars04.png)

I have seen in some cases that is better to generate the new image using the Unrepaired V instead of V. Using channel Combination tool generate the new image.

(http://1.bp.blogspot.com/-NlYiAdmYThU/U9LFqBbdTiI/AAAAAAAABAI/wsXgTC6GpNw/s1600/stars05.png)

The center of the stars look better now in the linear image:

(http://4.bp.blogspot.com/-c6w-Le--upI/U9LFrEiB12I/AAAAAAAABAU/wYPt9d0ie1c/s1600/stars06.png)

Finally apply the same Masked stretch process to the repaired image.

(http://4.bp.blogspot.com/-H-umIKs5LCI/U9LFuAQwRoI/AAAAAAAABAg/uYA_tw59eSA/s1600/stars07.png)

I also recomend that you see Bob Andersson post (http://pixinsight.com/forum/index.php?topic=5149.0) about this script.

Saludos, Alejandro.
Title: Re: New MaskedStretch Tool
Post by: Torsinadoc on 2014 July 25 17:38:01
thanks. ill try it out
Title: Re: New MaskedStretch Tool
Post by: Jose Joaquin Perez on 2014 July 27 18:03:40
Thanks Alejandro for sharing this interesting and useful workflow!!

Abrazo...maestro (-: