在Python中对常规网格进行插值 [英] Interpolation over regular grid in Python

查看:1500
本文介绍了在Python中对常规网格进行插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力整合2D矩阵中空"像素的数据.基本上,我理解(但不是很深入)插值技术,例如反距离权重,Kriging,Bicubic等.我不完全知道起点(无论是在问题陈述中还是在Python案例中).

I have been struggling to inteprolate the data for "empty" pixels in my 2D matrix. Basically, I understand (but not deeply) interpolation techniques such as Inverse Distance Weighting, Kriging, Bicubic etc. I dont know the starting point exactly (either in the statement of the problem or Python case).

问题定义: 我有MxN矩阵(规则网格),其中每个像素代表一定的测量值(以下 ,并且此图中使用的数据是 ="https://www.dropbox.com/s/ropyjwl88l6ejmw/measurement.txt" rel ="noreferrer">此处).我想使用我拥有的现有数据作为蓝色像素,为问号空间"(白色空间,它也由相同大小但空的像素组成)区域内插数据.

The problem definition: I have MxN matrix (regular grid) in which each pixel represents certain measurement value (figure below and data used in this figure is here). I wanted to interpolate the data for "question mark space" (white space which also consists of the same sized but empty pixels) areas using the existing data I have as blue pixels.

我的问题:

1)如何插入这些数据.谁能给我一个简单的例子(例如3x3矩阵)来清楚地理解这一点?

1) How can I interpolate this data. Could anyone give me simple example (e.g. 3x3 matrix) to understand that clearly?

2)有人可以指导我如何在Python环境中执行解决方案的步骤吗?

2) Could anyone guide me how to perform the steps towards solution in Python environment?

3)如何使用Python在准确性方面比较插值技术?

3) How can I compare the interpolation techniques in accuracy sense using Python?

4)您认为根据数据的密度使用不同的插值是一个好主意吗?

4) Do you think it is a good idea to use different interpolation depending on the density of the data?

感谢您的回答和建议.

推荐答案

什么是明智的解决方案,很大程度上取决于您要使用插值像素回答的问题-警告虚空:对丢失的数据进行推断会导致非常严重的后果.误导性答案!

What is a sensible solution largely depends on what questions you're trying to answer with the interpolated pixels -- caveat emptor: extrapolating over missing data can lead to very misleading answers!

径向基函数插值/内核平滑

就可用的Python实用解决方案而言,填充这些像素的一种方法是使用Scipy的径向基函数插值实现(请参见

In terms of practical solutions available in Python, one way to fill those pixels in would be to use Scipy's implementation of Radial Basis Function interpolation (see here) which is intended for the smoothing/interpolation of scattered data.

给定矩阵M以及基础的一维坐标数组rc(使得M.shape == (r.size, c.size)),其中M的缺失项设置为nan,这似乎可以很好地处理线性RBF内核如下:

Given your matrix M and underlying 1D coordinate arrays r and c (such that M.shape == (r.size, c.size)), where missing entries of M are set to nan, this seems to work fairly well with a linear RBF kernel as follows:

import numpy as np
import scipy.interpolate as interpolate

with open('measurement.txt') as fh:
    M = np.vstack(map(float, r.split(' ')) for r in fh.read().splitlines())
r = np.linspace(0, 1, M.shape[0]) 
c = np.linspace(0, 1, M.shape[1])

rr, cc = np.meshgrid(r, c)
vals = ~np.isnan(M)
f = interpolate.Rbf(rr[vals], cc[vals], M[vals], function='linear')
interpolated = f(rr, cc)

这将对您上面链接的数据进行以下插值,尽管看起来合理,但确实突出显示了丢失样本与真实数据的比率有多不利:

This yields the following interpolation of the data you've linked to above, which although reasonable looking, does highlight how unfavourable the ratio of missing samples to real data is:

高斯过程回归/克里金法

Kriging插值scikit-learn库中的Matlab工具箱).可以按如下方式调用它:

Kriging interpolation is available via the Gaussian Process Regression implementation (which is itself based on the DACE Kriging toolbox for Matlab) in the scikit-learn library. This could be invoked as follows:

from sklearn.gaussian_process import GaussianProcess

gp = GaussianProcess(theta0=0.1, thetaL=.001, thetaU=1., nugget=0.01)
gp.fit(X=np.column_stack([rr[vals],cc[vals]]), y=M[vals])
rr_cc_as_cols = np.column_stack([rr.flatten(), cc.flatten()])
interpolated = gp.predict(rr_cc_as_cols).reshape(M.shape)

这产生与上面的径向基函数"示例非常相似的插值.在这两种情况下,都有许多参数可供探索-这些参数的选择很大程度上取决于您可以对数据进行的假设. (上面的RBF示例中使用的线性核的一个优点是它没有自由参数)

This yields a very similar interpolation to the Radial Basis Function example above. In both cases there are a lot of parameters to explore - the choice of these largely hinges on the assumptions that you can make about the data. (One advantage of the linear kernel used in the RBF example above is that it has no free parameters)

修复

最后,完全出于视觉动机的解决方案将使用OpenCV的inpainting 功能,尽管该功能假定使用8位数组(0-255),并且没有简单的数学解释.

As a final aside, an entirely visually motivated solution would be use OpenCV's inpainting functionality, although this assumes 8bit arrays (0 - 255), and does not have a straightforward mathematical interpretation.

这篇关于在Python中对常规网格进行插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆