Python:在2D中高效地从不规则网格插值到规则网格
我在一个不规则间隔的2d网格中有一些数据点,我想在一个规则网格上插值。例如,假设源数据来自鱼眼照相机:Python:在2D中高效地从不规则网格插值到规则网格,python,opencv,image-processing,scipy,interpolation,Python,Opencv,Image Processing,Scipy,Interpolation,我在一个不规则间隔的2d网格中有一些数据点,我想在一个规则网格上插值。例如,假设源数据来自鱼眼照相机: 不规则源栅格的示例。笔记这些只是示例—通常源数据也可能以不同的方式扭曲—但仍然来自网格。 # Source Data x_src # A (n_src_rows, n_src_cols) array of x-coordinates of points y_src # A (n_src_rows, n_src_cols) array of y-coordinates of points
不规则源栅格的示例。笔记这些只是示例—通常源数据也可能以不同的方式扭曲—但仍然来自网格。
# Source Data
x_src # A (n_src_rows, n_src_cols) array of x-coordinates of points
y_src # A (n_src_rows, n_src_cols) array of y-coordinates of points
# (x_src, y_src) form an irregular grid. i.e. if you were to plot the lines connecting neighbouring points, no lines would ever cross.
f_src # A (n_src_rows, n_src_cols) array of values.
# Interpolation Points:
x_dst # An (n_dest_cols) sorted array of x-coordinates of columns in a regular grid
y_dst # An (n_dest_rows) sorted array of y-coordinates of rows in a regular grid.
# Want to calculate:
f_dst # An (n_dest_rows, n_dest_cols) array of interpolated data on the regular grid defined by x_dst, y_dst
到目前为止,我一直在使用,并将源点展平为1D数组,但速度有点慢,因为它没有利用源数据点(仅目标数据点)的网格结构。它还会在不在相邻源栅格点内的区域内进行插值(如果源栅格的边界为凹面,则会发生这种情况(如左图所示)
SciPy/opencv或类似的库中是否有一个函数可以在源数据位于不规则间隔的网格中时有效地进行插值?怎么样?它仍然不是最优的,因为它没有利用已知源数据沿网格分布的事实,但到目前为止,我发现最好的方法是使用SciPy的最接近的Interpolator,基于KDTree:
import scipy.interpolate
def fast_interp_irregular_grid_to_regular(
x_dst, # type: ndarray(dst_size_x) # x-values of columns in the destination image.
y_dst, # type: ndarray(dst_size_y) # y-values of rows in the destination image
x_src, # type: ndarray(src_size_y, src_sixe_x) # x-values of data points
y_src, # type: ndarray(src_size_y, src_size_x) # y-values of data points
f_src, # type: ndarray(src_size_y, src_size_x, n_dim) # values of data points.
fill_value = 0, # Value to fill in regions outside pixel hull
zero_edges = True, # Zero the edges (ensures that regions outside source grid are zero)
): # type: (...) -> array(dst_size_y, dst_size_x, n_dim) # Interpolated image
"""
Do a fast interpolation from an irregular grid to a regular grid. (When source data is on a grid we can interpolate
faster than when it consists of arbitrary points).
NOTE: Currently we do not exploit the fact that the source data is on a grid. If we were to do that, this function
could be much faster.
"""
assert zero_edges in (False, True, 'inplace')
univariate = f_src.ndim==1
if univariate:
f_src = f_src[:, None]
else:
assert f_src.ndim==3
if zero_edges:
if zero_edges is True:
f_src = f_src.copy()
f_src[[0, -1], :] = fill_value
f_src[:, [0, -1]] = fill_value
interp = scipy.interpolate.NearestNDInterpolator(
x = np.hstack([x_src.reshape(-1, 1), y_src.reshape(-1, 1)]),
y = f_src.reshape(-1, f_src.shape[-1]),
)
grid_x, grid_y = np.meshgrid(x_dst, y_dst)
z = interp((grid_x, grid_y)).reshape((len(y_dst), len(x_dst), f_src.shape[-1]))
return z
从开始,它要么要求源点位于规则网格中,要么只需要源点的平面向量。因此,它无法利用源点的不规则网格。我已经尝试过此函数,但运行速度非常慢。能否定义一个函数,将不规则点映射到规则网格?如果可以,可以映射x_src,y_src和x_dst,y_dst并使用规则网格执行插值。您是指函数
x_newsrc,y_newsrc=f(x_src,y_src)
其中(x_newsrc,y_newsrc)
形成一个规则的网格?也许我可以,但是为了正确起见,我必须对我的目标网格应用相同的函数,然后我得到一个规则的源网格和一个不规则的目标网格…还有同样的问题。是的,你需要将函数应用到x_dst
和y_dst
上。有一个不规则的de定位网格不是同一个问题。值得注意的是,如果应用于地理坐标数据(即经度和纬度),该函数必须考虑测地距离。因此,线性(甚至多项式、样条曲线等)函数将不会返回正确的插值结果。从优化的角度来看:如果不将问题正则化(或者它是病态的),就没有(好的)方法。您需要定义有关可能失真的假设。有一个相当强的假设-源数据以网格形式出现。在(xy[i,j],xy[i+1,j],xy[i,j+1])
之间,没有其他点会落在三角形中。。Scipy的网格数据
不使用此知识(它对源数据的结构没有预先假设)。