2017-07-19 88 views
0

我有一个关于OSRM-Project中双线性插值的问题。 我明白“正常”双线性插值。这里从维基百科的图片,什么是疯狂:双线性插值 - OSRM Rastersource

Bilinear Interpolation - Wikipedia

现在我想明白这是在OSRM项目中使用的栅格源数据的双线性插值。

// 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

有人可以解释我的代码是如何工作的?

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

变量高度宽度被定义为NROWSncolumns。 变量XSTEPystep被定义为:

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

计数ystep宽度XSTEP最大分钟类似高度

而另一个问题: 我可以在TIF格式和全世界使用相同的代码数据吗?

+0

如果如你所说,你已经理解双线性插值了,那么你当然应该已经理解了这个代码的大部分? – meowgoesthedog

+0

好的,也许我应该清楚:我理解双线性插值背后的理论。我理解这段代码的基本逻辑,但我不明白为什么变量“底部”是为什么计算的,尤其是“身高 - 1”。以及“fromX”变量的计算如何工作。 –

回答

1

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

线条

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

转换输入图像空间坐标(long, lat)像素坐标xstep图片空间中每个像素的宽度。

将其舍入(使用floor)会使像素与样本区域在一侧相交,并向上舍入(ceil),从而使像素位于另一侧。对于X坐标,这些给出leftright

之所以使用fminfmax坐标,使它们不超过像素坐标范围。


编辑:因为你是试图解释这幅画,我将列出下面相应的部分:

  • 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.在今后虽然,谷歌是你的朋友。

+0

好的!但是“从右= 1 - 从左到右”是什么?和“fromBottom = 1 - fromTop;”?为什么是“1 - x”? –

+0

@DanielChristoph那么你显然并没有理解双线性插值的实现。这些计算与采样框任一侧上的像素的*分数*相交,然后一起使用以计算由它重叠的每个像素占据的采样框的分数面积。这些区域然后用作插值的权重;所以基本上你只理解代码的最后一行。 – meowgoesthedog

+0

@DanielChristoph btw从维基百科的图片并不真正帮助你的情况,因为标签是非常不同的。我建议你在代码的上下文中忽略它。 – meowgoesthedog