Skip to content

Conversation

@cookpa
Copy link
Member

@cookpa cookpa commented Oct 8, 2025

The docstring said:

level : scalar or tuple of scalars
Level(s) at which to mask image. If vector or list of values, output image is non-zero at all locations where label image matches any of the levels specified.

But this is only true if binarize is True.

The binarize option

binarize : boolean
whether binarize the output image

didn't actually binarize the output, it binarized the mask without reference to the scalar image. So

>>> masked = ants.mask_image(img, mask) # NOT non-zero where mask matches level
>>> masked_binary = ants.mask_image(img, mask, binarize=True) # NOT a binarized version of masked

Now fixed to always mask the scalar image, and binarize afterwards if requested. This can be used to subset labels with

ants.mask_image(labels,labels, level=(1,2,3,4))

or create a binary mask

ants.mask_image(labels,labels, level=(1,2,3,4), binarize=True)

@cookpa cookpa linked an issue Oct 8, 2025 that may be closed by this pull request
@coveralls
Copy link

coveralls commented Oct 8, 2025

Coverage Status

coverage: 76.915% (+0.01%) from 76.901%
when pulling de7b650 on mask_binarize
into c8b0add on master.

@cookpa
Copy link
Member Author

cookpa commented Oct 9, 2025

@stnava @ntustison any objections to this fix? I looked at antspynet and antsMM, and I don't see any uses that would be affected.

@ntustison
Copy link
Member

@cookpa Yeah, fine with me. I didn't write this and can't remember anytime I've ever used it.

@cookpa cookpa merged commit 9e3f8ac into master Oct 9, 2025
2 checks passed
@cookpa cookpa deleted the mask_binarize branch October 9, 2025 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mask_image documentation is confusing

4 participants