插值三维体积与numpy的和或SciPy的 [英] interpolate 3D volume with numpy and or scipy

查看:1205
本文介绍了插值三维体积与numpy的和或SciPy的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到非常沮丧,因为几个小时后,我似乎无法能够做一个看似简单的3D插值蟒蛇。在Matlab的一切我所要做的就是

I am extremely frustrated because after several hours I can't seem to be able to do a seemingly easy 3D interpolation in python. In Matlab all I had to do was

Vi = interp3(x,y,z,V,xi,yi,zi)

这样做有什么用SciPy的的ndimage.map_coordinate或其他numpy的方法完全等效?

What is the exact equivalent of this using scipy's ndimage.map_coordinate or other numpy methods?

感谢

推荐答案

在SciPy的0.14或更高版本,有一个新的功能,<一个href="http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.RegularGridInterpolator.html#scipy.interpolate.RegularGridInterpolator"相对=nofollow> scipy.interpolate.RegularGridInterpolator 这非常类似于 interp3

In scipy 0.14 or later, there is a new function scipy.interpolate.RegularGridInterpolator which closely resembles interp3.

MATLAB命令 VI = interp3(X,Y,Z,V,十一,苡仁,紫)将转化是这样的:

The MATLAB command Vi = interp3(x,y,z,V,xi,yi,zi) would translate to something like:

from numpy import array
from scipy.interpolate import RectangularGridInterpolator as rgi
my_interpolating_function = rgi((x,y,z), V)
Vi = my_interpolating_function(array([xi,yi,zi]).T)

下面是一个完整的例子来展示这两种;它会帮助你了解确切的差异...

Here is a full example demonstrating both; it will help you understand the exact differences...

MATLAB code:

MATLAB CODE:

x = linspace(1,4,11);
y = linspace(4,7,22);
z = linspace(7,9,33);
V = zeros(22,11,33);
for i=1:11
    for j=1:22
        for k=1:33
            V(j,i,k) = 100*x(i) + 10*y(j) + z(k);
        end
    end
end
xq = [2,3];
yq = [6,5];
zq = [8,7];
Vi = interp3(x,y,z,V,xq,yq,zq);

结果是 VI = [268 357] 这确实是在这两点(2,6,8)的值(3,5,7)

The result is Vi=[268 357] which is indeed the value at those two points (2,6,8) and (3,5,7).

MATPLOTLIB code:

MATPLOTLIB CODE:

from scipy.interpolate import RegularGridInterpolator
from numpy import linspace, zeros, array
x = linspace(1,4,11)
y = linspace(4,7,22)
z = linspace(7,9,33)
V = zeros((11,22,33))
for i in range(11):
    for j in range(22):
        for k in range(33):
            V[i,j,k] = 100*x[i] + 10*y[j] + z[k]
fn = RegularGridInterpolator((x,y,z), V)
pts = array([[2,6,8],[3,5,7]])
print(fn(pts))

同样是 [268357] 。所以你看到一些细微的差别:Matplotlib使用X,Y,Z指数为了在MATLAB用Y,X,Z(奇怪);在Matplotlib定义在一个单独的步骤中的函数,当你调用它,坐标分组像(X1,Y1,Z1),(X2,Y2,Z2),...而MATLAB的使用(X1,X2,... ),(Y1,Y2,...),(Z1,Z2,...)。

Again it's [268,357]. So you see some slight differences: Matplotlib uses x,y,z index order while MATLAB uses y,x,z (strangely); In Matplotlib you define a function in a separate step and when you call it, the coordinates are grouped like (x1,y1,z1),(x2,y2,z2),... while matlab uses (x1,x2,...),(y1,y2,...),(z1,z2,...).

除此之外,两者是相似的,并同样易于使用。

Other than that, the two are similar and equally easy to use.

这篇关于插值三维体积与numpy的和或SciPy的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆