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
628 stars 161 forks source link

Minimizing deformation when applying registration with two identical images #309

Closed IlvaHou55 closed 4 months ago

IlvaHou55 commented 2 years ago

Hi everyone,

Two days ago I opened this issue on the global ANTs page. I encountered that with ants.registration(fixed=fixedmri, moving=fixedmri, type_of_transform='SyN') my original image and registered image were quite different, where you would expect no transformation because two identical images are used for the fixed and moving image.

Please see the thread for a reproducible example.

Now I wondered how I could reproduce the example of @cookpa in ANTsPy:

export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1
antsRegistration -d 3 -u 0 -v 1 \
   -t SyN[0.1] \
   -m Mattes[ MRI_4.nii , MRI_4.nii , 1, 64, Regular ] \
   -c [ 25, 1e-8 ,10 ] -f 1 -s 0 \
   -o [ MRI_4ToSelf, MRI_4ToSelfDeformed.nii.gz ] 

I thought of: grad_step=0.1 because of -t SyN[0.1] aff_sampling=64 and syn_sampling=64 because of -m Mattes[ MRI_4.nii , MRI_4.nii , 1, 64, Regular ] reg_iterations=(25, 1e-8, 10) because of -c [ 25, 1e-8 ,10 ]

However, regarding the smoothing and downsampling, the ANTsPy docs state 'we will set the smoothing and multi-resolution parameters based on the length of this (reg_iterations) vector.' Does this mean I cannot manually set these parameters to zero-smoothing and zero-downsampling?

Thank you in advance.

cookpa commented 2 years ago

I think you want reg_iterations=(X) for X iterations at full resolution. If you have reg_iterations = (X,Y,Z) it will do three levels: X downsampled 4x, Y downsampled 2x, and Z at full resolution. You may also want type_of_transform='SyNOnly' to disable affine registration.

I'm not sure how you set the other convergence parameters.

stnava commented 2 years ago

MI is not the best choice for detecting images with zero difference. if you want to add such a check you might do:

if mean( imgF - imgM ) == 0 : return identity else : do registration

as others said, mutual information is a probabilistic measurement with built-in uncertainty. i would not worry about sub-voxel displacements coming out of it.

stnava commented 2 years ago

also - I would recommend using ANTsPy I/O as follows:

import ants
img=ants.image_read("MRI_4.nii").iMath("Normalize")
img2=ants.image_read("MRI_4.nii").iMath("Normalize")
reg=ants.registration(img,img2,'SyN')
# check mag of def
warp=ants.image_read( reg['fwdtransforms'][0] ).split_channels()
for k in range(3):
     print(warp[k].abs().max())
# 0.115857765
# 0.25167125
# 0.15904859

voxel size is 1mm so these max displacements are far under that. you should read these as effectively zero.

IlvaHou55 commented 2 years ago

@cookpa Thank you for your reply. Why would you want to disable the affine transformation?

cookpa commented 2 years ago

Just trying to isolate the variability in SyN.

IlvaHou55 commented 2 years ago

@cookpa I see, but for my application I need them both, so think it is fair if I use them both in this test case.

IlvaHou55 commented 2 years ago

@stnava Thank you for your reply. Would you always normalize the images before registration?

stnava commented 2 years ago

if they have negative values, then yes