ar6
ar6

Reputation: 23

Reduce 3D volume mask by uniform margin in python

I'm working with 3D boolean arrays that mask a volume. My goal is to take a mask and reduce the area of the mask by some margin, m, in all dimensions.

Is there an easy way to do this using some common libraries (numpy, scipy, pandas, etc..)?

I found some code online that uses multiple for loops to expand a mask by one dimension. This works for the expansion case but I feel like there is a more compact way out there.

Here is a minimum example of what I am looking for in 2D.

Original

array([[0., 0., 1., 0., 0.],
       [0., 1., 1., 1., 0.],
       [1., 1., 1., 1., 1.],
       [0., 1., 1., 1., 0.],
       [0., 0., 1., 0., 0.]])

Uniform reduction by 1 pixel

array([[0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 1., 1., 1., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 0.]])

But I want this to be in 3D space. Thank you for any input.

Upvotes: 2

Views: 420

Answers (1)

Eric
Eric

Reputation: 97565

You might be looking for scipy.ndimage.binary_erosion(a):

a = np.array([
    [0., 0., 1., 0., 0.],
    [0., 1., 1., 1., 0.],
    [1., 1., 1., 1., 1.],
    [0., 1., 1., 1., 0.],
    [0., 0., 1., 0., 0.]
])
b = scipy.ndimage.binary_erosion(a)  # returns an array of bool

Note that this will erode internal surfaces too

Upvotes: 2

Related Questions