-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransform.py
266 lines (185 loc) · 7.97 KB
/
Transform.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# %%
import glob
import os
import time
import numpy as np
import cv2
from scipy import ndimage
import slicer
import Elastix
import JupyterNotebooksLib as slicernb
import sitkUtils as su
import SimpleITK as sitk
# %%
root_dir = 'E:\\Test_data_set'
mni_path = 'C:\\Users\\User\\AppData\\Local\\NA-MIC\\Slicer 4.11.20200930\\mni_icbm152_t1_tal_nlin_asym_09a.nii'
# %%
sub_path_list = sorted(glob.glob(root_dir))
# %%
sub_path_list
# %%
def load_nii(path):
"""Load dicom files to sitk image"""
reader = sitk.ImageFileReader()
reader.SetImageIO("NiftiImageIO")
reader.SetFileName(path)
image = reader.Execute()
return image
# %%
def registration_elastix(fixedVolumeNode, movingVolumeNode):
"""Ridgid registration Elastix"""
# Set Parmeter
elastixLogic = Elastix.ElastixLogic()
parameterFilenames = elastixLogic.getRegistrationPresets()[0][Elastix.RegistrationPresets_ParameterFilenames]
outputVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode")
outputTransformNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTransformNode")
regi = elastixLogic.registerVolumes(fixedVolumeNode, movingVolumeNode, parameterFilenames=parameterFilenames,
outputVolumeNode=outputVolumeNode, outputTransformNode=outputTransformNode)
return outputVolumeNode, outputTransformNode
# if (slicer.util.getNode("register_tmp") == None):
# outputVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode").SetName("register_tmp")
# else:5 -
# outputVolumeNode = slicer.util.getNode("register_tmp")
# Execute
# %%
def write_nii(image, path):
"""Write medical image to nii"""
sitk.WriteImage(image, path)
# %%
def rescaleIntentsity(image, min_intensity, max_intensity):
"""Rescale Image intensity range of [min , max]"""
return sitk.RescaleIntensity(image, min_intensity, max_intensity)
# %%
def TransformMarkups(transformNode):
# originalFidNode=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsFiducialNode")
markupsNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLMarkupsFiducialNode')
# markupsNode.AddControlPoint(vtk.vtkVector3d(0, -1, -1)) #AC
# markupsNode.AddControlPoint(vtk.vtkVector3d(0, -23, 0 )) #PC
markupsNode.AddControlPoint(vtk.vtkVector3d(8, -73.0, 19.0)) # V1
markupsNode.AddControlPoint(vtk.vtkVector3d(37, -13.0, 51.0)) # M1
# markupsNode.AddControlPoint(vtk.vtkVector3d(42, -20.0, 48.0 )) #S1
markupsNode.AddControlPoint(vtk.vtkVector3d(5.0, 14.0, 47)) # Dosal_ACC + up5
markupsNode.AddControlPoint(vtk.vtkVector3d(12, -15, 18.0)) # Thalamus
# markupsNode.AddControlPoint(vtk.vtkVect|or3d(5.0, 14.0, 57.0)) #Dorsal_ACC + up15
# markupsNode.AddControlPoint(vtk.vtkVector3d(5.0, -10.0, 51.0)) #Caudal_ACC
# markupsNode.AddControlPoint(vtk.vtkVector3d(5.0, -10.0, 56.0)) #Caudal_ACC + up5
# markupsNode.AddControlPoint(vtk.vtkVector3d(5.0, -10.0, 61.0)) #Caudal_ACC + up15
# markupsNode.AddControlPoint(vtk.vtkVector3d(8, -73.0 - 5, 19.0)) #V1 _5
# markupsNode.AddControlPoint(vtk.vtkVector3d(37, -13.0, 51.0 + 5)) #M1 _ 5
# markupsNode.AddControlPoint(vtk.vtkVector3d(42, -20.0, 48.0 + 5)) #S1 _ 5
markupsNode.SetAndObserveTransformNodeID(transformNode.GetID())
# slicer.modules.transforms.logic().CreateDisplacementVolumeFromTransform(transformNode, markupsNode, False)
narray = slicer.util.arrayFromMarkupsControlPoints(markupsNode, world=True)
print(narray)
slicer.mrmlScene.RemoveNode(markupsNode)
return narray
# %%
scene = slicer.mrmlScene
sub_path_list = sorted(glob.glob(os.path.join(root_dir, 'S*')))
print(len(sub_path_list))
i = 0
# %%
sub_path_list
# %%
scene = slicer.mrmlScene
sub_path_list = sorted(glob.glob(os.path.join(root_dir, 'S*')))
print(len(sub_path_list))
i = 0
mni_image = load_nii(mni_path)
MNI_node = su.PushVolumeToSlicer(mni_image)
for sub_path in sub_path_list:
try:
sub_name = sub_path.split("\\")[-1].split("_")[0]
print(i)
i += 1
T1_path = glob.glob(os.path.join(sub_path, '*crop_T1.nii'))[0]
T1_image = load_nii(T1_path)
print('load dicom', T1_path)
# sitk image to VolumeNode
T1_node = su.PushVolumeToSlicer(T1_image)
print('push image to volumenode')
MNI_registeredNode, MNI_transformNode = registration_elastix(T1_node, MNI_node)
print('mni-registration')
target_np = TransformMarkups(MNI_transformNode)
sub_name = sub_path.split("\\")[-1].split("_")[0]
save_path = os.path.join(sub_path)
if not os.path.exists(save_path):
os.makedirs(save_path)
print(save_path)
V1 = str(
'{:.3f}'.format(target_np[0][0]) + ' {:.3f}'.format(target_np[0][1]) + ' {:.3f}'.format(target_np[0][2]))
f = open(os.path.join(save_path, 'target_V1_MNI.txt'), 'w')
f.write(V1)
f.close()
M1 = str(
'{:.3f}'.format(target_np[1][0]) + ' {:.3f}'.format(target_np[1][1]) + ' {:.3f}'.format(target_np[1][2]))
f = open(os.path.join(save_path, 'target_M1_MNI.txt'), 'w')
f.write(M1)
Dorsal_ACC = str(
'{:.3f}'.format(target_np[2][0]) + ' {:.3f}'.format(target_np[2][1]) + ' {:.3f}'.format(target_np[2][2]))
f = open(os.path.join(save_path, 'target_DorsalACCUp5.txt'), 'w')
f.write(Dorsal_ACC)
f.close()
Thalamus = str(
'{:.3f}'.format(target_np[3][0]) + ' {:.3f}'.format(target_np[3][1]) + ' {:.3f}'.format(target_np[3][2]))
f = open(os.path.join(save_path, 'target_Thalamus.txt'), 'w')
f.write(Thalamus)
f.close()
print('save target mni to native space')
# clear
slicer.mrmlScene.RemoveNode(T1_node)
slicer.mrmlScene.RemoveNode(MNI_transformNode)
slicer.mrmlScene.RemoveNode(MNI_registeredNode)
except Exception as e:
print(e)
print(sub_path, 'wrong')
continue
# %%
scene = slicer.mrmlScene
sub_path_list = sorted(glob.glob(os.path.join(root_dir, 'S*')))
print(len(sub_path_list))
i = 0
mni_image = load_nii(mni_path)
MNI_node = su.PushVolumeToSlicer(mni_image)
for sub_path in sub_path_list:
try:
sub_name = sub_path.split("\\")[-1].split("_")[0]
print(i)
i += 1
T1_path = glob.glob(os.path.join(sub_path, '*crop_T1.nii'))[0]
T1_image = load_nii(T1_path)
print('load dicom', T1_path)
# sitk image to VolumeNode
T1_node = su.PushVolumeToSlicer(T1_image)
print('push image to volumenode')
MNI_registeredNode, MNI_transformNode = registration_elastix(T1_node, MNI_node)
print('mni-registration')
target_np = TransformMarkups(MNI_transformNode)
sub_name = sub_path.split("\\")[-1].split("_")[0]
save_path = os.path.join(sub_path)
if not os.path.exists(save_path):
os.makedirs(save_path)
print(save_path)
V1 = str(
'{:.3f}'.format(target_np[0][0]) + ' {:.3f}'.format(target_np[0][1]) + ' {:.3f}'.format(target_np[0][2]))
f = open(os.path.join(save_path, 'target_V1_MNI.txt'), 'w')
f.write(V1)
f.close()
M1 = str(
'{:.3f}'.format(target_np[1][0]) + ' {:.3f}'.format(target_np[1][1]) + ' {:.3f}'.format(target_np[1][2]))
f = open(os.path.join(save_path, 'target_M1_MNI.txt'), 'w')
f.write(M1)
Dorsal_ACC = str(
'{:.3f}'.format(target_np[2][0]) + ' {:.3f}'.format(target_np[2][1]) + ' {:.3f}'.format(target_np[2][2]))
f = open(os.path.join(save_path, 'target_DorsalACCUp5.txt'), 'w')
f.write(Dorsal_ACC)
f.close()
print('save target mni to native space')
# clear
slicer.mrmlScene.RemoveNode(T1_node)
slicer.mrmlScene.RemoveNode(MNI_transformNode)
slicer.mrmlScene.RemoveNode(MNI_registeredNode)
except Exception as e:
print(e)
print(sub_path, 'wrong')
continue