Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Denoise all anatomical images, but outside of loop #1373

Merged
merged 2 commits into from
Jun 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions Scripts/antsAtroposN4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ for (( i = 1; i <= $ATROPOS_SEGMENTATION_NUMBER_OF_CLASSES; i++ ))
NUMBER_OF_PRIOR_IMAGES=${#PRIOR_IMAGE_FILENAMES[*]}

INITIALIZE_WITH_KMEANS=0
if [[ ${NUMBER_OF_PRIOR_IMAGES} -eq 0 ]];
if [[ -z "${ATROPOS_SEGMENTATION_PRIORS}" ]];
then
echo "Initializing with kmeans segmentation."
INITIALIZE_WITH_KMEANS=1
Expand Down Expand Up @@ -391,7 +391,8 @@ ATROPOS_SEGMENTATION_POSTERIORS=${ATROPOS_SEGMENTATION_OUTPUT}Posteriors%${FORMA
################################################################################
#
# Preprocess anatomical images
# 1. Denoise input images (if requested)
# 1. Truncate input intensity (needed for N4)
# 1. Denoise image (if requested)
#
################################################################################

Expand All @@ -406,6 +407,20 @@ if [[ ${DENOISE_ANATOMICAL_IMAGES} -ne 0 ]];
fi
fi

# Anatomical images that have been truncated, and optionally denoised
SEGMENTATION_PREPROCESSED_IMAGES=()

for (( j = 0; j < ${#ANATOMICAL_IMAGES[@]}; j++ ))
do
SEGMENTATION_PREPROCESSED_IMAGES=( ${SEGMENTATION_PREPROCESSED_IMAGES[@]} ${ATROPOS_SEGMENTATION_OUTPUT}PreprocessedAnatomical${j}.${OUTPUT_SUFFIX} )
# Truncate on the whole head to get outliers over the whole volume, without losing contrast in the brain
logCmd ${ANTSPATH}/ImageMath ${DIMENSION} ${SEGMENTATION_PREPROCESSED_IMAGES[$j]} TruncateImageIntensity ${ANATOMICAL_IMAGES[$j]} 0 0.995 256
if [[ ${DENOISE_ANATOMICAL_IMAGES} -ne 0 ]];
then
logCmd ${ANTSPATH}/DenoiseImage -d ${DIMENSION} -i ${SEGMENTATION_PREPROCESSED_IMAGES[$j]} -o ${SEGMENTATION_PREPROCESSED_IMAGES[$j]} --verbose 1
fi
done

################################################################################
#
# Segmentation
Expand Down Expand Up @@ -448,22 +463,10 @@ POSTERIOR_PROBABILITY_CONVERGED=0
for (( i = 0; i < ${N4_ATROPOS_NUMBER_OF_ITERATIONS}; i++ ))
do
SEGMENTATION_N4_IMAGES=()
for(( j = 0; j < ${#ANATOMICAL_IMAGES[@]}; j++ ))
for (( j = 0; j < ${#ANATOMICAL_IMAGES[@]}; j++ ))
do
SEGMENTATION_N4_IMAGES=( ${SEGMENTATION_N4_IMAGES[@]} ${ATROPOS_SEGMENTATION_OUTPUT}${j}N4.${OUTPUT_SUFFIX} )
if [[ $j == 0 ]];
then
# BA edit - forcing the image to copy physical space due to ITK bug images do not occupy same space
# Truncate on the whole image, not just within the brain - avoids losing brain contrast
logCmd ${ANTSPATH}/ImageMath ${DIMENSION} ${SEGMENTATION_N4_IMAGES[$j]} TruncateImageIntensity ${ANATOMICAL_IMAGES[$j]} 0 0.995 256
if [[ ${DENOISE_ANATOMICAL_IMAGES} -ne 0 ]];
then
logCmd ${ANTSPATH}/DenoiseImage -d ${DIMENSION} -i ${SEGMENTATION_N4_IMAGES[$j]} -o ${SEGMENTATION_N4_IMAGES[$j]} --verbose 1
fi
else
cp ${ANATOMICAL_IMAGES[$j]} ${SEGMENTATION_N4_IMAGES[$j]}
fi
exe_n4_correction="${N4} -d ${DIMENSION} -i ${SEGMENTATION_N4_IMAGES[$j]} -x ${ATROPOS_SEGMENTATION_MASK} -s ${N4_SHRINK_FACTOR} -c ${N4_CONVERGENCE} -b ${N4_BSPLINE_PARAMS} -o ${SEGMENTATION_N4_IMAGES[$j]} --verbose 1"
exe_n4_correction="${N4} -d ${DIMENSION} -i ${SEGMENTATION_PREPROCESSED_IMAGES[$j]} -x ${ATROPOS_SEGMENTATION_MASK} -s ${N4_SHRINK_FACTOR} -c ${N4_CONVERGENCE} -b ${N4_BSPLINE_PARAMS} -o ${SEGMENTATION_N4_IMAGES[$j]} --verbose 1"
if [[ -f ${SEGMENTATION_WEIGHT_MASK} ]];
then
exe_n4_correction="${exe_n4_correction} -w ${SEGMENTATION_WEIGHT_MASK}"
Expand Down Expand Up @@ -586,7 +589,7 @@ for (( i = 0; i < ${N4_ATROPOS_NUMBER_OF_ITERATIONS}; i++ ))

done

TMP_FILES=( $SEGMENTATION_WEIGHT_MASK ${POSTERIOR_IMAGE_FILENAMES_PREVIOUS_ITERATION[@]} ${SEGMENTATION_PREVIOUS_ITERATION} )
TMP_FILES=( $SEGMENTATION_WEIGHT_MASK ${POSTERIOR_IMAGE_FILENAMES_PREVIOUS_ITERATION[@]} ${SEGMENTATION_PREVIOUS_ITERATION} ${SEGMENTATION_PREPROCESSED_IMAGES[@]} )

if [[ $KEEP_TMP_IMAGES -eq 0 ]];
then
Expand Down