卷切片操作通常依赖于插值的概念,最典型的有:Nearest neighbor、Linear 和Cubic。请注意,这些方法适用于更多维数(例如参见 Bilinear 或 Trilinear 插值)。
在这种情况下,您是说您有一个卷,您可以从中检索 X、Y、Z 中的切片(或混合但不考虑这种情况,因为它是一个新的整体问题,只会带来混乱)。
因此,以切片 X=5 和 X=6 为例,您想知道如何获得 X=5.5。看看这个例子:
def linear_interpolation(p1, p2, x0):
"""
Function that receives as arguments the coordinates of two points (x,y)
and returns the linear interpolation of a y0 in a given x0 position. This is the
equivalent to obtaining y0 = y1 + (y2 - y1)*((x0-x1)/(x2-x1)).
Look into https://en.wikipedia.org/wiki/Linear_interpolation for more
information.
Parameters
----------
p1 : tuple (floats)
Tuple (x,y) of a first point in a line.
p2 : tuple (floats)
Tuple (x,y) of a second point in a line.
x0 : float
X coordinate on which you want to interpolate a y0.
Return float (interpolated y0 value)
"""
return p1[1] + (p2[1] - p1[1]) * ((x0 - p1[0]) / (p2[0] - p1[0]))
X, Y, Z = np.meshgrid(range(10), range(10), range(10))
vol = np.sqrt((X-5)**2 + (Y-5)**2 + (Z-5)**2)
Slice5dot5 = linear_interpolation((Y[5, :, :], vol[5, :, :]), (Y[6, :, :], vol[6, :, :]), 5.5)
f, (ax1, ax2, ax3) = plt.subplots(1, 3, sharey=True)
ax1.imshow(vol[5, :, :], interpolation='nearest', origin='lower', vmin=vol.min(), vmax=vol.max())
ax1.set_title("vol[5, :, :]")
ax2.imshow(Slice5dot5, interpolation='nearest', origin='lower', vmin=vol.min(), vmax=vol.max())
ax2.set_title("vol[5.5, :, :]")
ax3.imshow(vol[6, :, :], interpolation='nearest', origin='lower', vmin=vol.min(), vmax=vol.max())
ax3.set_title("vol[6, :, :]")
plt.show()
该函数似乎已记录在案(它是我做的一个旧项目的一部分),可以与数字一起使用,但它也可以与 numpy 2D 切片一起使用(并且比循环所有这些单元格要快得多)。
结果是这样的:
您会注意到颜色从左到右越来越淡。中间的切片是完全插值的,以前不存在。