我有一个 360 度经度和 120 度纬度的 equirectangular 全景源图像。
我想编写一个函数来渲染它,给定视口的宽度和高度以及经度旋转。我想将我的输出图像设置为完整的 120 度高度。
有没有人有任何指示?我无法理解如何从目标坐标转换回源坐标的数学。
谢谢
滑
到目前为止,这是我的代码:-(创建 ac# 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;
}
使用此代码,当我将目标图像宽度设置为 120 度时,我得到了预期的结果。我看到水平线等的正确曲率,如下所示,当我尝试使用真实的等距矩形全景图时,它看起来就像商业观众渲染的那样。
但是,当我使输出图像更宽时,一切都出错了。您开始在中心的抛物线顶部和底部看到无效像素,如下所示,图像宽 150 度,高 120 度:-
商业观众似乎做的是放大 - 所以在中心,图像是 120 度高,因此在侧面,更多的被剪裁了。因此,没有洋红色(即没有无效的源像素)。
但我无法理解如何在数学中做到这一点。
这不是家庭作业,这是一个爱好项目。因此,为什么我缺乏对正在发生的事情的理解!另外,请原谅代码的严重低效,当我让它正常工作时,我会优化它。
再次感谢