【问题标题】:Bilinear Interpolation - OSRM Rastersource双线性插值 - OSRM Rastersource
【发布时间】:2017-07-19 11:18:21
【问题描述】:

我有一个关于 OSRM 项目中双线性插值的问题。 我了解“正常”双线性插值。这里的图片来自维基百科,什么是疯狂的:

现在我正在尝试了解在 OSRM-Project 中用于栅格源数据的双线性插值。

// Query raster source using bilinear interpolation
RasterDatum RasterSource::GetRasterInterpolate(const int lon, const int lat) const
{
if (lon < xmin || lon > xmax || lat < ymin || lat > ymax)
{
    return {};
}

const auto xthP = (lon - xmin) / xstep;
const auto ythP =
    (ymax - lat) /
    ystep; // the raster texture uses a different coordinate system with y pointing downwards

const std::size_t top = static_cast<std::size_t>(fmax(floor(ythP), 0));
const std::size_t bottom = static_cast<std::size_t>(fmin(ceil(ythP), height - 1));
const std::size_t left = static_cast<std::size_t>(fmax(floor(xthP), 0));
const std::size_t right = static_cast<std::size_t>(fmin(ceil(xthP), width - 1));

// Calculate distances from corners for bilinear interpolation
const float fromLeft = xthP - left; // this is the fraction part of xthP
const float fromTop = ythP - top;   // this is the fraction part of ythP
const float fromRight = 1 - fromLeft;
const float fromBottom = 1 - fromTop;

return {static_cast<std::int32_t>(raster_data(left, top) * (fromRight * fromBottom) +
                                  raster_data(right, top) * (fromLeft * fromBottom) +
                                  raster_data(left, bottom) * (fromRight * fromTop) +
                                  raster_data(right, bottom) * (fromLeft * fromTop))};
}

Original Code here

谁能解释一下代码是如何工作的?

输入格式为SRTM ASCII 格式的数据。

变量height和width被定义为nrows和ncolumns。 变量xstep和ystep定义为:

return (max - min) / (static_cast<float>(count) - 1)

其中 count 对于 ystep 是 height,对于 xstep 是 width,max 和 min 类似。

还有一个问题: 我可以对 TIF 格式 和整个世界的数据使用相同的代码吗?

【问题讨论】:

  • 如果如你所说,你已经了解双线性插值,那么你肯定应该已经了解大部分代码了吗?
  • 好吧,也许我应该清楚:我了解双线性插值背后的理论。我理解这段代码的基本逻辑,但我不明白为什么,例如变量“bottom”是这样计算的,尤其是“height - 1”。以及“fromX”变量的计算是如何工作的。

标签: c++ raster osrm bilinear-interpolation


【解决方案1】:

水平像素坐标在[0, width - 1]范围内;同样的垂直坐标在[0, height - 1]。 (包括 C++ 在内的许多语言中使用的零索引约定)

线条

const auto xthP = (lon - xmin) / xstep;(以及ythP)

将输入的图像空间坐标(long, lat)转换为像素坐标。 xstep 是 image-space 中每个像素的宽度。

向下舍入(使用floor)给出与一侧的样本区域相交的像素,向上舍入(ceil)给出另一侧的像素。对于 X 坐标,这些给出 left 和 right。

使用fmin和fmax的原因是clamp坐标,使它们不超过像素坐标范围。


编辑:由于您试图解释这张图片,我将在下面列出相应的部分:

  • Q11 = (left, top)
  • Q12 - (left, bottom) 等
  • P = (xthP, ythP)
  • R1 = fromTop、R2 = fromBottom 等

http://www.cs.uu.nl/docs/vakken/gr/2011/Slides/06-texturing.pdf,幻灯片 27 是一个很好的起点。不过,Google 是您未来的朋友。

【讨论】:

  • 好的!但是“fromRight = 1 - fromLeft;”是怎么回事?和“fromBottom = 1 - fromTop;”?为什么是“1 - x”?
  • @DanielChristoph 那么您显然还没有理解实现上下文中的双线性插值。这些计算与采样框两侧像素的小数交集,然后一起用于计算其重叠的每个像素所占据的采样框的小数面积。然后将这些区域用作插值的权重;所以本质上你只了解代码的最后一行。
  • @DanielChristoph 顺便说一句,来自维基百科的图片并不能真正帮助您解决问题,因为标签非常不同。我建议你在这段代码的上下文中忽略它。
  • 好吧,也许这是我的问题。我正在尝试将 OSRM 中的代码“翻译”为这张图片。
  • 你有很好的资源吗,我可以在其中学习实现上下文中的双线性插值?
猜你喜欢
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多