有谁知道如何在Python中执行双三次样条插值?

时间:2014-02-23 15:49:47

标签: python interpolation

双三次样条插值是三次样条的扩展,用于在2D规则网格上进行插值。插值曲面比通过双线性插值获得的相应曲面更平滑。

有没有人已经有相应的功能来启用这种插值?

以下是代码的开头:

def bicubicspline_interpolation(x, y, points):
'''Interpolate (x,y) from values associated with four points.

The four points are a list of four triplets:  (x, y, value).
The four points can be in any order.  They should form a rectangle.

    >>> bilinear_interpolation(12, 5.5,
    ...                        [(10, 4, 100),
    ...                         (20, 4, 200),
    ...                         (10, 6, 150),
    ...                         (20, 6, 300)])
    165.0

'''
# See formula at:  http://en.wikipedia.org/wiki/Bicubic_interpolationon

points = sorted(points)               # order points by x, then by y
(x1, y1, q11), (_x1, y2, q12), (x2, _y1, q21), (_x2, _y2, q22) = points

if x1 != _x1 or x2 != _x2 or y1 != _y1 or y2 != _y2:
    raise ValueError('points do not form a rectangle')
if not x1 <= x <= x2 or not y1 <= y <= y2:
    raise ValueError('(x, y) not within the rectangle')



value = 

return value

请帮忙吗?

谢谢!

3 个答案:

答案 0 :(得分:2)

正如@Will指出的,scipy有一些插值函数。查看griddata,因为它具有三次插值。我想出了一个小例子。

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt

def func( x, y ):
    return np.sin(x*12.0)*np.sin(y*20.0)

points = np.random.rand(1000, 2)
values = func(points[:,0], points[:,1])

grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
grid_z = griddata(points, values, (grid_x, grid_y), method='cubic')

plt.imshow(grid_z.T, extent=(0,1,0,1), origin='lower')
plt.scatter(points[:,0], points[:,1], c='k')

plt.show()

答案 1 :(得分:1)

如果没有直接回答您的问题,您可能需要查看scipy interpolate函数。 SciPy和家庭通常是这种处理的好主意。

答案 2 :(得分:1)

查看“C ++中的数字食谱”中找到的方法&#39;在高阶平滑度下:双立方插值&#39;。观察衍生公式的指数,有些应该从1开始而不是0,并且当在控制点进行评估时可能导致除以零。这里的网格需要是矩形的,但宽度和高度不需要相同。列出的矩阵是正确的。我能够在3 微秒中生成以下插值(无渲染)。

enter image description here