April 10, 2016

What can we do in R?

  1. Read/Write Images
  2. Visualization of Images
  3. Inhomogeneity/Bias Field Correction
  4. Skull Stripping/Brain Extraction
  5. Image Registration
  6. Tissue-Class Segmentation
  7. Image operations
  8. Complex Modeling (yeah - it's R)

Data used from NITRC

Multi-modal dataset from HAMMER, (NIfTI conversion from ANALYZE).

            t1             t2             pd          flair 
   "T1.nii.gz"    "T2.nii.gz"    "PD.nii.gz" "FLAIR.nii.gz" 

Starting from Raw Data/DICOM

Introduction: Packages

Medical Imaging Task View

  • dti - adaptive smoothing and diffusion tensor tools

  • fmri - post-processing analysis: linear models and p-value smoothing
  • AnalyzeFMRI - fMRI analysis (last updated in 2013)
  • spm12r package calls out MATLAB using SPM

Introduction: Packages used Here

Medical Imaging Task View

  • oro.nifti: read/write data, the nifti object
  • fslr: process data (need FSL for most of the functionality)
  • ANTsR: process data (full toolbox)
  • extrantsr: makes ANTsR work with nifti objects

Basics: Read in the Files!

fslr: readnii uses oro.nifti::readNIfTI:

base_t1 = readnii(files["t1"])
  • like an array
  • ANTsR uses pointers (faster), but not as intuitive

Orthographic View


Orthographic Overlays

over_50 = mask_img(base_t1, base_t1 > 40); ortho2(base_t1, over_50)

Image Slices

image(base_t1, z = 55, plot.type = "single")

Slice Overlays

over_50[over_50 <= 0] = NA; over_50 = cal_img(over_50)
overlay(base_t1, over_50, z = 55, plot.type = "single")

Cropped Image Slices

Bias Field Correction


  • bias_correct from extrantsr package calls ANTsR::n4BiasFieldCorrection (N. J. Tustison et al. 2010)
  • EM-like, assumes bias is smooth over space, logs the data
n4_t1 = bias_correct(file = base_t1, correction = "N4", retimg = TRUE)

fslr: Uses method by Sled, Zijdenbos, and Evans (1998) (slow)

bc_t1 = fsl_biascorrect(file = base_t1)
FSLDIR='/usr/local/fsl/'; export FSLDIR; sh "${FSLDIR}/etc/fslconf/fsl.sh"; FSLOUTPUTTYPE=NIFTI_GZ; export FSLOUTPUTTYPE; $FSLDIR/bin/fast    -B --nopve --out="/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//Rtmpa4mKOM/file15e6f7f9ff9a7" "/private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/Rtmpa4mKOM/file15e6f60841581.nii.gz";

Bias Field Correction: Results

ratio = finite_img(n4_t1 / base_t1)
ortho2(n4_t1, ratio, col.y = alpha(hotmetal(), 0.75))

Skull Stripping: FSL BET

ss_t1 = fslbet(n4_t1, outfile = "SS_Image")
FSLDIR='/usr/local/fsl/'; export FSLDIR; sh "${FSLDIR}/etc/fslconf/fsl.sh"; FSLOUTPUTTYPE=NIFTI_GZ; export FSLOUTPUTTYPE; $FSLDIR/bin/bet2 "/private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/Rtmpa4mKOM/file15e6f2126f0db.nii.gz" "./SS_Image" 

Overlaying Skull Stripped mask

mask = ss_t1 > 0
ortho2(base_t1, y = mask, col.y = alpha("red", 0.5))

Visualization: Cropped Image Slices

cropped = dropEmptyImageDimensions(ss_t1)
image(cropped, z = floor(dim(cropped)[3]/2), plot.type = "single")

Visualization: 3-dimensions

  • rgl, misc3d (contour3d function)
  • brainR - put on a webpage with some controls

Rigid-Body Registration


  • antsRegistration - rigid/affine/non-linear diffeomorphic
  • extrantsr::registration - wraps antsRegistration to use nifti objects


  • flirt - linear/affine registration
  • fnirt - non-linear registration (need affine first)
  • fnirt_with_affine - wraps above 2

Rigid-Body Registration

  • registration from extrantsr is a general function to do linear/non-linear registration, using antsRegistration
ants_reg_flair = registration(
  filename = files["flair"], 
  template.file = n4_t1, 
  typeofTransform = "Rigid")

Rigid-Body Registration Results

Rigid-Body Registration Slice

Non-linear Registration

  • Registering the skull-stripped T1-weighted image to the MNI 152 brain image
  • Uses Symmetric Normalization (SyN) (B. B. Avants et al. 2008)
template.file = mni_fname(mm = "1", brain = TRUE)
ss_t1_to_mni = registration(
  filename = ss_t1, 
  template.file = template.file, 
  typeofTransform = "SyN", remove.warp = FALSE,
  outprefix = "temp")

Non-linear Registration Results

Tissue-Class Segmentation

ANTsR/extrantsr: uses Atropos (Brian B Avants et al. 2011)

  • ANTsR - atropos, extrantsr - otropos
tissue_seg = otropos(a = ss_t1,
  x = mask)

fslr: uses FAST (Zhang, Brady, and Smith 2001)

  • --nobias as an option does not do bias field correction
fast_t1 = fast(ss_t1, opts = "--nobias")
FSLDIR='/usr/local/fsl/'; export FSLDIR; sh "${FSLDIR}/etc/fslconf/fsl.sh"; FSLOUTPUTTYPE=NIFTI_GZ; export FSLOUTPUTTYPE; $FSLDIR/bin/fast   --nobias --out="/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//Rtmpa4mKOM/file15e6f60cbfee3" "/private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/Rtmpa4mKOM/file15e6f682913b1.nii.gz";

See alos spm12r_segment

ANTsR Tissue-Class Segmentation

fslr Tissue-Class Segmentation

Image operations


  • fslsmooth - Gaussian/box smoothing
  • fslerode/fsldilate - erosion/dilation
  • fslfill/fslfill2 - fill holes


  • spm_bwlabel - label connected components


  • smooth_image - Gaussian smoothing
  • oMath("ME")/oMath("MD") - erosion/dilation
  • oMath("FillHoles") - fill holes
  • oMath("GetLargestComponent") - find largest components


  • fsl_slicetimer - slice timing correction
  • ANTsR::preprocessfMRI
  • spm12r
    • slice timing correction
    • realignment - get motion correction parameters
    • co-registration
    • segmentation/normalization to a template

Intensity Normalization

  • WhiteStripe (Shinohara et al. 2014)
  • Whole brain z-scoring
  • Histogram matching
  • General standardization methods
  • RAVEL (talk to Taki)


  • Many methods are being developed for processing neuroimaging in R
  • Analysis tools are largely already in R
  • These are not standardized nor centralized
  • Develop something like BioConductor
    • standard data structures
    • standard data sets
    • NITRC isn't exactly what we want
  • GitHub and Neuroconductor

Interactive Visualization using papayar


Avants, B. B., C. L. Epstein, M. Grossman, and J. C. Gee. 2008. “Symmetric Diffeomorphic Image Registration with Cross-Correlation: Evaluating Automated Labeling of Elderly and Neurodegenerative Brain.” Medical Image Analysis, Special issue on the third international workshop on biomedical image registration - WBIR 2006, 12 (1): 26–41. doi:10.1016/j.media.2007.06.004.

Avants, Brian B, Nicholas J Tustison, Jue Wu, Philip A Cook, and James C Gee. 2011. “An Open Source Multivariate Framework for N-Tissue Segmentation with Evaluation on Public Data.” Neuroinformatics 9 (4). Springer: 381–400.

Shinohara, Russell T., Elizabeth M. Sweeney, Jeff Goldsmith, Navid Shiee, Farrah J. Mateen, Peter A. Calabresi, Samson Jarso, Dzung L. Pham, Daniel S. Reich, and Ciprian M. Crainiceanu. 2014. “Statistical Normalization Techniques for Magnetic Resonance Imaging.” NeuroImage: Clinical 6: 9–19. doi:10.1016/j.nicl.2014.08.008.

Sled, John G, Alex P Zijdenbos, and Alan C Evans. 1998. “A Nonparametric Method for Automatic Correction of Intensity Nonuniformity in MRI Data.” Medical Imaging, IEEE Transactions on 17 (1). IEEE: 87–97.

Tustison, Nicholas J., Brian B. Avants, Philip A. Cook, Yuanjie Zheng, Alexander Egan, Paul A. Yushkevich, and James C. Gee. 2010. “N4ITK: Improved N3 Bias Correction.” IEEE Transactions on Medical Imaging 29 (6): 1310–20. doi:10.1109/TMI.2010.2046908.

Zhang, Yongyue, Michael Brady, and Stephen Smith. 2001. “Segmentation of Brain MR Images Through a Hidden Markov Random Field Model and the Expectation-Maximization Algorithm.” Medical Imaging, IEEE Transactions on 20 (1). IEEE: 45–57.