gpt4 book ai didi

python - 用 numpy 和/或 scipy 插入 3D 体积

转载 作者:IT老高 更新时间:2023-10-28 20:53:32 24 4
gpt4 key购买 nike

我非常沮丧,因为几个小时后,我似乎无法在 python 中进行看似简单的 3D 插值。在 Matlab 中,我所要做的就是

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

使用 scipy 的 ndimage.map_coordinate 或其他 numpy 方法与此完全等价的是什么?

谢谢

最佳答案

在 scipy 0.14 或更高版本中,有一个新功能 scipy.interpolate.RegularGridInterpolator这与 interp3 非常相似。

MATLAB 命令 Vi = interp3(x,y,z,V,xi,yi,zi) 将转换为:

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)

这是一个完整的例子,展示了两者;它将帮助您了解确切的差异...

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

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

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

关于python - 用 numpy 和/或 scipy 插入 3D 体积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21836067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com