justou
2016-07-14 02:16:44 +08:00
不太清楚你的问题细节, 写了个 3d 插值的例子:
# -*- coding: utf-8 -*-
import numpy as np
from scipy.interpolate import interpn
from enthought.mayavi import mlab
mlab.options.offscreen = True
original_points = 20j
interp_points = 40j
figsize = (1280, 720)
x, y, z = np.mgrid[-15:14:original_points, -12:12:original_points, -14:15:original_points]
# x.shape: 20x20x20
# y.shape: 20x20x20
# z.shape: 20x20x20
s = np.sin(x*y*z)/(x*y*z) # s.shape: 20x20x20
mlab.contour3d(x, y, z, s)
mlab.savefig("original.png", size=figsize)
xs, ys, zs = np.mgrid[-15:14:interp_points, -12:12:interp_points, -14:15:interp_points]
# Construct a 3d array of 3-dimensional points.
arr_4d = np.concatenate((xs[..., None], ys[..., None], zs[..., None]), axis=3) # shape: 40x40x40x3
ss = interpn((x[:, 0, 0], y[0, :, 0], z[0, 0, :]), s, arr_4d, method="nearest")
# Weirdly though, this will also work, why?(゚д゚)
sss = interpn((x[:, 0, 0], y[0, :, 0], z[0, 0, :]), s, (xs, ys, zs), method="nearest")
print np.all(ss == sss) # True
mlab.contour3d(xs, ys, zs, ss)
mlab.savefig("interpolate.png", size=figsize)