Last active
February 25, 2023 20:32
-
-
Save valosekj/a03195d9060b0e164faff95102129feb to your computer and use it in GitHub Desktop.
Fix ITK direction for nnU-Net
This file contains hidden or 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
# Set ITK direction from image1 to image2 | |
# | |
# Usage: | |
# python change_itk_orientation.py <image1> <image2> | |
# Example: | |
# python change_itk_orientation.py sub-001_T1w.nii.gz sub-001_T1w_seg.nii.gz | |
# | |
# Jan Valosek | |
# | |
import sys | |
import SimpleITK as sitk | |
# Read images | |
image1 = sitk.ReadImage(sys.argv[1]) | |
image2 = sitk.ReadImage(sys.argv[2]) | |
print(f'image1 direction:\n{image1.GetDirection()}') | |
print(f'image2 direction:\n{image2.GetDirection()}') | |
# Change image2 direction | |
print('changing image2 direction...') | |
image2.SetDirection(image1.GetDirection()) | |
# Save updated image2 | |
sitk.WriteImage(image2, sys.argv[2].replace('.nii.gz', '_updated_dir.nii.gz')) |
This file contains hidden or 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
# Read ITK origin, spacing, direction, and size | |
# Details: https://simpleitk.readthedocs.io/en/master/fundamentalConcepts.html | |
# | |
# Usage: | |
# python read_itk.py <image1> <image2> | |
# Example: | |
# python read_itk.py sub-001_T1w.nii.gz sub-001_T1w_seg.nii.gz | |
# | |
# Jan Valosek | |
# | |
import sys | |
import SimpleITK as sitk | |
# Read images | |
image1 = sitk.ReadImage(sys.argv[1]) | |
image2 = sitk.ReadImage(sys.argv[2]) | |
ori1, spacing1, direction1, size1 = image1.GetOrigin(), image1.GetSpacing(), image1.GetDirection(), image1.GetSize() | |
ori2, spacing2, direction2, size2 = image2.GetOrigin(), image2.GetSpacing(), image2.GetDirection(), image2.GetSize() | |
print("origin:") | |
print(ori1) | |
print(ori2) | |
print("spacing:") | |
print(spacing1) | |
print(spacing2) | |
print("direction:") | |
print(direction1) | |
print(direction2) | |
print("size:") | |
print(size1) | |
print(size2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment