Author Topic: (in)Compatibility of fits floating point data format in PixInsight  (Read 9088 times)

Offline gvanhau

  • PixInsight Old Hand
  • ****
  • Posts: 345
Hello
I've been analyzing a lot of documentation about the fits format, the way PixInsight handles it and some issues around it.

I was surprised  when surfing in the web, came around with a statement like "How the default PixInsight rescaling affects color." from the  AstroPhotographer Bob Franke, but after contacting him, realized that the problem he found was real.

I also read this thread http://pixinsight.com/forum/index.php?topic=361.msg1370#msg1370  which is very interesting because there is a mention to the problems related to fits format  and the PI underlying [0,1] scaling.

The main problem is that while the world (everyone except PixInsight) reads fits floating point data as absolute values, PixInsight interpret them as relative values (ie relative to white point and black point).
Remember that Fits Means Flexible Image Transport System, and that Images consist of pixels and that each pixel is represented by one or more intensities (and not a relative values).
So when the boundaries are not established like in fixed point formats the internal scaling procedure of PixInsight fails, and we get issues as the stated by Bob Franke.

PixInsight invented a work around to solve the read issue, forcing the user to introduce a range in format explorer, and so allowing a correct scaling of the data, but this has its own issues:

If for example in format explorer I set  the range values to 0 and 65536,  then reading of (some) foreign images performs ok. I tested this with Bob's images and the color scaling problem disappeared. But what if that was not the correct range? 
Even worse:  with this setting now PixInsigth own generated images, are also read and scaled in the 0,65536 range, and they now appear almost completely black because they were written in the 0-1 range.  Imagine the mess having for each image to manipulate the read range in order to get them read ok, imagine if you have a script reading and writing images....

There is also the write issue:  all fits floating point data is written by PixInsight in 0-1 range (absolute) so, if you read any image in PixInsight and then write it to a floating point fits file, the data is altered.
All floating point fits format images written by PixInsight are read as almost pure black in other programs. I tested this with images which where stretched and ready for printing and got almost black results in other programs.

Geert
Geert Vanhauwaert

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
This topic has been discussed ad nauseam on this forum. So let's disassemble this fallacy once more.

Quote
The main problem is that while the world (everyone except PixInsight) reads fits floating point data as absolute values, PixInsight interpret them as relative values (ie relative to white point and black point).

PixInsight is an image processing application, hence it needs to know the black and white points of an image in order to correctly represent and handle its pixel data.

Reading "absolute values", as you describe it, can be enough for a data array manipulation application, although this can be questionable. An image processing application, however, definitely needs to work with pixel sample values relative to the numerical ranges where the images are being represented. So perhaps the problem is with this "world except PixInsight" that you mention.

This is really a basic point; if it has to be explained or justified further, then I would recommend some bibliography on basic image processing topics.

Quote
So when the boundaries are not established like in fixed point formats the internal scaling procedure of PixInsight fails, and we get issues as the stated by Bob Franke.

No, the internal scaling procedures of PixInsight don't fail (modulo bugs, of course). The issues you are referring to are mainly due to lack of knowledge about PixInsight. They are also caused, IMHO, by wrong preconceived ideas about PixInsight and the superiority complex that plagues part of the astrophotography community, but this is a different story.

Quote
PixInsight invented a work around to solve the read issue, forcing the user to introduce a range in format explorer, and so allowing a correct scaling of the data

Again, I disagree. PixInsight does not "force" anything. On the contrary, our implementations of the FITS and TIFF formats are flexible enough to allow you to import virtually any image in PixInsight, in any numeric range, generated by virtually any application.

Instead of hard coding numeric ranges in FITS or TIFF preferences, you can use format hints to define them locally in tools such as ImageCalibration, ImageIntegration, StarAlignment, etc. Format hints are even more flexible and adaptable.

Quote
Imagine the mess having for each image to manipulate the read range in order to get them read ok, imagine if you have a script reading and writing images....

The mess certainly exists and is being caused by deficiencies in the FITS format specification, which we have discussed uncountable times on this forum, along with inconsistent and/or undocumented implementations in other applications. These deficiencies generate interoperability problems, which are more "visible" in PixInsight simply because PixInsight is more complex and imposes more strict constraints, as described above.

One of the wrong preconceived ideas about PixInsight that we see more often is that it only works in the [0,1] range internally. This is simply not true. A real or complex floating point image can use an arbitrary range in PixInsight. A script or module can read floating point FITS images in memory exactly as they are stored in the source files without any transformation. So this problem simply does not exist from a programming perspective, especially considering that all of the code involved is open-source.

Quote
There is also the write issue:  all fits floating point data is written by PixInsight in 0-1 range (absolute) so, if you read any image in PixInsight and then write it to a floating point fits file, the data is altered.

Numerical values are not sacred tokens; they are useful only if they can be meaningfully interpreted and manipulated, and representing them in different ranges and scales does not change their meaning, as long as the applied representations are documented so that everybody can use them correctly.

The data are not altered because rescaling to a different range is a linear operation. Floating point pixel samples are simply represented relative to a particular real range, which has been documented. Other applications use different ranges to store floating point and integer data, which in some cases remain undocumented. However, you don't say that they are altering the data, why?

Quote
All floating point fits format images written by PixInsight are read as almost pure black in other programs. I tested this with images which where stretched and ready for printing and got almost black results in other programs.

This happens because, contrarily to PixInsight, these other programs are unable to interpret floating point pixel data relative to an arbitrary numeric range. We publicize and document the numeric ranges that we use consistently, while others don't. In addition, our FITS format support module is an open-source product, whose source code can be studied, analyzed and contributed:

https://github.com/PixInsight/PCL/tree/master/src/modules/file-formats

Finally, we have started the XISF initiative to create an open-source, free, efficient and interoperable format, where all of these problems cannot happen by design.

Other programs are simply not flexible enough, so in this case the problem is theirs, not ours.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Aircrftr

  • Newcomer
  • Posts: 20
I've been using PixInsight for a little over a year now and I've had trouble with FITs fomat every since I've started.  What I've been doing is converting all my subs to TIFF and they work perfectly.  What I get when I load a FITS file ( from MaxIm Dl) is a *very* narrow histogram where the bulk of the image is.  This makes it nearly impossible to adjust things like the light mask in ACDNR and the histogram Transformation.. In ACDNR, I can't move the slider the slightest bit in one direction without going from completely white to completely dark.  Same goes for the histogram transformation..  I've included a screen shot with the Fits header and what the histogram looks like on the HT process...  What settings should my fits files be in order to be workable in PI?  Again, when I convert them to TIFF, I can manipulate them just fine...
« Last Edit: 2016 January 10 22:31:33 by Aircrftr »

Offline vicent_peris

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 988
    • http://www.astrofoto.es/
I've been using PixInsight for a little over a year now and I've had trouble with FITs fomat every since I've started.  What I've been doing is converting all my subs to TIFF and they work perfectly.  What I get when I load a FITS file ( from MaxIm Dl) is a *very* narrow histogram where the bulk of the image is.  This makes it nearly impossible to adjust things like the light mask in ACDNR and the histogram Transformation.. In ACDNR, I can't move the slider the slightest bit in one direction without going from completely white to completely dark.  Same goes for the histogram transformation..  I've included a screen shot with the Fits header and what the histogram looks like on the HT process...  What settings should my fits files be in order to be workable in PI?  Again, when I convert them to TIFF, I can manipulate them just fine...

Hi,

If you're working with Maxim DL, please save the FITS files in unsigned 16-bit integer.

Regards,
Vicent.

Offline Aircrftr

  • Newcomer
  • Posts: 20
Hi Vicent;

I've tried tonight saving files in MaxIm as 16 bit Unsigned and it appears to do nothing but just move the very thin histogram on the chart.  Still too thin to do any adjustment to...  I have a screen cap that shows the fits header, how the image looks with STF applied and the file setting in MaxIm to the RH side of the pic....   I'll post another with an image done the normal way in MaxIm with 16 bit signed...

Offline Aircrftr

  • Newcomer
  • Posts: 20
Here is the screen shot with a file done the normal way in MaxIm with 16 bit signed. 

Offline Aircrftr

  • Newcomer
  • Posts: 20
Here is what my "Save As" menu allows me to save as in MaxIm..  (other than the file specifications in the settings as posted in the above pics)

Offline pfile

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 4729
unfortunately the best practice here is to save as 16-bit unsigned. most if not all of us are doing that and PI works fine with fits files formatted in such a way.

rob

Offline Aircrftr

  • Newcomer
  • Posts: 20
Hi Rob;

So are you getting the 16 Bit Unsigned files out of MaxIm?  Or are you using another program for capture?

Dan

Offline pfile

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 4729
yes, 16-bit unsigned from maxim driven by ACP... camera is FLI16803.

rob

Offline Aircrftr

  • Newcomer
  • Posts: 20
Re: (in)Compatibility of fits floating point data format in PixInsight
« Reply #10 on: 2016 January 11 21:01:00 »
Thanks Rob, Evidently I'm doing something wrong... If  you look at the jpg I posted above that says "16 bit Unsigned.jpg" do you have your checkboxes like I do in MaxIm on the RH side of that pic?  and do your histograms look that skinny?

Dan

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: (in)Compatibility of fits floating point data format in PixInsight
« Reply #11 on: 2016 January 12 01:23:50 »
As for histogram shapes in your screenshots, don't worry: they are perfectly normal for a linear image. I would say they are reasonably wide, actually, compared to most raw images.

Now the FITS compatibility thing. You have two valid options to import alien[1] 16-bit integer FITS images in PixInsight:

BZERO = 32768 / BSCALE = 1

This is the preferred option. It will work with PixInsight's default FITS settings. In this format, alien pixel data will be interpreted as 16-bit unsigned integers in the [0,65535] range, which is perfectly fine.

This is the well-known BZERO trick, a convention used to store unsigned integer data in FITS files. This is necessary because FITS does not support unsigned integer formats natively.

Some applications set BZERO to different values by mistake (supposedly), such as 32767 for example, or even stranger ones, which is legal as per the FITS standard, since these keywords can take arbitrary values. This causes all kinds of interoperability problems.

BZERO = 0 / BSCALE = 1

This is the native integer FITS format. Note that this applies also when BZERO and BSCALE are not defined, since 0 and 1 are the default values of these keywords, respectively. In this format pixel data will be interpreted as signed 16-bit integers.

AFAIK, other applications always truncate the negative half of the signed range. To import these alien images correctly, the signed integer images store physical pixel data option must be enabled in FITS Format Preferences.

Since the most significant bit of each pixel sample value is used to store the sign, your alien image will be stored with an effective bit depth of 15 bits, that is, with pixel sample values in the [0,32767] range. Note that this is not true if you save an image in signed 16-bit integer format with PixInsight, which will always store pixel values using the whole [-32768,+32767] signed range, where -32768 represents black and +32767 is white.

Loading signed integer alien FITS images is not recommended because of potential interoperability problems, unless strictly required (some applications don't give other options). The same applies for output: never write signed integer FITS images with PixInsight, for the same reason. By default, the FITS format support module will write unsigned integer images using the BZERO trick.

Your "16bit_unsigned.JPG" screenshot clearly shows what happens when signed integer data are wrongly interpreted. Note the histogram peak located about the middle of the available range, which is obviously wrong. Curiously, you have enabled a "save as unsigned" option in this case, so I don't know why BZERO = 0 in the resulting image. Puzzling.

All of these problems do not exist with the XISF format, where numeric formats, ranges and their interpretations are strictly and unequivocally defined. Unfortunately (but expectedly) other applications show little interest in XISF, for now.


[1] Alien data: data generated by other applications.
« Last Edit: 2016 January 12 01:35:48 by Juan Conejero »
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Aircrftr

  • Newcomer
  • Posts: 20
Re: (in)Compatibility of fits floating point data format in PixInsight
« Reply #12 on: 2016 January 12 05:56:22 »
Juan,  Thanks for the reply.  How do I verify the Bzero/Bscale of the fits I import?   Also, all this time, I've been using TIFF files and getting great workability as far as all the processes being able to work with all the sliders in a linear fashion. With the TIFF format, the hump in the histogram is nice and wide and I can make fine adjustments with the sliders.  When I use the TIFF format, am I losing data/quality?
 With the histogram being like in the 16 bit Unsigned example I posted, I can't move the histogram slider in small enough increments to actually adjust the histogram.  The tiniest bit of movement with the mouse near the hump changes the image from completely black to completely white, as does the same in ACDNR with the masking function...

P.S.  Sorry to the original poster for hijacking the thread...  I was thinking we had the same problem...

Dan


Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: (in)Compatibility of fits floating point data format in PixInsight
« Reply #13 on: 2016 January 12 06:32:25 »
Quote
How do I verify the Bzero/Bscale of the fits I import?

By inspecting the FITS header keywords after loading the image. You can use the FITSHeader tool, or even faster, select the image and enter the lskwd in the Process Console window. Make sure that BZERO is 32768.

For example, your "16bit_signed.JPG" screenshot shows an image correctly imported in 16-bit unsigned integer format. Judging by this screenshot, it seems MaxIm DL is using a sort of "inverse logic" in this case, since it writes unsigned integers with the "Save Data in Unsigned Format" option disabled. Anyway, this is how you have to write your raw images.

Quote
With the TIFF format, the hump in the histogram is nice and wide

If that happens, most likely the data are no longer linear. If the histograms of a deep-sky image are "nice and wide", then you can bet that a nonlinear transformation has been applied. Definitely, this is not what you want if you want to get full control over your data.

Narrow histogram peaks are absolutely normal and common in linear deep-sky images. I strongly recommend you read/watch some tutorials about basic image stretching tasks. There are many good tutorials and processing examples available online.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Aircrftr

  • Newcomer
  • Posts: 20
Re: (in)Compatibility of fits floating point data format in PixInsight
« Reply #14 on: 2016 January 12 06:43:54 »
Ok, Thanks Juan.  I'll play with it tonight and see what I can come up with. 

Much appreciated!  Dan