我在OSRM-Project中有一个关于双线性插值的问题。
我了解“正常”双线性插值。这是维基百科的图片,简直是疯了:
现在,我试图理解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
有人可以向我解释代码的工作原理吗?
输入格式是ASCII格式的SRTM数据。
变量height和width定义为nrows和ncolumns。
变量xstep和ystep定义为:
return (max - min) / (static_cast<float>(count) - 1)
其中count是ystep的高度,xstep的宽度,最大值和最小值相似。
还有一个问题:
我可以对TIF格式的数据和整个世界使用相同的代码吗?
最佳答案
水平像素坐标在[0, width - 1]
范围内;同样,垂直坐标在[0, height - 1]
中。 (零索引约定在包括C ++在内的许多多种语言中使用)
线const auto xthP = (lon - xmin) / xstep;
(对于ythP
)
将输入的图像空间坐标(long, lat)
转换为像素坐标。 xstep
是图像空间中每个像素的宽度。
向下舍入(使用floor
)将使像素与一侧的采样区域相交,而向上舍入(ceil
)将使像素与另一侧的像素相交。对于X坐标,给出left
和right
。
使用fmin
和fmax
的原因是要钳位坐标,以使它们不超出像素坐标范围。
编辑:由于您试图解释这张图片,我将在下面列出相应的部分: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是您的朋友。
关于c++ - 双线性插值-OSRM Rastersource,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45188988/