【问题标题】:Arbitrary Slice of 3D Array in PythonPython中3D数组的任意切片
【发布时间】:2018-04-30 08:26:47
【问题描述】:

我有一个 3D 体积数据存储在一个背景值为 0 和体积值为 1 的 3 维数组中。 现在我想得到这个体积的任意截面。我在这里阅读了答案:How can an almost arbitrary plane in a 3D dataset be plotted by matplotlib?

但似乎接受的答案是错误的,它生成 xoy 平面的映射坐标而不是切片坐标。 那么如何得到切片平面的正确形状呢?有没有将映射形状转换为原始形状的方法? 谢谢!

【问题讨论】:

  • 您的意思是 Python 意义上的“切片”(使用下标,例如选择与轴平行的 3d 数组的 2D 子数组),还是像沿体积数据插值一样进行切片一个平面(平面不一定平行于任何轴)?其中第一个比第二个简单得多
  • @Alex 对不起,我没有很好地表达我的问题。其实问题应该是第二种情况,需要插值的方法。

标签: python arrays slice


【解决方案1】:

问题可能已经过时,但看起来scipy.ndimage 中的following function 可能会解决您的问题。

scipy.ndimage.interpolation.rotate 所做的是将完整的 3d 数组围绕 3 个轴中的任何一个旋转一定角度,将存储的值内插到新的“单元格”。它还相应地调整(扩展)新数组的大小,用您指定的值填充新的空单元格。之后,您可以像往常一样进行切片,例如:array[:,sy//2,:]

简而言之,这里是围绕平行于 z 轴的对角线的中心切割(为简单起见):

sz, sy, sx = array.shape
array[:,sy//2,:] # this is a cut in the x-z plane ... 
                 # ... passing through the middle of the y-axis

# rotating by 45 degrees around the z axis ... 
# ... `(2,1)` is `(x,y)` which defines the rotation plane
array_rotated = scipy.ndimage.interpolation.rotate(array, angle=45, axes=(2,1))

sz, sy, sx = array_rotated.shape
# now you'll notice that `sz` is the same ... 
# ... but `sx` and `sy` have increased because the diagonal is longer

array_rotated[:,sy//2,:] # this slice is now in the new x'-z' plane ...
                         # ... but in terms of the original array ...
                         # ... it passes through the diagonal of x-y along z

您实际上可以进一步思考,并通过围绕不同轴旋转几次将其扩展到任意切片。

PS。如果您觉得花费太多时间,您可以通过传递order=0(默认为order=3)来牺牲插值质量。这将运行得更快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-11
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多