Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36618
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-01-11 04:20 -0800 |
| References | <5b5b7e09-7482-4609-b774-d143de366615@googlegroups.com> <mailman.343.1357772410.2939.python-list@python.org> |
| Subject | Re: Interpolating/crossfading a stack of matrices |
| From | raphael@mameghani.de |
| Message-ID | <mailman.395.1357906820.2939.python-list@python.org> (permalink) |
>> 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... :-) > Since numpy arrays make it so easy to form linear combinations of > arrays without loops I would probably eliminate the loops and just > form the appropriate combinations of the image arrays. For example, to > use linear interpolation you could do: > > > > def interp_frames_linear(times, frames, t): > > '''times is a vector of floats > > frames is a 3D array whose nth page is the image for time t[n] > > t is the time to interpolate for > > ''' > > # Find the two frames to interpolate between > > # Probably a better way of doing this > > for n in range(len(t)-1): > > if times[n] <= t < times[n+1]: > > break > > else: > > raise OutOfBoundsError > > > > # Interpolate between the two images > > alpha = (t - times[n]) / (times[n+1] - times[n]) > > return (1 - alpha) * frames[:, :, n] + alpha * frames[:, :, n+1] > > > > I'm not really sure how quadratic interpolation is supposed to work > (I've only ever used linear and cubic) but you should be able to do > the same sort of thing. > > Oscar Indeed, the 'manual' reimplementation of the interpolation formula using numpy arrays significantly sped up the code. The numexpr package made it even faster. Thanks a lot for your advice! Raphael
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Interpolating/crossfading a stack of matrices raphael@mameghani.de - 2013-01-09 08:02 -0800
Re: Interpolating/crossfading a stack of matrices Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-01-09 22:59 +0000
Re: Interpolating/crossfading a stack of matrices raphael@mameghani.de - 2013-01-11 04:20 -0800
Re: Interpolating/crossfading a stack of matrices raphael@mameghani.de - 2013-01-11 04:20 -0800
csiph-web