Brain Segmentation

Once you have a brain template for your population and it has the proper priors you’re ready to actually get to work on the entire population. You’ll be able to get cortical thickness, segmentation, and warp images from what follows (and much more).
Here’s the script I use…
#!/bin/bash

#SBATCH --time=20:00:00
#SBATCH --ntasks=1
#SBATCH --nodes=1
#SBATCH --mem-per-cpu=8192M
#SBATCH -o /fslhome/ShDynasty/logfiles/dataset/output_antsCT_0.txt
#SBATCH -e /fslhome/ShDynasty/logfiles/dataset/error_antsCT_0.txt
#SBATCH -J "antsCT_0"
#SBATCH --mail-user=ShDynasty@gmail.com
#SBATCH --mail-type=BEGIN
#SBATCH --mail-type=END
#SBATCH --mail-type=FAIL

export PBS_NODEFILE=`/fslapps/fslutils/generate_pbs_nodefile`
export PBS_JOBID=$SLURM_JOB_ID
export PBS_O_WORKDIR="$SLURM_SUBMIT_DIR"
export PBS_QUEUE=batch
export OMP_NUM_THREADS=$SLURM_CPUS_ON_NODE

export ANTSPATH=/fslhome/ShDynasty/bin/antsbin/bin/
PATH=${ANTSPATH}:${PATH}

ROOT=/fslhome/ShDynasty/
DATASET=${ROOT}/compute/dataset/
TEMPLATE=${DATASET}/template/
BRAINMASK=${TEMPLATE}T_template0_BrainCerebellumExtractionMask.nii.gz
LABS=${TEMPLATE}Labels/antsjlf_6Labels.nii.gz

IFS=$'\n'
array=( $(find ${DATASET}/*/ -type d -name t1) )
for i in 0;
    do
    SUB=$(dirname ${array[$i]})
    OUT=${SUB}/t1/ct/
    mkdir ${OUT}
    
    ${ANTSPATH}antsCorticalThickness.sh -d 3 \
      -a ${SUB}/t1/n4_resliced.nii.gz \
      -e ${TEMPLATE}T_template0.nii.gz \
      -m ${TEMPLATE}T_template0_BrainCerebellumProbabilityMask.nii.gz \
      -f ${TEMPLATE}T_template0_BrainCerebellumExtractionMask.nii.gz \
      -p ${TEMPLATE}Priors/prior%d.nii.gz \
      -t ${TEMPLATE}T_template0_BrainCerebellum.nii.gz \
      -k 1 \
      -q 1 \
      -o ${OUT}/
    
    # Create white matter, grey matter, csf, and whole brain warp images for analysis
    WARP=${SUB}/t1/warp/
    mkdir ${WARP}
    tmp=${WARP}tmpsegment.nii.gz
    
    # whole-brain
    ${ANTSPATH}/ImageMath 3 ${WARP}/brainWarp.nii.gz m ${OUT}SubjectToTemplate1Warp.nii.gz ${BRAINMASK}
    
    # white-matter
    ${ANTSPATH}/ThresholdImage 3 ${LABS} $tmp 3 3 1 0 
    ${ANTSPATH}/ImageMath 3 ${WARP}/wmWarp.nii.gz m ${OUT}SubjectToTemplate1Warp.nii.gz ${tmp}
    
    # grey-matter
    ${ANTSPATH}/ThresholdImage 3 ${LABS} $tmp 2 2 1 0
    ${ANTSPATH}/ImageMath 3 ${WARP}/gmWarp.nii.gz m ${OUT}SubjectToTemplate1Warp.nii.gz ${tmp}
    
    # csf
    ${ANTSPATH}/ThresholdImage 3 ${LABS} $tmp 1 1 1 0
    ${ANTSPATH}/ImageMath 3 ${WARP}/csfWarp.nii.gz m ${OUT}SubjectToTemplate1Warp.nii.gz ${tmp}
    
    rm ${tmp}
done
From this I obtain the following files:
ACTStage1Complete.txt
ACTStage2Complete.txt
ACTStage3Complete.txt
ACTStage4Complete.txt
ACTStage5Complete.txt
ACTStage6Complete.txt
BrainExtractionBrain.nii.gz
BrainExtractionCSF.nii.gz
BrainExtractionGM.nii.gz
BrainExtractionInitialAffineFixed.nii.gz
BrainExtractionInitialAffine.mat
BrainExtractionInitialAffineMoving.nii.gz
BrainExtractionLaplacian.nii.gz
BrainExtractionMask.nii.gz
BrainExtractionMaskResampled.nii.gz
BrainExtractionPrior0GenericAffine.mat
BrainExtractionPrior1InverseWarp.nii.gz
BrainExtractionPrior1Warp.nii.gz
BrainExtractionPriorWarped.nii.gz
BrainExtractionSegmentation.nii.gz
BrainExtractionTemplateLaplacian.nii.gz
BrainExtractionTmp.nii.gz
BrainExtractionWM.nii.gz
BrainNormalizedToTemplate.nii.gz
BrainSegmentation0N4.nii.gz
BrainSegmentation0N4Resampled.nii.gz
BrainSegmentationConvergence.txt
BrainSegmentationMaskDilated.nii.gz
BrainSegmentation.nii.gz
BrainSegmentationPosteriors1.nii.gz
BrainSegmentationPosteriors2.nii.gz
BrainSegmentationPosteriors3.nii.gz
BrainSegmentationPosteriors4.nii.gz
BrainSegmentationPosteriors5.nii.gz
BrainSegmentationPosteriors6.nii.gz
BrainSegmentationPosteriorsPreviousIteration1.nii.gz
BrainSegmentationPosteriorsPreviousIteration2.nii.gz
BrainSegmentationPosteriorsPreviousIteration3.nii.gz
BrainSegmentationPosteriorsPreviousIteration4.nii.gz
BrainSegmentationPosteriorsPreviousIteration5.nii.gz
BrainSegmentationPosteriorsPreviousIteration6.nii.gz
BrainSegmentationPreviousIteration.nii.gz
BrainSegmentationPrior0GenericAffine.mat
BrainSegmentationPrior1InverseWarp.nii.gz
BrainSegmentationPrior1Warp.nii.gz
BrainSegmentationPriorInverseWarped.nii.gz
BrainSegmentationPriorWarped1.nii.gz
BrainSegmentationPriorWarped2.nii.gz
BrainSegmentationPriorWarped3.nii.gz
BrainSegmentationPriorWarped4.nii.gz
BrainSegmentationPriorWarped5.nii.gz
BrainSegmentationPriorWarped6.nii.gz
BrainSegmentationPriorWarped.nii.gz
BrainSegmentationResampled.nii.gz
BrainSegmentationRGB.nii.gz
BrainSegmentationTiledMosaic.png
BrainSegmentationWeightMask.nii.gz
brainvols.csv
CorticalThicknessHotResampled.nii.gz
CorticalThicknessHotRGB.nii.gz
CorticalThicknessMask.nii.gz
CorticalThickness.nii.gz
CorticalThicknessNormalizedToTemplate.nii.gz
CorticalThicknessPosteriors2.nii.gz
CorticalThicknessPosteriors3.nii.gz
CorticalThicknessSegmentation.nii.gz
CorticalThicknessTiledMosaic.png
ExtractedBrain0N4.nii.gz
ExtractedTemplateBrain.nii.gz
ItkSnapColormap.txt
N4Corrected0.nii.gz
N4Truncated0.nii.gz
RegistrationTemplateBrainMask.nii.gz
SubjectToTemplate0GenericAffine.mat
SubjectToTemplate1Warp.nii.gz
SubjectToTemplateInverseWarped.nii.gz
SubjectToTemplateLogJacobian.nii.gz
SubjectToTemplateWarped.nii.gz
TemplateToSubject0Warp.nii.gz
TemplateToSubject1GenericAffine.mat

brainWarp.nii.gz
csfWarp.nii.gz
gmWarp.nii.gz
wmWarp.nii.gz
As you can see, there are a lot of files here. The antsCorticalThickness.sh command does most of the heavy lifting here and all but the last files are produced by it. This is all fairly well documented in ANTs so if there’s something you don’t understand here don’t be afraid to look at some of the resources that ANTs provides to explain its software.
There are basically two steps in this scripts:
  1. Run antsCorticalThickness.sh: This will perform a brain extraction, segmentation, map the subject space warp to template space, and provide cortical thickness measurements. Unless you’re know better, don’t touch anything that has ${TEMPLATE} as part of the argument. The rest of the commands can be understood as follows:
  • -a: this is your structural T1 image that you want processed.
  • -k: Keep all temporary files. (1 = TRUE). I mostly keep all these files because when I started off I had no idea what I was doing and it can be very difficult to figure out how you get an image from point A to point B.
  • -q: Quick registration for brain extraction (1 = TRUE). I’ve been told that this doesn’t have a significant impact on results, so I allow myself to be impatiant and use the quicker option.
  • -o: Output filenames’ prefix
  1. Segment SubjectToTemplate1Warp.nii.gz into grey matter, white matter, and CSF components.
  • The ThresholdImage command allows me to take a segmentation image of the template and extract the ROI I’m interested in. In the following example I just want to extract the white matter (which has a value of 3 in the segmentation image) and set it a value of 1.
    ${ANTSPATH}/ThresholdImage 3 \  # image dimensions
      ${LABS} \  # image in (the template segmentation image)
      $tmp \  # image out
      3 \  # thresh low 
      3 \  # thresh high 
      1 \  # inside value
      0 \  # outside value
  • The ImageMath command can do a ridiculous amount of things. In this instance I’m just multiplying the last two images in the following argument to isolate the white matter region of the SubjectToTemplate1Warp image.
    ${ANTSPATH}/ImageMath 3 \  # image dimensions
      ${WARP}/wmWarp.nii.gz \  # output file name
      m \  # the type of "image math" performed
      ${OUT}SubjectToTemplate1Warp.nii.gz \  
      ${tmp
The next thing to do is actually perform statistical analyses on the data, but for now just enjoy the mathematical beauty that all of this can produce.

Comments

Popular posts from this blog

Align and Bias Correct Your Brain!

Making a Brain Template

Getting R Ready for Neuroimaging