用 numpy 和或 scipy 插入 3D 体积 [英] interpolate 3D volume with numpy and or scipy

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

问题描述

我非常沮丧,因为几个小时后我似乎无法在 python 中进行看似简单的 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 或更高版本中,有一个新功能 scipy.interpolate.RegularGridInterpolatorinterp3 非常相似代码>.

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,xi,yi,zi) 将转换为:

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 RegularGridInterpolator 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 代码:

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).

SCIPY 代码:

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))

还是 [268,357].所以你会看到一些细微的差异:Scipy 使用 x,y,z 索引顺序,而 MATLAB 使用 y,x,z(奇怪);在 Scipy 中,您在单独的步骤中定义一个函数,当您调用它时,坐标分组为 (x1,y1,z1),(x2,y2,z2),... 而 matlab 使用 (x1,x2,...),(y1,y2,...),(z1,z2,...).

Again it's [268,357]. So you see some slight differences: Scipy uses x,y,z index order while MATLAB uses y,x,z (strangely); In Scipy 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 插入 3D 体积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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