-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reorientation_imaging.py
81 lines (75 loc) · 2.24 KB
/
Reorientation_imaging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
######################################
# by YCH 20240119
# Reorient the position of a monkey from one orientation to another.
# usage: python Reorient_position.py InputFileName.nii.gz OutputFileName.nii.gz init_axcodes(eg: RIA) final_axcodes(eg: RAS)
######################################
import nibabel as nib
import numpy as np
import sys
def compute_orientation(init_axcodes, final_axcodes):
"""Compute the orientation of the image.
Parameters
----------
init_axcodes : str
The initial orientation of the image.
final_axcodes : str
The final orientation of the image.
Returns
-------
orientation : int
The orientation of the image.
"""
init_ornt = nib.orientations.axcodes2ornt(init_axcodes)
final_ornt = nib.orientations.axcodes2ornt(final_axcodes)
orientation = nib.orientations.ornt_transform(init_ornt, final_ornt)
return orientation
def reorient_image(image, orientation):
"""Reorient an image.
Parameters
----------
image : array, shape (I, J, K)
The image to reorient.
orientation : int
The orientation of the image.
Returns
-------
image : array, shape (I, J, K)
The reoriented image.
"""
image = nib.orientations.apply_orientation(image, orientation)
return image
def reorient_file(fileIn, fileOut, orientation):
"""Reorient a file.
Parameters
----------
fileIn : str
The input file.
fileOut : str
The output file.
orientation : int
The orientation of the image.
"""
imagineIn = nib.load(fileIn)
header = imagineIn.header
affine = imagineIn.affine
imgData = imagineIn.get_fdata()
imgReorient = reorient_image(imgData, orientation)
nib.Nifti1Image(imgReorient, affine=affine, header=header).to_filename(fileOut)
def main():
"""Reorient a file.
Parameters
----------
fileIn : str
The input file.
fileOut : str
The output file.
"""
fileIn = sys.argv[1]
fileOut = sys.argv[2]
init_axcodes = sys.argv[3]
final_axcodes = sys.argv[4]
orientation = compute_orientation(init_axcodes, final_axcodes)
print(orientation)
reorient_file(fileIn, fileOut, orientation)
if __name__ == "__main__":
main()