The bicubic spline interpolation is an extension of cubic spline for interpolating on a 2D regular grid. The interpolated surface is smoother than corresponding surfaces obtained by bilinear interpolation.

Does anyone have already the corresponding function that enables such an interpolation?

Here is the beginning of the code:

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

Any help please?

Thank you!

a

有帮助吗?

解决方案

As @Will pointed out, scipy has some interpolation function. Check out griddata, as it has cubic interpolation. I just came up with a little example.

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()

其他提示

At the risk of not answering your question directly, you may want to look into scipy interpolate functions. SciPy and family is usually a good idea for this sort of processing.

Take a look at the method found in 'Numerical Recipes in C++' under 'Higher Order Smoothness: Bicubic Interpolation'. Watch the indices on the derivative formulas, some should start at 1 not 0 and could cause a division by zero when evaluated at a control point. The grid here needs to be rectangular but the width and height do not need to be the same. The matrix listed is correct. I was able to produce the following interpolation (sans rendering) in 3 micro seconds.

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top