将世界坐标系转换为笛卡尔坐标系的问题

4
我有两张来自WFC3的fits图像,我正在尝试用C#程序将它们结合起来。当我尝试合并多个图像时,我认为从赤经/赤纬计算得出的x/y坐标值不正确。我期望最终图像的宽度与两个图像相同,但实际上它的宽度大约是两倍,高度也是两倍。我知道最终图像应该是单个图像宽度的两倍,因为我手动在Photoshop中组合了这些图像,最终图像的宽度大约是两个原始图像的宽度之和。
注意:当我说“图像”时,它们是fits图像,因此它们只是文件中的一堆单个值,因此要将它们组合起来,我创建一个新文件,并将正确数量的单个值(宽度*高度)初始化为零,然后填入我要使用的图像中的值。它们不是jpg或tif或png。
我使用以下公式从世界坐标系转换为笛卡尔坐标: 公式是(因为距离对于所有内容都是相同的): x = cos(dec)*cos(ra) y = cos(dec)*sin(ra)
我从fits文件的头部获得赤经和赤纬。
对于最终图像的尺寸,我计算x1和x2之间的距离,并创建一个新图像,其宽度为1/2图像1宽度+距离+1/2图像2宽度。对于最终高度,我使用y和图像高度进行类似的计算。
这些图像也有旋转部分,但由于两个图像具有相同的旋转,我忽略了这一点。这可能是问题的一部分。
public const double PixelsPerArcSecond = .039; // per WFC3 spec from Nasa

public static ImageDataModel Combine(List<ImageDataModel> inputImages)
{
    //  Right ascension is CRVAL1
    //  Declination is CRVAL2

    //  formula is (since distance is the same for everything):
    //     x = cos(dec) * cos(ra)
    //     y = cos(dec) * sin(ra)

    ImageDataModel returnImage = new ImageDataModel();
    ImageDataModel bm = inputImages[0];

    double x1, y1, x2, y2;

    x1 = Math.Cos(bm.CRVAL2) * Math.Cos(bm.CRVAL1);
    y1 = Math.Cos(bm.CRVAL2) * Math.Sin(bm.CRVAL1);

    int mult = 4; // todo: set this based off of the bitpix of the incoming images.

    for (int i = 1; i < inputImages.Count; i++)
    {
        ImageDataModel cm = inputImages[i];

        x2 = Math.Cos(cm.CRVAL2) * Math.Cos(cm.CRVAL1);
        y2 = Math.Cos(cm.CRVAL2) * Math.Sin(cm.CRVAL1);

        double dx = x1 - x2;
        double dy = y1 - y2;

        int distX = (int)((dx * 3600) / PixelsPerArcSecond);
        int distY = (int)((dy * 3600) / PixelsPerArcSecond);

        // This is what I expect to be wider than tall, but the converse is true.
        int w = Math.Abs(distX) + (bm.ImageWidth / 2) + (cm.ImageWidth / 2);
        int h = Math.Abs(distY) + (bm.ImageHeight / 2) + (cm.ImageHeight / 2);
        // This is where the two images are combined into the final image.
        ImageDataModel imd = CombineTwoImages(bm, cm, i, w, h, mult);
        bm = imd;
    }

    return returnImage;
}

我期望得到一个像这样的图片:
http://wierdling.net/stack-overflow-images/ManuallyCombined.png 但是得到的却是这个:
http://wierdling.net/stack-overflow-images/CombinedTest.png 第一张图片的统计信息:宽度=4139,高度=4535,RA=350.1584456860353(CRVAL1),DEC=61.16155335032816(CRVAL2),ORIENTAT=-125
第二张图片的统计信息:宽度=4139,高度=4535,RA=350.1159150008405(CRVAL1),DEC=61.19543100394401(CRVAL2),ORIENTAT=-125
最终期望的宽度接近7733,高度接近4773。
最终实际的宽度为4284,高度为7662。
有人知道我做错了什么吗?
程序的完整源代码可以从https://bitbucket.org/wierdling/fitscombiner/src/master/下载。
它目前只能处理WFC3数据,而且该程序还在不断改进中。

1
请注意,您的代码需要登录才能查看。最好使用像PasteBin或类似服务(例如Github的Gists)。这将为观看者提供即时的代码可读性,而无需账户、登录等。 - VC.One
1个回答

1
我认为你的图像程序已经完成了旋转,所以你也应该这样做。
如果我将你计算出来的坐标旋转125度,然后计算x1和x2的左侧距离以及y坐标的相同距离,我得到宽度6725和高度6166。
虽然不是完美的,但我认为它朝着正确的方向发展。
希望有所帮助。
    public static ImageDataModel Combine(List<ImageDataModel> inputImages)
    {
        //  Right ascension is CRVAL1
        //  Declination is CRVAL2

        //  formula is (since distance is the same for everything):
        //     x = cos(dec) * cos(ra)
        //     y = cos(dec) * sin(ra)

        ImageDataModel returnImage = new ImageDataModel();
        ImageDataModel bm = inputImages[0];

        double x1, y1, x2, y2;

        x1 = Math.Cos(bm.CRVAL2) * Math.Cos(bm.CRVAL1);
        y1 = Math.Cos(bm.CRVAL2) * Math.Sin(bm.CRVAL1);
        var values = Rotate(0 - bm.Orientation, x1, y1);
        x1 = values.x;
        y1 = values.y;
        int mult = 4; // todo: set this based off of the bitpix of the incoming images.

        for (int i = 1; i < inputImages.Count; i++)
        {
            ImageDataModel cm = inputImages[i];

            x2 = Math.Cos(cm.CRVAL2) * Math.Cos(cm.CRVAL1);
            y2 = Math.Cos(cm.CRVAL2) * Math.Sin(cm.CRVAL1);
            var values2 = Rotate(0 - bm.Orientation, x2, y2);

            x2 = values2.x;
            y2 = values2.y;

            double dx = x1 - x2;
            double dy = y1 - y2;

            int distX = (int)((dx * 3600) / PixelsPerArcSecond);
            int distY = (int)((dy * 3600) / PixelsPerArcSecond);

            double width = (1.0 + x1) * (bm.ImageWidth / 2) + (1.0 - x2) * (cm.ImageWidth / 2) + Math.Abs(distX);
            double height = (1.0 + y1) * (bm.ImageHeight / 2) + (1.0 - y2) * (cm.ImageHeight / 2) + Math.Abs(distY);
            // This is what I expect to be wider than tall, but the converse is true.
            int w = Math.Abs(distX) + (bm.ImageWidth / 2) + (cm.ImageWidth / 2);
            int h = Math.Abs(distY) + (bm.ImageHeight / 2) + (cm.ImageHeight / 2);
            // This is where the two images are combined into the final image.
            ImageDataModel imd = CombineTwoImages(bm, cm, i, w, h, mult);
            bm = imd;
        }

        return returnImage;
    }

    private static (double x, double y) Rotate(int angle, double x, double y)
    {
        double rad = Math.PI * angle / 180.0;

        return (x * Math.Cos(rad) - y * Math.Sin(rad), x * Math.Sin(rad) + y * Math.Cos(rad));
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接