【问题标题】:CSharpFits: Writing FITS-file results in wrong imageCSharpFits:写入 FITS 文件会导致图像错误
【发布时间】:2021-01-12 04:21:39
【问题描述】:

我目前正在做一个 C# 项目来减少阶梯光谱,这些光谱基本上是 FITS 格式的 2D 16 位图像。就目前而言,最终结果应该是另一个 FITS 文件,但是是一维的。

对于这项任务,我正在使用 CSharpFits 库 (https://github.com/SidWatch/CSharpFITS),它是 Java 实现 (https://github.com/nom-tam-fits/nom-tam-fits) 的一个端口,所有文档都位于该库中。

我仍处于此库的学习阶段。到目前为止,我成功地可以读取 FITS 文件,将其转换为 ints 数组的数组并处理数据。但是尝试编写 FITS 文件给我带来了一个我无法理解的特殊问题。下面的代码,作为一个玩具示例,产生如图所示的结果。

private void SaveFits()
{
    int[][] array = new int[10][];
    int delta = 65536 / 100;
    for (int i = 0; i < array.Length; i++)
    {
        array[i] = new int[10];
        for (int j = 0; j < array[i].Length; j++)
        {
            array[i][j] = (i + 1) * (j + 1) * delta;
         }
    }
    nom.tam.fits.Fits fits = new nom.tam.fits.Fits();
    nom.tam.fits.BasicHDU hdu = nom.tam.fits.Fits.MakeHDU(array);
    hdu.AddValue("BITPIX", 16, null);  // set bit depth of 16 bit
    hdu.AddValue("NAXIS", 2, null);    // 2D-image
    fits.AddHDU(hdu);                  // Debugging here shows correct HDU-data, i.e. gradient from top left to bottom right
    nom.tam.util.BufferedFile file = new nom.tam.util.BufferedFile(@"D:\test.fits", System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
    fits.Write(file);  
    file.Flush();
    file.Close();
}

pixel-representation of 10-by-10-pixel FITS-file

如您所见,代码应生成一个 16 位图像,从左上角到右下角具有灰度渐变。创建 HDU 后,其数据数组的控制台输出读取

655   1310  1965  2620  3275  3930  4585  5240  5895  6550
1310  2620  3930  5240  6550  7860  9170 10480 11790 13100
1965  3930  5895  7860  9825 11790 13755 15720 17685 19650
2620  5240  7860 10480 13100 15720 18340 20960 23580 26200
3275  6550  9825 13100 16375 19650 22925 26200 29475 32750
3930  7860 11790 15720 19650 23580 27510 31440 35370 39300
4585  9170 13755 18340 22925 27510 32095 36680 41265 45850
5240 10480 15720 20960 26200 31440 36680 41920 47160 52400
5895 11790 17685 23580 29475 35370 41265 47160 53055 58950
6550 13100 19650 26200 32750 39300 45850 52400 58950 65500

然而在最终图像中(在 PixInsight 中查看,但任何其他可以显示 FITS 图像的程序都显示相同)每隔一列只有值为 0 的像素,并且每隔一行重复第一行。

到目前为止,我已经尝试将 HDU 实际创建为 ImageHDU,但没有任何效果。 我还检查了正确读取的 FITS 文件的 HDU-header-fields,并尝试在我的 (hdu.AddValue) 中手动设置它们,但到目前为止没有任何效果。 可以在此处找到几个使用 CSharpFITS 编写 FITS 文件的示例 (https://csharp.hotexamples.com/examples/nom.tam.fits/Fits/Write/php-fits-write-method-examples.html),但除了我所做的之外,没有其他任何东西可以使用。

非常感谢您的建议。谢谢!

【问题讨论】:

  • 我不熟悉这个库,所以我不能肯定地说:你会认为如果你明确地将 BITPIX 标头设置为 16,它可能会将数据转换为正确的类型,但不是一定。更有可能的是,如果它像 pyfits 一样工作,它会根据数据数组的数据类型自动设置 BITPIX 标头。而不是 int 尝试使用数组的 short 类型来明确表示它是 16 位数据。
  • 阅读documentation for nom.tam.fits 似乎确实遵循了尽可能隐藏 FITS 细节的相同“一般哲学”。因此,不要设置像NAXISBITPIX 这样的“结构”关键字,而应该只传递一个数组,它会根据数组结构/数据类型将这些关键字设置为适当的值。

标签: c# fits


【解决方案1】:

根据 Iguananauts 的评论,将 int 数组更改为 short 数组会得到预期的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2013-03-22
    相关资源
    最近更新 更多