我有一些不规则间隔的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
# (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
到目前为止,我一直在使用scipy.interpolate.griddata,并将我的源点展平为一维数组,但它有点慢,因为它没有利用源数据点的网格结构(只有目的地数据点)。它还会在不在相邻源网格点内的区域进行插值(如果源网格的边界是凹的,则会发生这种情况(如左图所示)。
SciPy / opencv或类似的库中是否有一个函数在源数据以不规则间隔的网格进行插值时有效插值?
答案 0 :(得分:0)
答案 1 :(得分:0)
它仍然不是最佳的,因为它没有使用已知源数据位于网格中的事实,但我迄今为止发现的最佳方法是使用SciPy&# 39; s NearestNDInterpolator,基于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