Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'scipy': 0.05; 'linear': 0.07; 'problem?': 0.07; 'python': 0.09; "'''": 0.09; '40,': 0.09; '[1,': 0.09; 'library...': 0.09; 'rows': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; "wouldn't": 0.11; ':-)': 0.13; 'alpha': 0.15; 'stack': 0.15; '0.2': 0.16; '1.0]': 0.16; '100,': 0.16; '[2,': 0.16; 'arrays.': 0.16; 'cc:name:python list': 0.16; 'combinations': 0.16; 'dimension': 0.16; 'frames': 0.16; 'loops': 0.16; 'numpy': 0.16; 'quadratic': 0.16; 'y1,': 0.16; 'wrote:': 0.17; 'exists': 0.17; 'appropriate': 0.20; 'sort': 0.21; 'import': 0.21; 'supposed': 0.21; 'explicit': 0.22; 'cc:2**0': 0.23; 'raise': 0.24; 'somewhere': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'compiled': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:/': 0.28; '50,': 0.29; 'arrays': 0.29; 'routine': 0.29; 'surprised': 0.29; 'vector': 0.29; 'array': 0.29; 'probably': 0.29; "i'm": 0.29; 'maybe': 0.29; 'e.g.': 0.30; 'code': 0.31; 'anybody': 0.32; 'could': 0.32; 'print': 0.32; 'problem': 0.33; '(with': 0.33; 'code:': 0.33; 'hi,': 0.33; 'skip:- 50': 0.34; 'received:google.com': 0.34; 'third': 0.34; 'skip:k 20': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; "wasn't": 0.36; 'should': 0.36; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'possible.': 0.38; 'sure': 0.38; 'page': 0.38; 'header:Received:5': 0.40; 'easy': 0.60; 'skip:u 10': 0.60; '30,': 0.62; 'between': 0.63; 'ever': 0.63; 'more': 0.63; 'here': 0.65; '20,': 0.65; '2013': 0.84; 'oscar': 0.84; 'do:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=VIawqgajuFCZTfLRA7jGNWfd6GgnDdWNG4V+4XbXBng=; b=XJR5xiyIhpHLq8Zkxe5r+LXQNSLhOtlb475wfD4FF9gzD98yfmWYedl696PNkb+spt GdvdKryQpYwB3714NxCdux65ep+zQ7Gk2RuzV3VyCnwU3KFmSKnkQYdWAAuoqw5WRogL HNnB+hy+uT+6lrszmHQvqxW8FR61PcFKRL0oSg1+vK2+bac6e9elF+yDTmzh3Eo4gxWS q5s5RsaIisZCdp3MdKLAzjK/HJP8DA1JR7NpnpTs2VLLSPGnqmYIhANDHv0QiZpdPn8p mOrIGRXhFDoKZU0vBkj+e+n9ypYPBpX64yok0kpUqOf7IAKMzNGbruFI/CKohZF6Td/4 LYlw== MIME-Version: 1.0 In-Reply-To: <5b5b7e09-7482-4609-b774-d143de366615@googlegroups.com> References: <5b5b7e09-7482-4609-b774-d143de366615@googlegroups.com> Date: Wed, 9 Jan 2013 22:59:56 +0000 Subject: Re: Interpolating/crossfading a stack of matrices From: Oscar Benjamin To: raphael@mameghani.de Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357772410 news.xs4all.nl 6936 [2001:888:2000:d::a6]:56532 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36524 On 9 January 2013 16:02, wrote: > Hi, > > I want to interpolate (with quadratic splines) a stack of 2D-arrays/matri= ces y1, y2, y3, ... in a third dimension (which I call x) e.g. for crossfad= ing images. I already have a working code which unfortunately still contain= s two explicit loops over the rows and colums of the matrices. Inside these= loops I simply use 'interp1d' from scipy suitable for 1D-interpolations. I= s anybody here aware of a better, more efficient solution of my problem? Ma= ybe somewhere out there a compiled routine for my problem already exists in= a python library... :-) It's possible. I wouldn't be surprised if there wasn't any existing code ready for you to use. > > My code: > > -----=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D----- > from scipy.interpolate import interp1d > from numpy import array, empty_like, dstack > > x =3D [0.0, 0.25, 0.5, 0.75, 1.0] > > y1 =3D array([[1, 10, 100, 1000], [1, 10, 100, 1000]], float) > y2 =3D array([[2, 20, 200, 2000], [2, 20, 200, 2000]], float) > y3 =3D array([[3, 30, 300, 3000], [4, 40, 400, 4000]], float) > y4 =3D array([[4, 40, 400, 4000], [8, 80, 800, 8000]], float) > y5 =3D array([[5, 50, 500, 5000], [16, 160, 1600, 16000]], float) > > y =3D dstack((y1, y2, y3, y4, y5)) > > y_interpol =3D empty_like(y[:, :, 0]) > i_range, j_range =3D y.shape[:2] > > for i in xrange(i_range): > for j in xrange(j_range): > # interpolated value for x =3D 0.2 > y_interpol[i,j] =3D interp1d(x, y[i, j,:], kind=3D'quadratic')(0.= 2) > > print y_interpol > -----=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D----- 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] <=3D t < times[n+1]: break else: raise OutOfBoundsError # Interpolate between the two images alpha =3D (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