Python:从不规则网格高效地插值到二维规则网格

标签 python opencv image-processing scipy interpolation

我在不规则间隔的二维网格中有一些数据点,我想将其插值到规则网格上。例如,假设源数据来自鱼眼相机:
(来源:starizona.com)

不规则源网格示例。注意...这些只是示例 - 一般源数据也可能以不同的方式扭曲 - 但仍然来自网格。

# 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 或一些类似的库中是否有一个函数可以在源数据出现在不规则间隔的网格中时有效地进行插值?

最佳答案

它仍然不是最优的,因为它没有利用已知源数据位于网格中这一事实,但到目前为止我发现的最佳方法是使用 SciPy 的 NearestNDInterpolator,它基于KD树:

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

关于Python:从不规则网格高效地插值到二维规则网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49140965/

相关文章:

c++ - 寻找 OpenCV 教程

c++ - 计算图像的模糊度和锐度

python - cv2.drawContours 不会绘制填充轮廓

linux - 如何在命令行中合并图像?

ios - 从点数组中获取矩形

c - 获取bmp文件中每个像素的RGB值

python - 在 Python 中将序列拆分为单个步骤的最优雅方法

python - 如何对带标签的索引+ 1进行切片,python?

具有多个列表的 Python 循环?

python - flask super 教程配置问题