擅长:python、mysql、java
<p>这里有一个使用<code>scipy.ndimage.binary_dilation</code>的非常简单的方法,它本质上是您想要的操作的库版本。剩下要做的就是把原始体素清空。正如你所看到的,整件事在一条线上很舒服。在</p>
<pre><code>import numpy as np
from scipy import ndimage
# create mock data
i,j,k = np.ogrid[:10,:10,:10]
ball = ((i-5)**2+(j-5)**2+(k-5)**2 < 16).view(np.int8)
# create border
shell = (ndimage.binary_dilation(ball) & ~ball).view(np.int8)
# that's all folks, show one section of original and border
print(ball[5])
# [[0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 0 0]
# [0 0 0 1 1 1 1 1 0 0]
# [0 0 1 1 1 1 1 1 1 0]
# [0 0 1 1 1 1 1 1 1 0]
# [0 0 1 1 1 1 1 1 1 0]
# [0 0 1 1 1 1 1 1 1 0]
# [0 0 1 1 1 1 1 1 1 0]
# [0 0 0 1 1 1 1 1 0 0]
# [0 0 0 0 0 0 0 0 0 0]]
print(shell[5])
# [[0 0 0 0 0 0 0 0 0 0]
# [0 0 0 1 1 1 1 1 0 0]
# [0 0 1 0 0 0 0 0 1 0]
# [0 1 0 0 0 0 0 0 0 1]
# [0 1 0 0 0 0 0 0 0 1]
# [0 1 0 0 0 0 0 0 0 1]
# [0 1 0 0 0 0 0 0 0 1]
# [0 1 0 0 0 0 0 0 0 1]
# [0 0 1 0 0 0 0 0 1 0]
# [0 0 0 1 1 1 1 1 0 0]]
</code></pre>