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.
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.
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)
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
withX,Y
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With