问题
I am converting an MATLAB program to a Python program using NumPy and SciPy and I am still new to it. In part of the program, I have the following MATLAB code:
tImg(:,:,1) = interp2(x,y,Img(:,:,1),Tx,Ty,'cubic');
All parameters in the interp2 method are 298x142 double.
So I tried to convert it to the following Python code:
tImg[:, :, 0] = (scipy.interpolate.interp2d(x, y, img[:, :, 0], kind='cubic'))(Tx, Ty)
I am given MemoryError in the interp2d method. The MATLAB code runs fine. I have read through the interpolation documentation but I don't seem to find a solution.
I run the code above with 3GB memory, with scipy 0.16.0.
Any help is appreciated. Thanks.
回答1:
I suspect the problem is that Tx and Ty mean different things for MATLAB's interp2 function and scipy.interpolate.interp2d.
In the MATLAB interp2 function, Tx and Ty would be 2D arrays that specify the x,y coordinates for each individual output point, so the result would be a vector with the same shape as Tx and Ty, i.e.:
timg(i, j, 1) = interp2(x, y, Img(i, j, 1), Tx(i, j),Ty(i, j), 'cubic');
In scipy.interpolate.interp2d, Tx and Ty should be 1D vectors that specify the x and y coordinates for the rows and columns for a regular mesh over which the interpolant is to be evaluated, i.e.:
timg[i, j, 0] = intp(Tx[i], Ty[j])
I suspect that you are passing the coordinates of every point you want to interpolate at, in which case you will get an (nx*ny, nx*ny) output. If nx = 298 and ny = 142 then you would generate an (42316, 42316) array. Assuming it contains 64 bit floats, that would take up about 14GB of memory in total.
来源:https://stackoverflow.com/questions/35871837/scipy-interp2d-memory-error-looking-for-alternative