【发布时间】: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))};
}
谁能解释一下代码是如何工作的?
输入格式为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