-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPVS_watershed_shrink.sh
executable file
·185 lines (181 loc) · 3.41 KB
/
FPVS_watershed_shrink.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
#!/bin/sh
#
# Create BEM surfaces using the watershed algoritm included with
# FreeSurfer
#
# Copyright 2006
#
# Matti Hamalainen
# Athinoula A. Martinos Center for Biomedical Imaging
# Massachusetts General Hospital
# Charlestown, MA, USA
#
# $Header: /space/orsay/8/users/msh/CVS/CVS-MSH/MNE/mne_scripts/mne_watershed_bem,v 1.7 2008/10/23 21:41:45 msh Exp $
# $log$
#
# OH, April '20
# Olaf:
shrink_by=2 # shrink by mm, used at bottom
cleanup()
{
echo "Temporary files removed."
}
usage()
{
echo "usage: $0 [options]"
echo
echo " --overwrite (to write over existing files)"
echo " --subject subject (defaults to SUBJECT environment variable)"
echo " --volume name (defaults to T1)"
echo " --atlas specify the --atlas option for mri_watershed"
echo
echo "Minimal invocation:"
echo
echo "$0 (SUBJECT environment variable set)"
echo "$0 --subject subject (define subject on the command line)"
echo
}
#
if [ ! "$FREESURFER_HOME" ] ; then
echo "The FreeSurfer environment needs to be set up for this script"
exit 1
fi
#
if [ ! "$SUBJECTS_DIR" ]
then
echo "The environment variable SUBJECTS_DIR should be set"
exit 1
fi
if [ ! "$MNE_ROOT" ]
then
echo "MNE_ROOT environment variable is not set"
exit 1
fi
force=false
atlas=
volume=T1
#
# Parse the options
#
while [ $# -gt 0 ]
do
case "$1" in
--subject)
shift
if [ $# -eq 0 ]
then
echo "--subject: argument required."
exit 1
else
export SUBJECT=$1
fi
;;
--volume)
shift
if [ $# -eq 0 ]
then
echo "--volume: argument required."
exit 1
else
export volume=$1
fi
;;
--overwrite)
force=true
;;
--atlas)
atlas=-atlas
;;
--help)
usage
exit 1
;;
esac
shift
done
#
# Check everything is alright
#
if [ ! "$SUBJECT" ]
then
usage
exit 1
fi
#
subject_dir=$SUBJECTS_DIR/$SUBJECT
mri_dir=$subject_dir/mri
T1_dir=$mri_dir/$volume
T1_mgz=$mri_dir/$volume.mgz
bem_dir=$subject_dir/bem
ws_dir=$subject_dir/bem/watershed3
#
if [ ! -d $subject_dir ]
then
echo "Could not find the MRI data directory $subject_dir"
exit 1
fi
if [ ! -d $bem_dir ]
then
mkdir -p $bem_dir
if [ $? -ne 0 ]
then
echo "Could not create the model directory $bem_dir"
exit 1
fi
fi
if [ ! -d $T1_dir -a ! -f $T1_mgz ]
then
echo "Could not find the MRI data"
exit 1
fi
if [ -d $ws_dir ]
then
if [ $force = "false" ]
then
echo "$ws_dir already exists. Use the --overwrite option to recreate it"
exit 1
else
rm -rf $ws_dir
if [ $? -ne 0 ]
then
echo "Could not remove $ws_dir"
exit 1
fi
fi
fi
#
# Report
#
echo
echo "Running mri_watershed for BEM segmentation with the following parameters"
echo
echo "SUBJECTS_DIR = $SUBJECTS_DIR"
echo "Subject = $SUBJECT"
echo "Result dir = $ws_dir"
echo
mkdir -p $ws_dir/ws
if [ $? -ne 0 ]
then
echo "Could not create the destination directories"
exit 1
fi
cleanup
if [ -f $T1_mgz ]
then
mri_watershed $atlas -useSRAS -shk_br_surf $shrink_by int_h -surf $ws_dir/$SUBJECT $T1_mgz $ws_dir/ws
else
mri_watershed $atlas -useSRAS -shk_br_surf $shrink_by int_h -surf $ws_dir/$SUBJECT $T1_dir $ws_dir/ws
fi
if [ $? -ne 0 ]
then
exit 1
fi
#
cd $bem_dir
rm -f $SUBJECT-head.fif
mne_surf2bem --surf $ws_dir/"$SUBJECT"_outer_skin_surface --id 4 --fif $SUBJECT-head.fif
echo "Created $bem_dir/$SUBJECT-head.fif"
echo
echo "Complete."
echo
exit 0