interp2(X, Y, Z, XI, YI) from Matlab to Python

10,023

Solution 1

The correct syntax is ip = interp2d(x, y, z); zi = ip(xi, yi).

Also, interp2d is not exactly the same as interp2. RectBivariateSpline is closer.

Solution 2

for interp2(v,xq,yq)

ip = scipy.interpolate.griddata((y.ravel(),x.ravel()),distorted.ravel(),(yq.ravel(),xq.ravel()))

Note that the result returned needs to be resized. i.e ( ip.resize(img.shape))

here y,x are

x,y = np.meshgrid(np.arange(w),np.arange(h))

where w,h is the width and height of the image respectively.

For more you can read griddata documentation. https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html

For interp2(X,Y,V,Xq,Yq), simply replace x,y with X,Y

Solution 3

I have encountered the same issue, and figured out that scipy.ndimage.map_coordinates does the same as Vq = interp2(V,Xq,Yq). Please read the documentation of these commands to find out the solution for your case.

Try this for Matlab's Vq = interp2(V,Xq,Yq) :

Vq = scipy.ndimage.map_coordinates(V, [Xq.ravel(), Yq.ravel()], order=3, mode='nearest').reshape(V.shape)
Share:
10,023
blueSurfer
Author by

blueSurfer

Just another guy studying computer science.

Updated on June 04, 2022

Comments

  • blueSurfer
    blueSurfer almost 2 years

    I need the exact Python equivalent function of this Matlab function in order to interpolate matrices.

    In Matlab I have:

    interp2(X, Y, Z, XI, YI) 
    

    while in Scipy I have:

    interp2d(X, Y, Z). 
    

    In Scipy XI and YI are missing. How can I resolve this? I'm using all parameters in Matlab.