Arbitrary Slice of 3D Array in Python

Question:

I have a 3D volume data stored in an 3-dimension array with background value 0 and volume value 1.
Now I want to get arbitrary section planes of this volume. I read the answer here:How can an almost arbitrary plane in a 3D dataset be plotted by matplotlib?

But it seems that the accepted answer was wrong, it generates mapped coordinates of xoy plane but not the slice coordinates.
So how to get the correct shape of the slice plane? Are there any methods of transforming the mapped shape to original shape?
Thanks!

Asked By: Silver0427

||

Answers:

The question might be outdated, but looks like the following function from scipy.ndimage might solve your issue.

What scipy.ndimage.interpolation.rotate does is it rotates the full 3d array around either of the 3 axes by a certain angle interpolating the stored values to the new “cells”. It also resizes (extends) the new array accordingly, filling the new empty cells by a value you specify. After that you can take slice as you would normally do, say: array[:,sy//2,:].

So in short, here’s a central cut around a diagonal parallel to the z-axis (for simplicity):

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

You can actually think a bit further and extend this to an arbitrary slice by rotating it around different axes several times.

PS. If you feel it takes too much time, you can sacrifice the quality of interpolation by passing the order=0 (default is order=3). This will run faster.

Answered By: hayk

I think the answer I have given on this other question is useful for whoever is also having issues at reslicing a 3D array as wished.

Answered By: Pedro Osório
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.