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

标签 python interpolation

双三次样条插值是三次样条的扩展,用于在二维规则网格上进行插值。插值后的曲面比双线性插值得到的对应曲面更光滑。

有没有人已经有相应的函数来启用这样的插值?

这是代码的开头:

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/

相关文章:

java - Graphics2D 插值不能很好地处理非常小的 BufferedImages

opengl - 沿着圆弧插入相机运动

java - n 维中未知数量点的多重线性插值

python - 上采样数据和插值

image-processing - 成像插值 - 创建中间图像?

python - 如何使用 python、pandas 修改(字符串数据列表为单个字符串)数据系列的每个元素?

python - Pandas 数据框解析字符串列以将日期提取到新列中

python - 创建列值之间所有组合的数据框(即使没有观察)

python - zabbix api 获取所有主机名

python - 调用句柄后调用recv挂起