Align and Bias Correct Your Brain!

Once you’re files are ready to by preprocessed (Yes, there’s a word for the processing that takes place before the processing). They need to be aligned to a common image space.
Here’s my handy script:
#!/bin/bash

#SBATCH --time=30:00
#SBATCH --ntasks=1
#SBATCH --nodes=1
#SBATCH --mem-per-cpu=8192M
#SBATCH -o /fslhome/zach8769/logfiles/oasis_longitudinal/output_prepT1_0.txt
#SBATCH -e /fslhome/zach8769/logfiles/oasis_longitudinal/error_prepT1_0.txt
#SBATCH -J "prepT1_0"
#SBATCH --mail-user=GalileoHumpkins@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

ROOT=/fslhome/GalileoHumpkins/
DATASET=${ROOT}compute/oasis_longitudinal/
TEMPLATE=${ROOT}compute/templates/MICCAI2012-Multi-Atlas-Challenge-Data/
export ANTSPATH=/fslhome/zach8769/bin/antsbin/bin/
PATH=${ANTSPATH}:${PATH}

IFS=$'\n'
array=( $(find ${DATASET}/*/t1/*/ -type f -name t1.nii.gz) )
for i in 0;
  do
  SUB=$(dirname ${array[$i]})

  ${ANTSPATH}antsRegistrationSyNQuick.sh -d 3 \
    -f ${TEMPLATE}T_template0.nii.gz \
    -m ${SUB}/t1.nii.gz \
    -o ${SUB}/rigid_ \
    -t r

  N4BiasFieldCorrection -d 3 \
    -i ${SUB}/rigid_Warped.nii.gz \
    -o ${SUB}/n4.nii.gz \
    -s 8 -b [200] -c [50x50x50x50,0.000001]
    
  N4BiasFieldCorrection -d 3 \
    -i ${SUB}/n4.nii.gz \
    -o ${SUB}/n4.nii.gz \
    -s 4 -b [200] -c [50x50x50x50,0.000001]

  N4BiasFieldCorrection -d 3 \
    -i ${SUB}/n4.nii.gz \
    -o ${SUB}/n4.nii.gz \
    -s 2 -b [200] -c [50x50x50x50,0.000001]

  ResampleImage 3 \
    ${SUB}/n4.nii.gz \
    ${SUB}/n4_resliced.nii.gz 1x1x1

  rm ${SUB}/rigid_0GenericAffine.mat
  rm ${SUB}/rigid_InverseWarped.nii.gz
done
Th N4BiasFieldCorrection command accounts for different intensities in the scan that result from bias in the signal as a result of their location in 3 dimensions (there are a lot more details but they’re beyond the scope of this post). The ResampleImage command ensures that all images have the same voxel spacing. As a whole, this script ensures that image spacing and intensity is as comparable as possible before any further processes that compare anatomical differences are used.

After
Before

Comments

Popular posts from this blog

Making a Brain Template

Preparing for the MCAT