双三次样条插值是三次样条的扩展,用于在二维规则网格上进行插值。插值后的曲面比双线性插值得到的对应曲面更光滑。
有没有人已经有相应的函数来启用这样的插值?
这是代码的开头:
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
有什么帮助吗?
谢谢!
一个
最佳答案
正如@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()
关于python - 有谁知道如何在 Python 中执行双三次样条插值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970497/