Bilinear Interpolation
![](https://secure.gravatar.com/avatar/2daa4c6780621c763285c2803187af97.jpg?s=120&d=mm&r=g)
Hi, Is there function for doing a simple bilinear interpolation in scipy? I tried the interpolate.interp2d routines, but it appears that it uses B-splines even when kind='linear' is specified. (I wouldn't otherwise care except that my class assignment explicitly says to use bilinear.) I know I can do this using a series of interp1d calls, but I wanted to see if there was a simpler way I was missing. Thanks Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma (405)325-3791 rmay@rossby.ou.edu
![](https://secure.gravatar.com/avatar/af6c39d6943bd4b0e1fde23161e7bb8c.jpg?s=120&d=mm&r=g)
On Tue, Sep 26, 2006 at 11:19:37AM -0500, Ryan May wrote:
Is there function for doing a simple bilinear interpolation in scipy? I tried the interpolate.interp2d routines, but it appears that it uses B-splines even when kind='linear' is specified. (I wouldn't otherwise care except that my class assignment explicitly says to use bilinear.)
I'm speaking under correction, but if you choose 'linear', b-splines of degree 1 (i.e. straight lines) are used and you are doing linear interpolation. Regards Stéfan
![](https://secure.gravatar.com/avatar/2daa4c6780621c763285c2803187af97.jpg?s=120&d=mm&r=g)
Stefan van der Walt wrote:
On Tue, Sep 26, 2006 at 11:19:37AM -0500, Ryan May wrote:
Is there function for doing a simple bilinear interpolation in scipy? I tried the interpolate.interp2d routines, but it appears that it uses B-splines even when kind='linear' is specified. (I wouldn't otherwise care except that my class assignment explicitly says to use bilinear.)
I'm speaking under correction, but if you choose 'linear', b-splines of degree 1 (i.e. straight lines) are used and you are doing linear interpolation.
That's what I kinda thought, but using 'linear' for interp2d didn't give me the same answer as when I performed the calculation manually. I'll have to see what I can find on this... Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma rmay@rossby.ou.edu
![](https://secure.gravatar.com/avatar/2daa4c6780621c763285c2803187af97.jpg?s=120&d=mm&r=g)
Ryan May wrote:
Stefan van der Walt wrote:
On Tue, Sep 26, 2006 at 11:19:37AM -0500, Ryan May wrote:
Is there function for doing a simple bilinear interpolation in scipy? I tried the interpolate.interp2d routines, but it appears that it uses B-splines even when kind='linear' is specified. (I wouldn't otherwise care except that my class assignment explicitly says to use bilinear.) I'm speaking under correction, but if you choose 'linear', b-splines of degree 1 (i.e. straight lines) are used and you are doing linear interpolation.
That's what I kinda thought, but using 'linear' for interp2d didn't give me the same answer as when I performed the calculation manually. I'll have to see what I can find on this...
Ok, if I select the 4 points surrounding the location of interest, interp2d gives me the value I get with manually calculating a bilinear interpolation. However if I use the whole field (or even 9 points instead of 4), I get a different answer. I _know_ I wouldn't expect this for bilinear interpolation, and it would seem to imply that even _linear_ B-splines uses the information from additional points. Am I missing something here, or are the methods just not truly equivalent? Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma rmay@rossby.ou.edu
![](https://secure.gravatar.com/avatar/fc5fe18ae5ee9c204916fcd0998dd4fc.jpg?s=120&d=mm&r=g)
Ryan May wrote:
Ok, if I select the 4 points surrounding the location of interest, interp2d gives me the value I get with manually calculating a bilinear interpolation. However if I use the whole field (or even 9 points instead of 4), I get a different answer. I _know_ I wouldn't expect this for bilinear interpolation, and it would seem to imply that even _linear_ B-splines uses the information from additional points. Am I missing something here, or are the methods just not truly equivalent?
The two are equivalent when there are only 4 surrounding points (as you've found). Bilinear interpolation is simply linear interpolation, first in one and then in a second (perpendicular) direction, from what I understand the method uses 4 known points by definition. Spline interpolation is a different animal. Here you are trying to find the "smoothest" (actually minimum bending energy) function that can be constructed to fit *exactly* through *all* of the known data using a set of basis functions which may be linear (or cubic etc...). Then you evaluate the value of that function at your unknown location. Cheers, Scott
![](https://secure.gravatar.com/avatar/af6c39d6943bd4b0e1fde23161e7bb8c.jpg?s=120&d=mm&r=g)
Hi Ryan On Wed, Sep 27, 2006 at 08:09:18AM -0500, Ryan May wrote:
That's what I kinda thought, but using 'linear' for interp2d didn't give me the same answer as when I performed the calculation manually. I'll have to see what I can find on this...
Ok, if I select the 4 points surrounding the location of interest, interp2d gives me the value I get with manually calculating a bilinear interpolation. However if I use the whole field (or even 9 points instead of 4), I get a different answer. I _know_ I wouldn't expect this for bilinear interpolation, and it would seem to imply that even _linear_ B-splines uses the information from additional points. Am I missing something here, or are the methods just not truly equivalent?
Please send some code so we can see what you are doing. Without that, we are just discussing things in the air. Cheers Stéfan
![](https://secure.gravatar.com/avatar/2daa4c6780621c763285c2803187af97.jpg?s=120&d=mm&r=g)
Stefan van der Walt wrote:
Hi Ryan
On Wed, Sep 27, 2006 at 08:09:18AM -0500, Ryan May wrote:
That's what I kinda thought, but using 'linear' for interp2d didn't give me the same answer as when I performed the calculation manually. I'll have to see what I can find on this... Ok, if I select the 4 points surrounding the location of interest, interp2d gives me the value I get with manually calculating a bilinear interpolation. However if I use the whole field (or even 9 points instead of 4), I get a different answer. I _know_ I wouldn't expect this for bilinear interpolation, and it would seem to imply that even _linear_ B-splines uses the information from additional points. Am I missing something here, or are the methods just not truly equivalent?
Please send some code so we can see what you are doing. Without that, we are just discussing things in the air.
Here's some example code that demonstrates what I mean, but I think Scott Sinclair hit the nail on the head in another reply. The methods are just different except in the case of 4 points, since the splines are performing a global fit and the bilinear interpolation is only local. from scipy.interpolate import interpolate from numpy.random import randn from numpy import * data = randn(16).reshape(4,4) x = y = arange(4) X,Y = meshgrid(x,y) x_loc = 1.4 y_loc = 2.7 #Do the interpolation with all points, a partial set, and only 4 points, #making sure that the deisred location is within the domain full_bilin = interpolate.interp2d(X,Y,data,kind='linear') partial_bilin = interpolate.interp2d(X[1:,1:],Y[1:,1:],data[1:,1:],kind='linear') single_bilin = interpolate.interp2d(X[2:4,1:3],Y[2:4,1:3],data[2:4,1:3],kind='linear') #If interp2d was truly doing bilinear interpolation, these should be equal print full_bilin(x_loc,y_loc) print partial_bilin(x_loc,y_loc) print single_bilin(x_loc,y_loc) #This manual calculation of the interpolation is only equal to the last one xw = 0.4 yw = 0.7 xws = array([1-xw, xw]) yws = array([1-yw, yw]) print dot(yws,dot(data[2:4,1:3],xws)) Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma rmay@rossby.ou.edu
![](https://secure.gravatar.com/avatar/af6c39d6943bd4b0e1fde23161e7bb8c.jpg?s=120&d=mm&r=g)
Hi Ryan On Wed, Sep 27, 2006 at 10:41:21AM -0500, Ryan May wrote:
Here's some example code that demonstrates what I mean, but I think Scott Sinclair hit the nail on the head in another reply. The methods are just different except in the case of 4 points, since the splines are performing a global fit and the bilinear interpolation is only local.
Thanks for the code (and Scott for the explanation). I also attach a script I whipped up to demonstrate this effect on images. If you change the spline order from 2 to 1, you will notice weird things happening. For example, compare http://mentat.za.net/refer/spline_order2.png and http://mentat.za.net/refer/spline_order1.png Notice how dark the spline interpolated image of order 1 is. Can anyone explain this? Possibly due to pre-filtering? Cheers Stéfan
participants (3)
-
Ryan May
-
Scott Sinclair
-
Stefan van der Walt