Created
March 16, 2021 02:48
-
-
Save vfmatzkin/e120c1b9189268011f02a05af0560a42 to your computer and use it in GitHub Desktop.
I can't believe ANTs is that good in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script written by Franco Matzkin (with the great help of this amazing tool) | |
# Install ANTsPy with: | |
# pip install antspyx | |
# (I'm not sure this is the last version). | |
import ants | |
moving_ct_path = 'imgs/CTimage.nii.gz' | |
moving_mask_path = 'imgs/CT_brain_mask.nii.gz' | |
fixed_ct_path = 'imgs/Atlas.nii.gz' | |
ct = ants.image_read(moving_ct_path) | |
mask = ants.image_read(moving_mask_path) | |
fixed = ants.image_read(fixed_ct_path) | |
# Register | |
my_tx = ants.registration(fixed=fixed, moving=ct, | |
type_of_transform='AffineFast', | |
default_value=-1500) | |
transf_ct = my_tx['warpedmovout'] | |
reg_transform = my_tx['fwdtransforms'] | |
# Save transform | |
ants.write_transform(ants.read_transform(reg_transform[0]), '/tmp/reg_ct.mat') | |
reg_ct_path = '/tmp/reg_ct.nii.gz' | |
ants.image_write(transf_ct, reg_ct_path) | |
# Apply the same transform to the mask | |
transf_mask = ants.apply_transforms(fixed=transf_ct, moving=mask, | |
transformlist=reg_transform, | |
interpolator='nearestNeighbor') | |
reg_mask_path = '/tmp/reg_mask.nii.gz' | |
ants.image_write(transf_mask, reg_mask_path) | |
# Go back to the original size | |
inverse_image = ants.apply_transforms(fixed=ct, moving=transf_ct, | |
transformlist=reg_transform, | |
interpolator='linear', imagetype=0, | |
whichtoinvert=[True]) | |
inverse_mask = ants.apply_transforms(fixed=ct, moving=transf_mask, | |
transformlist=reg_transform, | |
interpolator='nearestNeighbor', | |
imagetype=0, | |
whichtoinvert=[True]) | |
inverse_image_path = '/tmp/inv_reg_ct.nii.gz' | |
ants.image_write(inverse_image, inverse_image_path) | |
inverse_mask_path = '/tmp/inv_reg_mask.nii.gz' | |
ants.image_write(inverse_mask, inverse_mask_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The 'default_value' seems to not have any effect on ANTsPy 0.2.7.