PixInsight Forum

Software Development => New Scripts and Modules => Topic started by: FrankW on 2017 October 03 12:39:09

Title: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 October 03 12:39:09
This is my first PI script and at the beginning the implementation was strongly affected by the fact that I am not very familiar with Java. By writing the script I learned a lot about PJSR and PCL but I cannot warrant that there are still some bugs or that the coding fulfills all expectations of a professional programmer. So I would by happy about any feedback on that. However, at the end I am quite happy with the result and want to share it with the community.

The idea to write this script arose after I read an article about image processing using fuzzy logical techniques. The article examined contrast enhancement of medical images but I thought that it is possible to use the same techniques for astronomical images as well. After poking around, I found an algorithm called “Local Fuzzy Histogram Hyperbolization” (LFHH) which becomes the theoretical basis for the script.

Compared to other contrast enhancement methods like Local Histogram Equalization a fuzzy algorithm offers the opportunity to define the profile of the contrast enhancement with the aid of a mathematical function (called fuzzy membership function). By changing the shape of the function the contrast enhancement can be adapted to the individual characteristics of the target image.

To apply the LFHH algorithm to astronomical images effectively, several adjustments were necessary. One main topic was to prevent the stars expanding too much during the process. In the script this can be achieved by using a star mask and it is strongly recommended to use this option. Moreover, the primary algorithm leads to a very strong contrast enhancement. Therefor it was essential to find possibilities to regulate the effect, particularly the impact of the local applying of the algorithm. Special parameters like a clustering of the image segmentation, the definition of lower and upper bounds for the sample values in the segments or the automatic calculation of a segmental intensifier are available to handle this matter. I described the functionality of these parameters in the corresponding mouse-over help of the script.

Like always in the field of image processing, no universal parameters exist. For each image you must find the best parameters fulfilling your individual requirements by trial and error. To give an impression how the script works I attach some examples. The target is a Ha-image of M16. For each of the four membership functions which are implemented in the script I enclose a typical result.

If you have any remarks or questions do not hesitate to contact me.

Cheers,
Frank


(https://dl.dropbox.com/s/sa8mt83dlhzfwsd/M16.jpg?dl=0)
Target Image

(https://dl.dropbox.com/s/jtza76imdp3cht7/Gauss.jpg?dl=0)
Gauss/15x15/Clustering 0.05/FHH glb 0.45/FHH lub 0.55/Local FHH beta 1.20/sigma 0.60/HT median 0.23

(https://dl.dropbox.com/s/kl6g3r7suub86y7/NormalizedGauss.jpg?dl=0)
NormalizedGauss/15x15/Clustering 0.05/FHH glb 0.45/FHH lub 0.55/Local FHH beta 1.10/sigma 0.75/HT median 0.23

(https://dl.dropbox.com/s/ko2uxzf3fflsueu/Sigmoid.jpg?dl=0)
Sigmoid/15x15/Clustering 0.05/FHH glb 0.4/FHH lub 0.6/Local FHH beta 1.05/gamma 5.0/x0 0.50/HT median 0.23

(https://dl.dropbox.com/s/paz9qxiobfk3axq/Linear.jpg?dl=0)
Linear/15x15/Clustering 0.05/FHH glb 0.45/FHH lub 0.55/Local FHH beta 1.10/HT median 0.23

(https://dl.dropbox.com/s/0nk7wvjo4gjko3g/Aggregate.jpg?dl=0)
... and a combination of the four images 
Title: Re: LocalFuzzyHistogramHyperbolization Script
Post by: oldwexi on 2017 October 03 14:26:34
Frank!

Excellent!
This works nice with very weak and nearly invisible(on the image)
Molecular clouds also!
The contrast increase is unbelievable.

Thanks a lot!

Gerald
Title: Re: LocalFuzzyHistogramHyperbolization Script
Post by: aworonow on 2017 October 03 14:38:38
Frank,
  Looks very interesting and useful. Thanks much. However, although I see a paper clip (meaning an attachment I guess) clicking it does nothing. How do I download the script?

Thanks, AlexW
Title: Re: LocalFuzzyHistogramHyperbolization Script
Post by: aworonow on 2017 October 03 14:40:22
Frank, I found it...at the bottom. Sorry to bother you.
Thanks again, Alex
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 October 04 07:42:45
Gerald and Alex,

Thanks for your feedback!

Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: dgruber on 2017 October 06 04:36:44
Thank you for your new -  most interesting and useful script -   I postet your News  to  the Austrian PixInsight Facebook-Group.

https://www.facebook.com/groups/565816306913779/
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: georg.viehoever on 2017 October 09 02:07:35
Frank,
do you have a source/citation/paper for the method?
Georg
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 October 09 07:58:33
Georg,

here one Link to a pdf-file:

https://www.researchgate.net/profile/Hamid_Tizhoosh/publication/285413959_Fuzzy_Histogram_Hyperbolization_for_Image_Enhancement/links/565e0f6d08aefe619b26ed1a/Fuzzy-Histogram-Hyperbolization-for-Image-Enhancement.pdf (https://www.researchgate.net/profile/Hamid_Tizhoosh/publication/285413959_Fuzzy_Histogram_Hyperbolization_for_Image_Enhancement/links/565e0f6d08aefe619b26ed1a/Fuzzy-Histogram-Hyperbolization-for-Image-Enhancement.pdf)

From the same author you can find several publications about this topic in the internet. He also published a book (in German) about fuzzy image processing.

Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: Juan Conejero on 2017 October 09 13:42:12
Hi Frank,

Congratulations on an excellent implementation of a very interesting and useful algorithm. If you agree, I'd like to include this script in the official distribution. Can you submit it to our open-source GitHub repository (https://github.com/PixInsight/PJSR)?

Do you plan on implementing this tool in C++/PCL?

Thank you so much for this nice contribution to PixInsight.
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 October 10 05:04:18
Hi Juan,

Thank you very much. I’m really delighted that you want to include the script in the official distribution. Of course, I agree to that. The script is submitted to the GitHub repository.

To be honest, I think that currently an implementation of this tool in C++/PCL is beyond my skills and not accordable with my available time. Instead, I would like to write a documentation of the algorithm and the tool. That would complete the whole thing for an integration of the script in PI!

Cheers,
Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 November 04 05:53:55
I have now finished the documentation of the script! Enclosed find a new version of the script which now includes a link to the documentation. The documentation can be downloaded as a pdf here:

https://dl.dropbox.com/s/ixvf7zmlnpu1iz4/LFHH_doc.pdf?dl=1 (https://dl.dropbox.com/s/ixvf7zmlnpu1iz4/LFHH_doc.pdf?dl=1)

All relevant files for the use in PixInsight can be downloaded here:

https://dl.dropbox.com/s/pvwzhomnh95hpst/LFHH_doc.zip?dl=1 (https://dl.dropbox.com/s/pvwzhomnh95hpst/LFHH_doc.zip?dl=1)

I tried to submit the files to the GitHub repository but I fear I didn’t get it.  So I would be glad to get some advice how to do this.

Cheers,
Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 November 06 11:23:08
I just managed it! Now all files are also available on the GitHub repository:

https://github.com/fweidenbusch/PJSR/tree/LFHH (https://github.com/fweidenbusch/PJSR/tree/LFHH)

Cheers,
Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: Juan Conejero on 2017 November 09 00:56:21
Hi Frank,

The script is fantastic and the documentation you have authored in PIDoc is just perfect, great work!

I want to release this script and its documentation as official updates as soon as possible. I'm going to invite you to our official GitHub development team. Your script should be on a PJSR/src/scripts/LocalFuzzyHistogramHyperbolization directory, and the documentation in PIDoc format on Reference-Documentation/scripts/LocalFuzzyHistogramHyperbolization. Please make these changes and submit a pull request to our official open-source repository.

Thank you again for this great contribution.

Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: Richard Christy - DarkSky|AP on 2017 November 10 08:19:57
Love this script! Excellent works and fantastic documentation to go with it!! Thank you....
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 November 12 11:18:48
Thanks again for this great feedback!  :)

Thank you, Juan, for your help to tackle my problems with GitHub. I hope that everything is going well now. And a big compliment for the PIDoc Compiler: It is a fantastic tool!
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: Alejandro Tombolini on 2017 November 14 17:05:41
Hello Frank, excellent job, thank you for sharing the script  :)

Saludos, Alejandro.
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: mar504 on 2017 November 15 11:13:01
Very useful! Thank you Frank for sharing this with all of us!
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: Alejandro Tombolini on 2017 November 15 19:47:59
Hi Frank, the script is excellent, easy to use and gives very good results.

To be honest I did not have the time to read the documentation but when trying it on an image with default parameters I noticed an extrange result (on the right of the image) It was when I applied the script to a linear not stretched image or to a linear stretched image.

After a non linear stretching of the image the script gives the expected result.

Does it means it is not allowed for linear images or could be something else?

Saludos, Alejandro.
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 November 16 01:01:35
Hi Alejandro,

Actually the target image should be non linear (I mentioned it in the documentation  ;)). To apply LFHH also on linear images is one topic on my to-do-list. The other one is the possibility to use color images. If you like, please send me your linear image. This may help me to solve the problem with linear images.

Cheers,
Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: Tertsi on 2017 November 16 11:07:54
Hi,

Can this script be used for the color images as well, i.e. if one extracts luminance from non-linear color image and then run script for it and then combine it back to color image with PixelMath? Or am I talking nonsense? :P

Tertsi
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 November 16 12:43:02
Hi,

This will work in general. This is one of the ideas I have tested to apply LFHH to a color image, but I am still searching for the best method. The results are extremely dependent on the color distribution of the target image - so just try it!  :D

Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: Alejandro Tombolini on 2017 November 16 16:48:11
Thank you Frank,

If you like, please send me your linear image. This may help me to solve the problem with linear images.

Of course, let me know if this (https://www.dropbox.com/s/66k1d52bkpqsy8t/IMG48.FIT?dl=0) link works

Saludos, Alejandro
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 November 17 00:16:36
Of course, let me know if this (https://www.dropbox.com/s/66k1d52bkpqsy8t/IMG48.FIT?dl=0) link works

Hi Alejandro,

Thank you, it works.  :)
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 November 17 13:04:56
Hi Alejandro,

Thanks again for sending me that image. The analysis of it was a really helpful exercise!

First, coming to these troubles at the right side of your image. That has nothing to do whether the image is linear or nonlinear. I had the same troubles with some more powerful parameter settings for the nonlinear image. The fact is that there are stacking artifacts on your image, at the bottom but especially an edge of two pixels at the right side! These artifacts lead to that strange results for the the LFHH algorithm and the impact of them is higher for a linear image than for a nonlinear one. This is the reason why you only recognized them for the linear image. But the main massage is:

Remove all stacking artifacts before applying LFHH!

After cropping the image, I got fine results - even at the borders of the image!

(https://dl.dropbox.com/s/3n7i3mp2cuf3opx/Image.jpg?dl=0)

The other finding from this analysis was a deeper understanding why the script doesn’t work for linear images. Predominantly, the image improvement you achieved for the linear images doesn’t depend on LFHH. In fact it is effect of the automatic HistogramTransformation included in the script. After unchecking this option you will see the real effect of the LFHH algorithm on a linear image. The underling problem is that the implemented membership functions are not aggressive enough to stretch a linear image. This is an important issue for the further improvement of the script! Thus your comments were really helpful.   :) Thank you for that!

Cheers,
Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: Alejandro Tombolini on 2017 November 17 15:18:20
Thank you very much Frank!

The fact is that there are stacking artifacts on your image, at the bottom but especially an edge of two pixels at the right side! These artifacts lead to that strange results for the the LFHH algorithm and the impact of them is higher for a linear image than for a nonlinear one. This is the reason why you only recognized them for the linear image. But the main massage is:
Remove all stacking artifacts before applying LFHH!
After cropping the image, I got fine results - even at the borders of the image!
Perfectly clear now, I didn't realize the two pixels column at the right side. By the way, it is a single uncalibrated image of a friend that I used only for testing.

I will continue testing

Saludos, Alejandro.
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: grztus on 2017 November 18 13:20:45
Hi friends,
I have a problem installing the auto-update with this script.
I have installed it, but it is not visible in the "script" --> "utilities" menu (and any other "script" menu).
I'm able to execute it only via "Script" --> "Execute script file".
What can I do, to make it visible in the "script" --> "utilities" menu??
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 November 19 04:11:15
I have installed it, but it is not visible in the "script" --> "utilities" menu (and any other "script" menu).
I'm able to execute it only via "Script" --> "Execute script file".
What can I do, to make it visible in the "script" --> "utilities" menu??

That sounds strange! Have you already tried  "Script" --> "Feature Scripts..." --> "Regenerate"?
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: chris.bailey on 2017 November 20 01:57:19
Thanks Frank for a great script. Like everything in PI it took a while to tune the settings but it works really well after Masked Stretch, TGVDenoise and with a good star mask. I have been doing some wide field Ha images of some of the big Sharpless objects and it works very well on these as hopefully this sample of Sharpless 131 shows

Chris
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: grztus on 2017 November 20 12:53:53
That sounds strange! Have you already tried  "Script" --> "Feature Scripts..." --> "Regenerate"?
It wasn't listed there (I have another computer with PI and LFHH installed, so I had a comparison).

I solved the problem in other way:
After I've checked "Script" --> "Feature Scripts..." --> "Regenerate" and saw, that there is no LFHH, I have started the PI 1.8.5 installer, chosen "Repair" and execute it.
After repair, I've started the PI and got notification, that I have 16 new updates (there was also LFHH) - I've chosen all of them, made an update and closed PI.
After closing PI there was a new notifiaction with information about installing new updates, I have chosen "restart PI" and now everything is OK, the LFHH is in the script "utilities" menu.

During the first installation of the LFHH update, I did't chose "restart PI" and I've shutted down the system after closing PI. Probably that was the point where I've made a mistake.
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2017 November 23 07:03:53
it took a while to tune the settings but it works really well after Masked Stretch, TGVDenoise and with a good star mask

Hi Chris ,

It’s really a great image! Yes, tuning the settings is always a cumbersome job. :-[ When I wrote the script, I faced the choice that the script works more “automatically”. But the characteristics of astronomical images can vary in a wide range and I belief that finally only the user can decide what is the best result from an image process. And your image shows that the effort will be worthwhile. :smiley:

Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: jtalbot on 2018 January 06 14:35:51
Love this script!  I recently processed an Ha only version of the area around the flame nebula with the script.  I found that if the stars were removed prior to running the script, I got excellent results.  I found this easier than creating a star mask with the star exactly the right size.  I then added the stars back in.  Here is the processed version.

Best,
Jon Talbot
(http://www.starscapeimaging.com/resources/Flaming-Star-Region_Ha_LFHH_50.jpg)
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2018 January 09 14:22:19
Jon,

that' s a great image! And it's a really interesting idea how to handle the 'star problem'. Can you give a brief description how you remove and add the stars.

Cheers,
Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: RickS on 2018 January 09 15:26:33
I used LFHH to enhance starless Ha and Oiii data to produce this CTA 1 image in the gallery: https://pixinsight.com/forum/index.php?topic=12015.0  It's a very useful script!

There are probably quite a few posts on the forum about star removal.  Here's a recent one: https://pixinsight.com/forum/index.php?topic=11879.msg73080

Cheers,
Rick.
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: jtalbot on 2018 January 10 08:53:24
Hi Frank,
I basically followed a technique similar to the link Rick posted.  My technique wasn't quite as involved, or as good, as what is in the link however it seemed to work just fine.
Again, thanks for a great script.

Best,
Jon
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2018 January 12 02:19:42
Jon, Rick,

Thank you very much. I tried the method and I am really excited.  :D  At the moment I work on an update of the script to apply LFHH to color images and even use the algorithm for stretching linear images. I am not sure if I can include your method in the script but it is a great hint anyway.

To give you an impression how the update works, I attached one of my test images. For this image of the Pelican Nebula most of the contrast enhancement was achieved with LFHH. I hope I will finish the work in the next weeks.

Cheers,
Frank

(https://dl.dropbox.com/s/hlfvgq3p2vwvje6/IC%205070.jpg?dl=0)
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2018 April 16 12:00:13
Hello,

As already announced in the forum, in the last weeks I worked on an update of the LFHH script. Today, I am pleased to present some significant improvements and enhancements to the script. These cover the following topics:


Membership functions
On the one hand I added the MTF function as an additional membership function and also the possibility of normalization for the Sigmoid function. But the most important innovation, however, is a membership function with which you can stretch linear images. The script was originally intended only for contrast enhancement, but the discussion with Alejandro Tombolini in the forum gave me the idea to deal with stretching as well.
When looking for a suitable membership function for the stretching, I was inspired by one of the PixelMath tutorials by Gerald Wechselberger. I adapted the idea of „masking“ the MTF function and developed a special membership function for the use with LFHH. Although PixInsight already has a number of excellent processes for image stretching, the results with the new membership function - I called it „Masked MTF“ - were so convincing that I absolutely had to include it in the script. Apart from this I think that's a good opportunity to thank Gerald for his excellent tutorials!

Here is an example: the first image is stretched with AutoSTF, the second one with the new Masked MTF function.

(https://dl.dropbox.com/s/ryprpt0juaq1495/img_IC410HT.jpg?dl=1)

(https://dl.dropbox.com/s/xn278okuhc9x1l0/img_IC410LFHH.jpg?dl=1)

I did a lot of testing with the new feature and I achieved really good results with it. I would be interested in further experiences. With appropriate feedback, I would invest more work in the development of this functionality.


Extrapolation algorithm for the image borders
This feature is not visible, but still a significant improvement of the program. Even if all marginal errors, such as stacking artifacts, were removed in the target image, the old version of the script occasionally had problems with the interpolation of the segments at the image borders. By improving the algorithm, these problems could be resolved on all critical test images. The algorithm is now much more robust.


Color images
Basically, I think contrast enhancement should be applied to the luminance component of an image. Nevertheless, the question arises, how to treat color images. I already described a possibility in the forum: extract the L component from the RGB image, then apply LFHH to the L image and merge it back to the RGB image using LRGBCombination. Another possibility I have now implemented in the script. There the individual colors are modified in the same ratio as the underlying L component.

Here is an example: the target image, contrast enhancement with extracted L component and direct application of the script.

(https://dl.dropbox.com/s/7ro118hgu4mz7lg/img_IC410_SHO.jpg?dl=1)

(https://dl.dropbox.com/s/xcdda3vhmzsedgl/img_IC410_SHO_LRGBC.jpg?dl=1)

(https://dl.dropbox.com/s/k4kkmu3ymuryek3/img_IC410_SHO_LFHH.jpg?dl=1)

Although the masked MTF function gives good results for color images, it is still not capable for rationally stretching linear RGB images. The script has a check routine, if a processing of an RGB image leads to meaningful results. Basically, the existing stretching processes like MaskedStretch or ArcsinhStretch are better suited for this.


Documentation of parameters
In my numerous tests, I came across the problem of reconstructing the script parameters at a later date. Although the selected parameters have been displayed in the console output so far, this information is lost after a reboot. Therefore, in the new version, all parameter data are stored in the FITS header of the resulting image.


You can read everything about all the new functionalities in the revised documentation you can download together with the updated script from the following link: 

https://www.dropbox.com/sh/75ky9j7tj2r08w9/AADMs4zYfUruqSPi7pQ54rFxa?dl=1 (https://www.dropbox.com/sh/75ky9j7tj2r08w9/AADMs4zYfUruqSPi7pQ54rFxa?dl=1)


I hope you like the new features and I would be happy to receive comments and suggestions for further improvement.

Kind regards,
Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: CharlesW on 2018 October 11 09:54:24
Win 10, fully updated copy of PI. I used the script for the first time. I found that the command window never released back to Ready, even after the new image had been created and the script had apparently stopped running. I was able to exit out of the script.
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2018 October 11 10:55:55
Hello Charles,

It's the first time I hear of a problem with the script. Can you please send me the console output of the script.

Kind regards,
Frank
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: CharlesW on 2018 October 11 14:29:29
It occurred on my Mac Pro also.
Title: Re: New Script: LocalFuzzyHistogramHyperbolization
Post by: FrankW on 2018 October 12 07:19:36
This looks normal. It is intended that the console will be left open after completing the calculations to view the log. The console does not close until you press the script's EXIT button. (As long as a script is open, no other process can be started anyway.)
During the calculation, all input fields and buttons turn gray, after the run they all become active again. If that happens, everything is ok.