import ants
import numpy as np
img1 = ants.image_read( ants.get_ants_data('r16') )
img2 = ants.image_read( ants.get_ants_data('r27') )
img3 = ants.image_read( ants.get_ants_data('r64') )

"""
Explanation: concatenation of multiple registrations to link data across different images
End of explanation
"""


seg1 = ants.kmeans_segmentation( img1, 3 )
ants.plot( img1, seg1['segmentation'] )

"""
Explanation: Segment image 1 and map this to image3 by way of image2.
End of explanation
"""


reg12 = ants.registration( img1, img2, 'SyN', reg_iterations = [100,100,20] )
reg23 = ants.registration( img2, img3, 'SyN', reg_iterations = [100,100,20] )

"""
Explanation: Compute quick registrations with SyN.
End of explanation
"""


mytx = reg23[ 'invtransforms'] + reg12[ 'invtransforms'] 

"""
Explanation: We concatenate the transformations.
End of explanation
"""


mywarpedimage = ants.apply_transforms( fixed = img3, 
                                       moving = seg1['segmentation'] , 
                                       transformlist = mytx, 
                                       interpolator  = 'nearestNeighbor', 
                                       whichtoinvert = [True,False,True,False])
ants.plot( img3, mywarpedimage, overlay_alpha = 0.5 )

"""
Explanation: Apply the concatentated transformations, taking care to invert appropriately.
End of explanation
"""


