Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 461
blueSurfer Avatar asked Jul 13 '12 10:07

blueSurfer


3 Answers

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.

like image 83
pv. Avatar answered Nov 17 '22 12:11

pv.


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)
like image 22
Elektrone Motyo Avatar answered Nov 17 '22 10:11

Elektrone Motyo


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

like image 1
Sankalp Bhamare Avatar answered Nov 17 '22 10:11

Sankalp Bhamare