ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
608 stars 161 forks source link

unabele to reproduce ants.registration['warpedmovout'] using ANTsTransform.apply_to_image #512

Closed MAeolus closed 8 months ago

MAeolus commented 8 months ago

Describe the bug Hello, I'm trying to apply the transforms generated by ants.registration to the same moving image using apply_to_image to get the same warped image of ants.registration, however, it returns an image with every voxel intensity equal to zero.

To Reproduce Here's what I'm trying to do:

fix_img = ants.image_read(fix_img_dir)
mov_img = ants.image_read(mov_img_dir)

outs = ants.registration(fix_img, mov_img, type_of_transform='Affine', outprefix=save_path + '/', verbose=True)
reg_img = outs['warpedmovout']
ants.image_write(reg_img, os.path.join(save_path, 'warpedmovout.nii.gz'))

affmat = ants.read_transform(os.path.join(save_path, '0GenericAffine.mat'))  # load the transforms generated by ants.registration
affine_img = affmat.apply_to_image(mov_img, reference=None, interpolation='linear')  # apply to the same moving image
ants.image_write(affine_img, os.path.join(save_path, 're_warped.nii.gz'))

reg_img is totally fine but affine_img returned by apply_to_image is just a matrix of zeros while they are supposed to be the same.

antspyx== 0.4.2

Any idea why this occurs? Thanks a lot!

cookpa commented 8 months ago

Maybe try

affine_img = affmat.apply_to_image(mov_img, reference=fix_img, interpolation='linear')
MAeolus commented 8 months ago

Maybe try

affine_img = affmat.apply_to_image(mov_img, reference=fix_img, interpolation='linear')

Thanks! It works. By the way, I've found that it works when interpolation='linear' but when interpolation='nearestNeighbor' there will have

Traceback (most recent call last): File "./reg_affine-V3-batch.py", line 107, in <module> affine_img = affmat.apply_to_image(mov_img, reference=fix_img, interpolation='nearestNeighbor') # apply to the same moving image File "/public/data/admin/anaconda3/envs/py37/lib/python3.7/site-packages/ants/core/ants_transform.py", line 178, in apply_to_image img_ptr = tform_fn(self.pointer, image.pointer, reference.pointer, interpolation) RuntimeError: /project/itksource/Modules/Filtering/ImageGrid/include/itkResampleImageFilter.hxx:540: ITK ERROR: ResampleImageFilter(0x16eb8850): Interpolator not set Is nearest neighbor interpolation not supported yet?

cookpa commented 8 months ago

It might be case sensitive, try nearestneighbor

MAeolus commented 8 months ago

It might be case sensitive, try nearestneighbor

Problem solved! Thanks.