Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Indexing images returns cropped images #626

Closed
wants to merge 17 commits into from
Closed

Conversation

ncullen93
Copy link
Member

Currently if you index an ants image it always returns a numpy array. The intuitive response to indexing an image should instead be an image. And that image should retain the spatial properties of the original image.

If you index only one dimension, that should return a 1D numpy array. And if you index on one value then you get that value.

Example:

import ants
img = ants.image_read(ants.get_data('mni'))

img[:10,:10,:10] # an ants image with shape (10,10,10)
img[10:20,10:20,10:20] # same as above but the origin has been shifted by 10 in all directions

img[:10,20,:10] # 2D image

img[:10,10,10] # 1D numpy array

img[10,10,10] # single value

@ncullen93
Copy link
Member Author

Also, indexing images with images works more as expected. The index image will act as a mask so any indexes in the image which are zero in the mask will be turned to zero.

Example:

import ants
img = ants.image_read(ants.get_data('r16'))
mask = img > 160
img2 = img[mask] # same shape as img but now values less than 160 have been set to 0

@cookpa
Copy link
Member

cookpa commented May 14, 2024

mask = img > 160
img2 = img[mask] # same shape as img but now values less than 160 have been set to 0

This one might cause some confusion, I do this to mean give me the voxels inside the mask.

@ncullen93
Copy link
Member Author

Ok, thanks for letting me know because I was also very torn about this one. I guess that same effect can be achieved with img * mask so it makes sense to keep the original functionality. Should it always be a flattened array of values or should it maintain the dimensions?

@coveralls
Copy link

coveralls commented May 15, 2024

Coverage Status

coverage: 80.91% (+0.4%) from 80.524%
when pulling 21f5f32 on index-to-image
into 0903c42 on master.

@cookpa
Copy link
Member

cookpa commented May 16, 2024

I'd say flat array, but would also ask @stnava and @ntustison

@ncullen93
Copy link
Member Author

That's the current behavior so it makes sense to keep it.

@ncullen93 ncullen93 closed this May 20, 2024
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.

None yet

3 participants