试图渲染一个equirectangular全景

时间:2011-04-27 11:42:47

标签: c# image-processing panoramas

我有一个equirectangular全景源图像,它是360度经度和120度纬度。

我想编写一个可以渲染它的函数,给定视口的宽度和高度以及经度旋转。我希望我的输出图像能够达到120度的高度。

有没有人有任何指示?我无法理解如何从目标坐标转换回源数据。

感谢

到目前为止,这是我的代码: - (创建一个c#2.0控制台应用程序,添加一个参考system.drawing)

static void Main(string[] args)
    {

        Bitmap src = new Bitmap(@"C:\Users\jon\slippyr4\pt\grid2.jpg");

        // constant stuff
        double view_width_angle = d2r(150);
        double view_height_angle = d2r(120);
        double rads_per_pixel = 2.0 * Math.PI / src.Width;

        // scale everything off the height
        int output_image_height = src.Width;

        // compute radius (from chord trig - my output image forms a chord of a circle with angle view_height_angle)
        double radius = output_image_height / (2.0 * Math.Sin(view_height_angle / 2.0));

        // work out the image width with that radius.
        int output_image_width = (int)(radius * 2.0 * Math.Sin(view_width_angle / 2.0));

        // source centres for later
        int source_centre_x = src.Width / 2;
        int source_centre_y = src.Height / 2;

        // work out adjacent length
        double adj = radius * Math.Cos(view_width_angle / 2.0);

        // create output bmp
        Bitmap dst = new Bitmap(output_image_width, output_image_height);

        // x & y are output pixels offset from output centre
        for (int x = output_image_width / -2; x < output_image_width / 2; x++)
        {
            // map this x to an angle & then a pixel
            double x_angle = Math.Atan(x / adj);
            double src_x = (x_angle / rads_per_pixel) + source_centre_x;

            // work out the hypotenuse of that triangle
            double x_hyp = adj / Math.Cos(x_angle);

            for (int y = output_image_height / -2; y < output_image_height / 2; y++)
            {
                // compute the y angle and then it's pixel
                double y_angle = Math.Atan(y / x_hyp);
                double src_y = (y_angle / rads_per_pixel) + source_centre_y;

                Color c = Color.Magenta;
                // this handles out of range source pixels. these will end up magenta in the target
                if (src_x >= 0 && src_x < src.Width && src_y >= 0 && src_y < src.Height)
                {
                    c = src.GetPixel((int)src_x, (int)src_y);
                }


                dst.SetPixel(x + (output_image_width / 2), y + (output_image_height / 2), c);
            }
        }

        dst.Save(@"C:\Users\slippyr4\Desktop\pana.jpg");

    }

    static double d2r(double degrees)
    {
        return degrees * Math.PI / 180.0;
    }

source image i am using

使用此代码,当我将目标图像宽度设置为120度时,我得到了我期望的结果。我看到水平线的正确曲率等,如下所示,当我尝试使用真实的equirectangular全景时,它看起来像商业观众渲染。 correct output at 120 degrees by 120 degrees

但是,当我将输出图像放大时,一切都会出错。您开始在中心看到抛物线顶部和底部的无效像素,如图所示,图像宽150度,高120度: -

enter image description here

商业观众似乎做的是放大 - 所以在中间,图像是120度高,因此在侧面,更多被剪裁。因此,没有洋红色(即没有无效的源像素)。

但我无法理解如何在数学中做到这一点。

这不是家庭作业,这是一个爱好项目。因此,为什么我缺乏对正在发生的事情的理解!另外,请原谅代码的严重缺陷,我会在它工作时优化它。

再次感谢

0 个答案:

没有答案