Normalize intensity by z-scores to improve performance of convolutional neural networks

Hi everyone,
In order to preprocess some MRIs by normalizing them to have zero mean and unit variance, one may run the following in a bash terminal.

mapfile -t mu < <( mrstats -mask brainmask.nii.gz -output mean image.nii.gz )
mapfile -t sigma < <( mrstats -mask brainmask.nii.gz -output std image.nii.gz )
mrcalc image.nii.gz $mu -sub $sigma -div normalized_image.nii.gz

This isn’t a question per se; I emailed Prof. Tournier directly and he gave me the answer and suggested I post it on this forum for others to benefit. A big thank you to Prof. Tournier.

Just a quick note on the above:

My initial suggestion was the following, but for some reason this didn’t work on @pegger0709’s system. I’m listing here in case it works for others (it relies on reading the output of the mrstats call, captured via command substitution into an array variable):

val=( $(mrstats image.mif -mask brainmask.nii.gz -output mean -output std) )
mrcalc image.mif ${val[0]} -sub ${val[1]} -div normalised.mif

I prefer the above since it only requires a single mrstats call to get both the mean and std dev in one pass.

Otherwise, you should be able to do the two-call version using standard command substitution:

mu=$(mrstats image.mif -mask brainmask.nii.gz -output mean)
sigma=$(mrstats image.mif -mask brainmask.nii.gz -output std)
mrcalc image.mif $mu -sub $sigma -div normalised.mif

Ultimately, it all boils down to bash gymnastics - there’s many ways to skin this cat…