Skip to content

Commit 1023a76

Browse files
authored
Merge pull request #1255 from ANTsX/antsInitAffine
ENH: Allow users to specify an initial transform
2 parents 31f7a61 + 05813f8 commit 1023a76

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

Scripts/antsBrainExtraction.sh

+51-38
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,27 @@ Required arguments:
5454
5555
Optional arguments:
5656
57-
-c: Tissue classification A k-means segmentation is run to find gray or white matter around
57+
-c: Tissue classification A k-means segmentation is run to find gray or white matter around
5858
the edge of the initial brain mask warped from the template.
5959
This produces a segmentation image with K classes, ordered by mean
6060
intensity in increasing order. With this option, you can control
61-
K and tell the script which classes represent CSF, gray and white matter.
61+
K and tell the script which classes represent CSF, gray and white matter.
6262
Format (\"KxcsfLabelxgmLabelxwmLabel\")
63-
Examples:
63+
Examples:
6464
-c 3x1x2x3 for T1 with K=3, CSF=1, GM=2, WM=3 (default)
6565
-c 3x3x2x1 for T2 with K=3, CSF=3, GM=2, WM=1
6666
-c 3x1x3x2 for FLAIR with K=3, CSF=1 GM=3, WM=2
6767
-c 4x4x2x3 uses K=4, CSF=4, GM=2, WM=3
6868
6969
-f: Brain extraction registration mask Mask used for registration to limit the metric computation to
7070
a specific region.
71-
-s: image file suffix Any of the standard ITK IO formats e.g. nrrd, nii.gz (default), mhd
72-
-u: use random seeding Use random number generated from system clock in Atropos (default = 1)
73-
-k: keep temporary files Keep brain extraction/segmentation warps, etc (default = false).
74-
-q: use floating point precision Use antsRegistration with floating point precision.
71+
-r: Initial moving transform An ITK affine transform (eg, from antsAI or ITK-SNAP) for the moving image.
72+
Without this option, this script calls antsAI to search for a good initial moving
73+
transform.
74+
-s: Image file suffix Any of the standard ITK IO formats e.g. nrrd, nii.gz (default), mhd
75+
-u: Use random seeding Use random number generated from system clock in Atropos (default = 1)
76+
-k: Keep temporary files Keep brain extraction/segmentation warps, etc (default = false).
77+
-q: Use floating point precision Use antsRegistration with floating point precision.
7578
7679
-z: Test / debug mode If > 0, runs a faster version of the script. Only for debugging, results will not be good.
7780
@@ -201,11 +204,14 @@ N4_BSPLINE_PARAMS="[ 200 ]"
201204

202205
USE_FLOAT_PRECISION=0
203206

207+
# Intial affine supplied on command line
208+
USER_INITIAL_AFFINE=""
209+
204210
if [[ $# -lt 3 ]] ; then
205211
Usage >&2
206212
exit 1
207213
else
208-
while getopts "a:c:d:e:f:h:k:m:o:q:s:u:z:" OPT
214+
while getopts "a:c:d:e:f:h:k:m:o:q:r:s:u:z:" OPT
209215
do
210216
case $OPT in
211217
d) #dimensions
@@ -225,11 +231,11 @@ else
225231
;;
226232
c) #k-means segmentation params
227233
# Check conventional ANTs vector designation (i.e., 'x')
228-
kmeansParamsArr=(${OPTARG//x/ })
234+
kmeansParamsArr=(${OPTARG//x/ })
229235
if [[ ${#kmeansParamsArr[@]} -ne 4 ]];
230236
then
231237
# Check alternative form
232-
kmeansParamsArr=(${OPTARG//,/ })
238+
kmeansParamsArr=(${OPTARG//,/ })
233239
if [[ ${#kmeansParamsArr[@]} -ne 4 ]];
234240
then
235241
echo "ERROR: unrecognized kmeans option (-c)."
@@ -239,7 +245,7 @@ else
239245
ATROPOS_NUM_CLASSES=${kmeansParamsArr[0]}
240246
ATROPOS_BRAIN_EXTRACTION_INITIALIZATION="kmeans[ ${ATROPOS_NUM_CLASSES} ]"
241247
ATROPOS_CSF_CLASS_LABEL=${kmeansParamsArr[1]}
242-
ATROPOS_GM_CLASS_LABEL=${kmeansParamsArr[2]}
248+
ATROPOS_GM_CLASS_LABEL=${kmeansParamsArr[2]}
243249
ATROPOS_WM_CLASS_LABEL=${kmeansParamsArr[3]}
244250
;;
245251
k) #keep tmp images
@@ -259,6 +265,9 @@ else
259265
;;
260266
q)
261267
USE_FLOAT_PRECISION=$OPTARG
268+
;;
269+
r)
270+
USER_INITIAL_AFFINE=$OPTARG
262271
;;
263272
s) #output suffix
264273
OUTPUT_SUFFIX=$OPTARG
@@ -292,7 +301,7 @@ if [[ -z "$ATROPOS_SEGMENTATION_MRF" ]];
292301
fi
293302
fi
294303

295-
echo "
304+
echo "
296305
Will run Atropos segmentation with K=${ATROPOS_NUM_CLASSES}. Classes labeled in order of mean intensity. Assuming CSF=${ATROPOS_CSF_CLASS_LABEL}, GM=${ATROPOS_GM_CLASS_LABEL}, WM=${ATROPOS_WM_CLASS_LABEL}
297306
"
298307

@@ -423,7 +432,7 @@ if [[ ! -f ${EXTRACTION_MASK} || ! -f ${EXTRACTION_WM} ]];
423432
## check if output was produced
424433
if [[ ! -f ${N4_CORRECTED_IMAGES[0]} ]];
425434
then
426-
echo "Expected output was not produce. The N4 corrected image doesn't exist:"
435+
echo "Expected output was not produced. The N4 corrected image doesn't exist:"
427436
echo " ${N4_CORRECTED_IMAGES[0]}"
428437
exit 1
429438
fi
@@ -454,29 +463,33 @@ if [[ ! -f ${EXTRACTION_MASK} || ! -f ${EXTRACTION_WM} ]];
454463
echo
455464

456465
## Step 1 ##
457-
logCmd ${ANTSPATH}/ResampleImageBySpacing ${DIMENSION} ${EXTRACTION_TEMPLATE} ${EXTRACTION_INITIAL_AFFINE_FIXED} 4 4 4 1
458-
logCmd ${ANTSPATH}/ResampleImageBySpacing ${DIMENSION} ${N4_CORRECTED_IMAGES[0]} ${EXTRACTION_INITIAL_AFFINE_MOVING} 4 4 4 1
459-
460466
logCmd ${ANTSPATH}/ImageMath ${DIMENSION} ${EXTRACTION_LAPLACIAN} Laplacian ${N4_CORRECTED_IMAGES[0]} 1.5 1
461467
logCmd ${ANTSPATH}/ImageMath ${DIMENSION} ${EXTRACTION_TEMPLATE_LAPLACIAN} Laplacian ${EXTRACTION_TEMPLATE} 1.5 1
462468

463-
# exe_initial_align="${ANTSPATH}/antsAffineInitializer ${DIMENSION} ${EXTRACTION_INITIAL_AFFINE_FIXED} ${EXTRACTION_INITIAL_AFFINE_MOVING} ${EXTRACTION_INITIAL_AFFINE} 15 0.1 0 10"
464-
exe_initial_align="${ANTSPATH}/antsAI -d ${DIMENSION} -v 1"
465-
exe_initial_align="${exe_initial_align} -m Mattes[ ${EXTRACTION_INITIAL_AFFINE_FIXED},${EXTRACTION_INITIAL_AFFINE_MOVING},32,Regular,0.2 ]"
466-
exe_initial_align="${exe_initial_align} -t Affine[ 0.1 ]"
467-
exe_initial_align="${exe_initial_align} -s [ 20,0.12 ]"
468-
exe_initial_align="${exe_initial_align} -g [ 40,0x40x40 ]"
469-
exe_initial_align="${exe_initial_align} -p 0"
470-
exe_initial_align="${exe_initial_align} -c 10"
471-
exe_initial_align="${exe_initial_align} -o ${EXTRACTION_INITIAL_AFFINE}"
472-
473-
if [[ -f ${EXTRACTION_REGISTRATION_MASK} ]];
469+
if [[ ! -f "${USER_INITIAL_AFFINE}" ]]
474470
then
475-
# exe_initial_align="${exe_initial_align} ${EXTRACTION_REGISTRATION_MASK}"
476-
exe_initial_align="${exe_initial_align} -x ${EXTRACTION_REGISTRATION_MASK}"
477-
fi
478471

479-
logCmd $exe_initial_align
472+
logCmd ${ANTSPATH}/ResampleImageBySpacing ${DIMENSION} ${EXTRACTION_TEMPLATE} ${EXTRACTION_INITIAL_AFFINE_FIXED} 4 4 4 1
473+
logCmd ${ANTSPATH}/ResampleImageBySpacing ${DIMENSION} ${N4_CORRECTED_IMAGES[0]} ${EXTRACTION_INITIAL_AFFINE_MOVING} 4 4 4 1
474+
475+
exe_initial_align="${ANTSPATH}/antsAI -d ${DIMENSION} -v 1"
476+
exe_initial_align="${exe_initial_align} -m Mattes[ ${EXTRACTION_INITIAL_AFFINE_FIXED},${EXTRACTION_INITIAL_AFFINE_MOVING},32,Regular,0.2 ]"
477+
exe_initial_align="${exe_initial_align} -t Affine[ 0.1 ]"
478+
exe_initial_align="${exe_initial_align} -s [ 20,0.12 ]"
479+
exe_initial_align="${exe_initial_align} -g [ 40,0x40x40 ]"
480+
exe_initial_align="${exe_initial_align} -p 0"
481+
exe_initial_align="${exe_initial_align} -c 10"
482+
exe_initial_align="${exe_initial_align} -o ${EXTRACTION_INITIAL_AFFINE}"
483+
484+
if [[ -f ${EXTRACTION_REGISTRATION_MASK} ]];
485+
then
486+
exe_initial_align="${exe_initial_align} -x ${EXTRACTION_REGISTRATION_MASK}"
487+
fi
488+
489+
logCmd $exe_initial_align
490+
else
491+
${ANTSPATH}/antsApplyTransforms -d ${DIMENSION} -t ${USER_INITIAL_AFFINE} -o Linear[ ${EXTRACTION_INITIAL_AFFINE}, 0 ]
492+
fi
480493

481494
basecall="${ANTS} -d ${DIMENSION} -u 1 -w [ 0.025,0.975 ] -o ${EXTRACTION_WARP_OUTPUT_PREFIX} -r ${EXTRACTION_INITIAL_AFFINE} -z 1 --float ${USE_FLOAT_PRECISION} --verbose 1"
482495
if [[ -f ${EXTRACTION_REGISTRATION_MASK} ]];
@@ -507,8 +520,8 @@ if [[ ! -f ${EXTRACTION_MASK} || ! -f ${EXTRACTION_WM} ]];
507520
fi
508521

509522
fi
510-
511-
if [[ ! -f ${EXTRACTION_SEGMENTATION} ]];
523+
524+
if [[ ! -f ${EXTRACTION_SEGMENTATION} ]];
512525
then
513526

514527
## Step 2 ##
@@ -530,8 +543,8 @@ if [[ ! -f ${EXTRACTION_MASK} || ! -f ${EXTRACTION_WM} ]];
530543

531544
exe_brain_extraction_3="${ATROPOS} -d ${DIMENSION} -o ${EXTRACTION_SEGMENTATION} ${ATROPOS_ANATOMICAL_IMAGES_COMMAND_LINE} -x ${EXTRACTION_MASK} -i ${ATROPOS_BRAIN_EXTRACTION_INITIALIZATION} -c ${ATROPOS_BRAIN_EXTRACTION_CONVERGENCE} -m ${ATROPOS_BRAIN_EXTRACTION_MRF} -k ${ATROPOS_BRAIN_EXTRACTION_LIKELIHOOD} -r ${USE_RANDOM_SEEDING} --verbose 1"
532545
logCmd $exe_brain_extraction_3
533-
fi
534-
546+
fi
547+
535548
# Pad image here to avoid errors from dilating into the edge of the image
536549
padVoxels=10
537550

@@ -541,7 +554,7 @@ if [[ ! -f ${EXTRACTION_MASK} || ! -f ${EXTRACTION_WM} ]];
541554
logCmd ${ANTSPATH}/ThresholdImage ${DIMENSION} ${EXTRACTION_SEGMENTATION} ${EXTRACTION_WM} ${ATROPOS_WM_CLASS_LABEL} ${ATROPOS_WM_CLASS_LABEL} 1 0
542555
logCmd ${ANTSPATH}/ThresholdImage ${DIMENSION} ${EXTRACTION_SEGMENTATION} ${EXTRACTION_GM} ${ATROPOS_GM_CLASS_LABEL} ${ATROPOS_GM_CLASS_LABEL} 1 0
543556
logCmd ${ANTSPATH}/ThresholdImage ${DIMENSION} ${EXTRACTION_SEGMENTATION} ${EXTRACTION_CSF} ${ATROPOS_CSF_CLASS_LABEL} ${ATROPOS_CSF_CLASS_LABEL} 1 0
544-
557+
545558
logCmd ${ANTSPATH}/ImageMath ${DIMENSION} ${EXTRACTION_WM} GetLargestComponent ${EXTRACTION_WM}
546559
logCmd ${ANTSPATH}/ImageMath ${DIMENSION} ${EXTRACTION_GM} GetLargestComponent ${EXTRACTION_GM}
547560

@@ -573,13 +586,13 @@ if [[ ! -f ${EXTRACTION_MASK} || ! -f ${EXTRACTION_WM} ]];
573586
logCmd ${ANTSPATH}/ImageMath ${DIMENSION} ${img} PadImage ${img} -$padVoxels
574587
done
575588

576-
589+
577590
logCmd ${ANTSPATH}/MultiplyImages ${DIMENSION} ${N4_CORRECTED_IMAGES[0]} ${EXTRACTION_MASK} ${EXTRACTION_BRAIN}
578591

579592
# Copy header information from original image into output
580593
logCmd ${ANTSPATH}/CopyImageHeaderInformation ${ANATOMICAL_IMAGES[0]} ${EXTRACTION_BRAIN} ${EXTRACTION_BRAIN} 1 1 1 0
581594
logCmd ${ANTSPATH}/CopyImageHeaderInformation ${ANATOMICAL_IMAGES[0]} ${EXTRACTION_MASK} ${EXTRACTION_MASK} 1 1 1 0
582-
595+
583596

584597
if [[ ! -f ${EXTRACTION_MASK} ]];
585598
then

0 commit comments

Comments
 (0)