C# 正在尝试渲染等矩形全景
我有一个等矩形的全景源图像,它是360度经度和120度纬度 我想写一个函数来渲染这个,给定视口的宽度和高度以及经度旋转。我想把我的输出图像调整到120度的高度 有人有什么建议吗?我无法理解如何从目标坐标转换回源坐标的数学问题 谢谢 滑倒 这是我到目前为止的代码:-(创建一个c#2.0控制台应用程序,向system.drawing添加一个引用)C# 正在尝试渲染等矩形全景,c#,image-processing,panoramas,C#,Image Processing,Panoramas,我有一个等矩形的全景源图像,它是360度经度和120度纬度 我想写一个函数来渲染这个,给定视口的宽度和高度以及经度旋转。我想把我的输出图像调整到120度的高度 有人有什么建议吗?我无法理解如何从目标坐标转换回源坐标的数学问题 谢谢 滑倒 这是我到目前为止的代码:-(创建一个c#2.0控制台应用程序,向system.drawing添加一个引用) static void Main(字符串[]args) { 位图src=新位图(@“C:\Users\jon\sliptyr4\pt\grid2.jpg”
static void Main(字符串[]args)
{
位图src=新位图(@“C:\Users\jon\sliptyr4\pt\grid2.jpg”);
//不变的东西
双视图\宽度\角度=d2r(150);
双视图高度角度=d2r(120);
双拉德每像素=2.0*Math.PI/src.Width;
//把所有东西都按高度缩放
int输出\图像\高度=src.宽度;
//计算半径(从弦三角-我的输出图像形成一个具有角度视图\高度\角度的圆弦)
双半径=输出图像高度/(2.0*Math.Sin(视图高度角度/2.0));
//用这个半径计算出图像的宽度。
int输出图像宽度=(int)(半径*2.0*Math.Sin(视图宽度\角度/2.0));
//来源中心供以后使用
int source_CENTER_x=钢筋混凝土宽度/2;
int source_CENTER_y=src.Height/2;
//计算相邻长度
双调整=半径*数学坐标(视图宽度角度/2.0);
//创建输出bmp
位图dst=新位图(输出图像宽度、输出图像高度);
//x和y是从输出中心偏移的输出像素
对于(int x=输出图像宽度/-2;x<输出图像宽度/2;x++)
{
//将这个x映射到一个角度,然后是一个像素
双x_角=数学坐标(x/adj);
双src_x=(x_角度/rads_每像素)+源中心;
//算出那个三角形的斜边
双x_hyp=adj/Math.Cos(x_角);
对于(int y=输出图像高度/-2;y<输出图像高度/2;y++)
{
//计算y角,然后得到像素
双y_角度=数学Atan(y/x_hyp);
双src_y=(y_角度/rads_每像素)+源中心y;
颜色c=颜色。洋红色;
//这将处理超出范围的源像素。这些像素将在目标中变成洋红色
如果(src_x>=0&&src_x=0&&src_y
使用这段代码,当我将目标图像宽度设置为120度时,我得到了预期的结果。我看到了水平线的右曲率等,如下所示,当我尝试使用真实的等矩形全景时,它看起来像是商业观众渲染的。
但是,当我将输出图像放大时,一切都会出错。您开始在中心的抛物线顶部和底部看到无效像素,如图所示,图像宽150度,高120度:-
商业观众所做的似乎是放大——因此在中间,图像是120度高,因此在侧面,更多的被剪辑。因此,没有洋红(即,没有无效的源像素)
但我不知道如何在数学课上做到这一点
这不是家庭作业,这是一个爱好项目。因此,我对正在发生的事情缺乏了解!。另外,请原谅代码的严重低效性,我会在它正常工作时对其进行优化
再次感谢您能给出您的代码吗?还是我们的家庭作业…?我现在已经添加了代码和示例图像。这不是家庭作业,我只是希望一些文章的指针能帮助我找出正确的数学,而不是让别人给我答案。。。因此,我没有发布我所有的工作。事实上,尽管谷歌搜索了很多,我还是很难找到很多关于这方面的信息。我想我没有在寻找正确的东西。你能给出你的代码吗?还是给我们做作业…?我现在已经添加了代码和示例图像。这不是家庭作业,我只是希望一些文章的指针能帮助我找出正确的数学,而不是让别人给我答案。。。因此,我没有发布我所有的工作。事实上,尽管谷歌搜索了很多,我还是很难找到很多关于这方面的信息——我想我没有在寻找正确的东西。
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;
}