使用scipy.interpolate.LSQBivariateSplines将2-D样条拟合到带有间隙的噪声数据

我有一个在网格上有矩形数据的numpy数组,并希望在它上面插入二维样条来重现大规模变化,同时消除所有/大部分噪声.数据还有一些区域标记为NaN值无效.

我尝试使用scipy.interpolate.RectBivariateSpline函数,但是差距搞砸了结果.所以我尝试使用同一个包中的LSQBivariateSpline函数,希望当我将所有NaN像素的权重设置为0时,它会简单地忽略它们.但是,当我遇到以下错误时,我不知道如何避免:

我的代码是:

# some preparation, loading data and stuff
# all my data is stored in 'data'

# Create the knots (10 knots in each direction, making 100 total
xcoord = numpy.linspace(5, data.shape[0]-5, 10)
ycoord = numpy.linspace(5, data.shape[1]-5, 10)

# Create all weights, and set them to 0 when the data is NaN
weights = numpy.ones(data.shape)
weights[numpy.isnan(data)] = 1e-15  # weights must be >0

# LSQBivariateSpline needs x and y coordinates as 1-D arrays
x, y = numpy.indices(data.shape)
spline_fit = scipy.interpolate.LSQBivariateSpline(x.ravel(), y.ravel(), data.ravel(), 
                                               xcoord, ycoord, 
                                               w=weights.ravel(),
                                               bbox=[None, None, None, None], 
                                               kx=2, ky=2)

输出的代码是以下错误消息:

The coefficients of the spline returned have been computed as the
minimal norm least-squares solution of a (numerically) rank deficient
system (deficiency=25). If deficiency is large, the results may be
inaccurate. Deficiency may strongly depend on the value of eps.
done!
Traceback (most recent call last):
  File "./fitpg.py", line 513, in <module>
    fit_pupilghost(prebinned, (center_y, center_x), (r_inner, r_outer), dr)
  File "./fitpg.py", line 298, in fit_pupilghost
    fitout = pupil2d(radius[:,y], angle[:,y])
  File "/usr/local/lib64/python2.7/site-packages/scipy/interpolate/fitpack2.py", line 545, in __call__
    raise ValueError("Error code returned by bispev: %s" % ier)
ValueError: Error code returned by bispev: 10

我输入的输入矩阵(‘数据’)大约是1000 x 1000 px,应该足以将样条约束在100节.在每个方向上将结的数量增加到100可以使代码运行速度变慢,但除了缺陷数之外没什么变化.我还尝试增加和减少eps值,其值介于1-e30到0.9之间(默认值为1e-16

我也尝试谷歌周围的错误代码,但无法获得良好的打击,所以这也没有帮助.

知道这里可能有什么问题吗?或者是否有解决此问题的解决方法/更好的方法?

任何帮助将不胜感激.

谢谢

解决方法:

样条拟合代码不以任何特殊方式处理NaN.由于任何与NaN接触的数字也会变成NaN,这意味着它们的存在会使整个计算中毒,因此您不会得到任何结果.

您可以做的是,除了设置零权重之外,还要用一些(任意)有限值替换NaN值,例如

weights = numpy.ones(data.shape)
mask = numpy.isnan(data)
weights[mask] = 0
data[mask] = 0   # arbitrary

由于重量很小,所选择的值无关紧要.您也可以尝试将相应的权重设置为较小的值,例如1e-15,如果由于某种原因不喜欢零权重.

上一篇:iOS GPUImage 自定义美颜滤镜


下一篇:[UE4]Spline使用注意事项