Interpolating/crossfading a stack of matrices

raphael at mameghani.de raphael at mameghani.de
Wed Jan 9 11:02:58 EST 2013


Hi,

I want to interpolate (with quadratic splines) a stack of 2D-arrays/matrices y1, y2, y3, ... in a third dimension (which I call x) e.g. for crossfading images. I already have a working code which unfortunately still contains two explicit loops over the rows and colums of the matrices. Inside these loops I simply use 'interp1d' from scipy suitable for 1D-interpolations. Is anybody here aware of a better, more efficient solution of my problem? Maybe somewhere out there a compiled routine for my problem already exists in a python library... :-)

My code:

-----============================================-----
from scipy.interpolate import interp1d
from numpy import array, empty_like, dstack

x = [0.0, 0.25, 0.5, 0.75, 1.0]

y1 = array([[1, 10, 100, 1000], [1,  10,   100,  1000]], float)
y2 = array([[2, 20, 200, 2000], [2,  20,   200,  2000]], float)
y3 = array([[3, 30, 300, 3000], [4,  40,   400,  4000]], float)
y4 = array([[4, 40, 400, 4000], [8,  80,   800,  8000]], float)
y5 = array([[5, 50, 500, 5000], [16, 160, 1600, 16000]], float)

y = dstack((y1, y2, y3, y4, y5))

y_interpol = empty_like(y[:, :, 0]) 
i_range, j_range = y.shape[:2]

for i in xrange(i_range):
    for j in xrange(j_range):
        # interpolated value for x = 0.2
        y_interpol[i,j] = interp1d(x, y[i, j,:], kind='quadratic')(0.2)
        
print y_interpol
-----============================================-----

Cheers, Raphael



More information about the Python-list mailing list