[SciPy-User] evaluating B-Splines made with scipy.signal.cspline2d?
Kay F. Jahnke
_kfj@yahoo....
Wed Nov 9 03:16:12 CST 2011
Hi group!
in brief: I'm looking for an efficient way to evaluate B-splines generated by
scipy.signal.cspline2d() at arbitrary float coordinates to interpolate image
data.
I am doing image processing and need a method to interpolate an image at
arbitrary float coordinates. Scipy.signal kindly provides a fast and efficient
routine to calculate spline coefficients, but the module seems not to adress
using this spline for interpolation.
I have written some code to calculate the interpolated value at arbitrary
positions, but this code is cumbersome and slow - I have to calculate the
value of the basis function 8 times (I suspect that takes a good while using
signal.bspline(), haven't done any measurements), plus a fair bit of matrix
manipulation to pick the relevant window of spline coefficients to multiply
with the basis function values - my code (without any frills or checks,
just to convey the basic idea) looks like this:
# assume the spline coefficients are in a 2D array 'cf'
# and (x,y) is the position to interpolate at
def cf_matrix ( cf , x , y ) :
return cf [ x - 1 : x + 3 , y - 1 : y + 3 ]
def base_matrix ( x , y ) :
x0 = x - floor ( x )
y0 = y - floor ( y )
rng = arange ( 1 , -3 , -1 )
xv = rng + x0
yv = rng + y0
xb = signal.bspline ( xv , 3 )
yb = signal.bspline ( yv , 3 )
xx , yy = meshgrid ( xb , yb )
return xx * yy
def interpolate ( cf , x , y ) :
m = cf_matrix ( cf , x , y )
b = base_matrix ( x , y )
return sum ( m * b )
obviously this is fine to use for a few hundred points or so, but for real
images it's not practical.
In Scipy.interpolate, there is scipy.interpolate.bisplev() to evaluate the
splines generated by scipy.interpolate.bisplrep(), but these splines are of
the more general form with arbitraryly spaced knot points, therefore the
relevant data structures contain the knot vectors, and I assume that all sorts
of performance gains that could be derived from the fact that the splines from
the signal package are using equally-spaced samples are inapplicable in the
general case.
I'd appreciate suggestions on how to proceed or hints at what I'm missing.
More information about the SciPy-User
mailing list