-
Notifications
You must be signed in to change notification settings - Fork 0
/
NiftiAnonymizer.sh
executable file
·294 lines (208 loc) · 7.14 KB
/
NiftiAnonymizer.sh
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
# FullAnonymizer.sh
# Script is used to anonymize nifti header and deface anatomical nifti images
# Written by: Ranjeet Khanuja, 2012 __Child Mind Institute__.
# License type: Creative Commons, Attribution - non-commercial
# Defacing routine based upon defacer.py by: Russ Poldrack via Github
# Template taken is the standard fsl brain template
# The defacing module works for images which does not have neck tissue
#no of arguments
ARGS=2
#mask to be used
face_mask=facemask_mni.nii.gz
template=MNI152_T1_1mm_brain.nii.gz
#temperary variables defined
tempfile=tempfile
tmpmat=tmpmat
invmat=invmat
analyze=analyze
new=_new
out=_out
headfile=.hdr
imagefile=.img
gz=.nii.gz
nii=.nii
deface=_defaced
rpi=_RPI
temp=_temp
dot=.
bet=_bet
change=_chg
if [ $# -ne "$ARGS" ]
then
echo "Usage: $0 NIFTI_File Is_This_Nifti_File_Anatomical - yes/no"
echo "Example: ./NiftiAnonymize.sh mprage.nii.gz yes"
exit 1
fi
if [ -f "$1" ]
then
input_nifti_file=$1
else
echo " File \"$1\" does not exist. "
exit 1
fi
echo "process begins"
xpath=$(dirname $input_nifti_file)
xpath=$xpath"/"
filename=$(basename $input_nifti_file)
extension=${filename#*.}
filename=${filename%%.*}
#nifti_file=$filename$change$dot$extension
nifti_file=$filename$change$gz
echo filepath is $xpath
echo filename is $filename
echo extension is $extension
#getting slope
scl_slope=$(fslval $input_nifti_file scl_slope)
scl_inter=$(fslval $input_nifti_file scl_inter)
echo "slope is $scl_slope"
echo "changing the data type to float"
fslmaths $input_nifti_file $nifti_file -odt float
###change nifti header
#change file to nifti_pair (.hdr and .img)
fslchfiletype NIFTI_PAIR $nifti_file $analyze
#get the required header information pizels, TR, dimensions and dataype
dim1=$(fslval $nifti_file dim1)
dim2=$(fslval $nifti_file dim2)
dim3=$(fslval $nifti_file dim3)
dim4=$(fslval $nifti_file dim4)
datatype=$(fslval $nifti_file datatype)
pixdim1=$(fslval $nifti_file pixdim1)
pixdim2=$(fslval $nifti_file pixdim2)
pixdim3=$(fslval $nifti_file pixdim3)
pixdim4=$(fslval $nifti_file pixdim4)
qform=$(fslval $nifti_file qform)
echo "Creating new Header"
#create new header from the nifti image
#default origin as 0 0 0
cmd="fslcreatehd $dim1 $dim2 $dim3 $dim4 $pixdim1 $pixdim2 $pixdim3 $pixdim4 0 0 0 16 _temp"
echo ${cmd}
$cmd
new_file=$temp$gz
#change filetype of newly created nifti file into anlayze
fslchfiletype NIFTI_PAIR $new_file $new
#copy orientation information
fslcpgeom $analyze$headfile $new$headfile
#copy new header into old header
cp $new$headfile $analyze$headfile
#new nifti file name
new_nifti_file=$xpath$filename$new$gz
temp_nifti_file=$filename$temp
if [ ${extension} = "nii.gz" ]
then
fslchfiletype NIFTI_GZ $analyze $temp_nifti_file
temp_nifti_file=$temp_nifti_file$gz
else
fslchfiletype NIFTI $analyze $temp_nifti_file
temp_nifti_file=$temp_nifti_file$nii
fi
echo "changing the data type back to short"
slope=${scl_slope/.*}
if [ $slope -gt 0 ]
then
echo "scaling down voxel data"
out_temp_file=$filename$out$gz
fslmaths $temp_nifti_file -sub $scl_inter -div $scl_slope $out_temp_file
fslmaths $out_temp_file $new_nifti_file -odt short
rm $out_temp_file
else
fslmaths $temp_nifti_file $new_nifti_file -odt short
fi
# remove the temperory files created
rm $analyze$headfile
rm $analyze$imagefile
rm $new$headfile
rm $new$imagefile
rm $new_file
rm $nifti_file
rm $temp_nifti_file
echo "New Nifti Image file with changed header info --> $new_nifti_file"
function deface_nifti {
echo "Checking for facemask and tempelate files"
if [ ! -f "$face_mask" ]
then
echo " File \"$face_mask\" does not exist. "
exit 1
fi
if [ ! -f "$template" ]
then
echo " File \"$template\" does not exist. "
exit 1
fi
defaced_file=${xpath}${filename}${new}${deface}$gz
nifti_file_rpi=${xpath}${filename}${new}${rpi}$gz
echo "Checking the orientation"
x_orient=$(fslval $input_nifti_file qform_xorient)
z_orient=$(fslval $input_nifti_file qform_zorient)
y_orient=$(fslval $input_nifti_file qform_yorient)
echo "Orientation values $x_orient, $y_orient, $z_orient"
if [ ${x_orient} = "Anterior-to-Posterior" -a ${z_orient} = "Left-to-Right" -a ${y_orient} = "Inferior-to-Superior" ]
then
echo "reorienting nifti image to RPI orientaion"
fslswapdim $new_nifti_file -z -x y $nifti_file_rpi
elif [ ${x_orient} = "Left-to-Right" -a ${z_orient} = "Anterior-to-Posterior" -a ${y_orient} = "Inferior-to-Superior" ]
then
echo "reorienting nifti image to RPI orientaion"
fslswapdim $new_nifti_file -x -z y $nifti_file_rpi
elif [ ${x_orient} = "Left-to-Right" -a ${z_orient} = "Inferior-to-Superior" -a ${y_orient} = "Posterior-to-Anterior" ]
then
echo "reorienting nifti image to RPI orientaion"
fslswapdim $new_nifti_file -x y z $nifti_file_rpi
elif [ ${x_orient} = "Right-to-Left" -a ${z_orient} = "Anterior-to-Posterior" -a ${y_orient} = "Inferior-to-Superior" ]
then
echo "reorienting nifti image to RPI orientaion"
fslswapdim $new_nifti_file x -z y $nifti_file_rpi
elif [ ${x_orient} = "Right-to-Left" -a ${z_orient} = "Posterior-to-Anterior" -a ${y_orient} = "Inferior-to-Superior" ]
then
echo "reorienting nifti image to RPI orientaion"
fslswapdim $new_nifti_file x z y $nifti_file_rpi
elif [ ${x_orient} = "Anterior-to-Posterior" -a ${z_orient} = "Right-to-Left" -a ${y_orient} = "Superior-to-Inferior" ]
then
echo "reorienting nifti image to RPI orientaion"
fslswapdim $new_nifti_file z -x -y $nifti_file_rpi
else
echo "Image is already is RPI orientation"
cp $new_nifti_file $nifti_file_rpi
fi
#checking if fslswapdim changed the oreitnation correctly or not
x_orient=$(fslval $nifti_file_rpi qform_xorient)
if [ ${x_orient} = "Left-to-Right" ]
then
echo "changing the orientation in the header"
fslorient -swaporient $nifti_file_rpi
fi
#echo "File with RPI orientation --> $nifti_file_rpi"
echo "Defacing Begins..."
tmpfile=${tempfile}$gz
nifti_file_rpi_bet=${filename}${rpi}${bet}$gz
echo "run bet"
#remove skull and put the output in a temperary file
bet $nifti_file_rpi $nifti_file_rpi_bet
echo "run flirt 1"
flirt -in $nifti_file_rpi_bet -ref $template -omat $tmpmat
echo "get transformation matrix, invmat"
convert_xfm -inverse -omat $invmat $tmpmat
echo "run flirt 2"
flirt -in $face_mask -out $tmpfile -ref $nifti_file_rpi -applyxfm -init $invmat
echo "run fslmaths"
fslmaths $nifti_file_rpi -mul $tmpfile $defaced_file
#remove temperary files
echo "remove temperary files"
rm $nifti_file_rpi_bet
rm $tmpmat
rm $invmat
rm $tmpfile
rm $nifti_file_rpi
return 0
}
if [ "$2" == "yes" ]
then
echo "Anatomical Nifti Image"
echo "Make sure you have \"$face_mask\" and \"$template\" present in the current directory"
"deface_nifti"
echo "The defaced nifti file is --> $defaced_file"
echo "Defacing Finishes..."
else
echo "No defacing for this file"
fi
echo "process complete"