Visualization of MRI data in R

[This article was first published on Alexej's blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Lately I was getting a little bored with genomic data (and then TCGA2STAT started to give me a segfault on my university’s high performance computing facility too :stuck_out_tongue:). So I decided to analyze some brain imaging data that I had lying around instead. The first step is to do some visual data exploration. In this blog post I present some functions which I was able to find for MRI visualization in R, and which I found to be very useful. All functions presented below presuppose an image in the NIfTI data format as input, and are very user-friendly.

Example MRI data

A T1 image obtained from http://www.jannin.org/mritemplate/ (this one to be precise) is used for example purposes in what follows. We can read it into R using the library oro.nifti.

<span class="n">library</span><span class="p">(</span><span class="n">oro.nifti</span><span class="p">)</span><span class="w">
</span><span class="n">img</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">readNIfTI</span><span class="p">(</span><span class="s2">"Template-T1-U8-RALPFH-BR.nii.gz"</span><span class="p">,</span><span class="w"> </span><span class="n">reorient</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w">
</span><span class="nf">class</span><span class="p">(</span><span class="n">img</span><span class="p">)</span><span class="w">
</span><span class="c1">## [1] "nifti"</span><span class="w">
</span><span class="c1">## attr(,"package")</span><span class="w">
</span><span class="c1">## [1] "oro.nifti"</span><span class="w">
</span><span class="nf">dim</span><span class="p">(</span><span class="n">img</span><span class="p">)</span><span class="w">
</span><span class="c1">## [1] 182 512 512</span><span class="w">
</span>

Visualize brain slices with image

The library oro.nifti provides a version of the generic function image. It allows to plot slices of the brain. The following plots the 225th axial slice of img.

<span class="n">image</span><span class="p">(</span><span class="n">img</span><span class="p">,</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">225</span><span class="p">,</span><span class="w"> </span><span class="n">plot.type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"single"</span><span class="p">)</span><span class="w">
</span>

plot of chunk image

It can also be used to draw all available slices in a single plot next to each other (but that turns out not very helpful in this case).

<span class="n">image</span><span class="p">(</span><span class="n">img</span><span class="p">)</span><span class="w">
</span>

plot of chunk image_all

Display an orthographic projection with orthographic

The function orthographic, also from the library oro.nifti, gives an excellent overview of the 3D structure of the brain in two dimensions.

<span class="n">orthographic</span><span class="p">(</span><span class="n">img</span><span class="p">)</span><span class="w">
</span>

plot of chunk orthographic

In order to indicate or emphasize a certain region of the brain, orthographic can be used to display a mask on top of the original image. For example, the following code chunk removes the skull from the MRI data (you need the FSL software and the fslr R package for this), and then plots the extracted brain as a mask on top of the original image.

<span class="c1"># skull stripping using FSL's Brain Extraction Tool (BET)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">fslr</span><span class="p">)</span><span class="w">
</span><span class="n">img_bet</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">fslbet</span><span class="p">(</span><span class="n">infile</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">img</span><span class="p">,</span><span class="w"> </span><span class="n">retimg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">

</span><span class="c1"># plotting extracted brain as a mask on top of original data</span><span class="w">
</span><span class="n">mask</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">img</span><span class="w">
</span><span class="n">in_mask</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">img_bet</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">0</span><span class="w">
</span><span class="n">mask</span><span class="p">[</span><span class="n">in_mask</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">1</span><span class="w">
</span><span class="n">mask</span><span class="p">[</span><span class="o">!</span><span class="n">in_mask</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="kc">NA</span><span class="w">
</span><span class="n">orthographic</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">img</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mask</span><span class="p">,</span><span class="w"> </span><span class="n">col.y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"lightblue"</span><span class="p">)</span><span class="w">
</span>

plot of chunk orthographic_mask

(Notice that the extracted brain does not fit the original image quite perfectly, but I will not pursue further improvements to the skull stripping at this point, in order to concentrate on visualization alone.)

Polish orthographic displays with ortho2

The R package neurobase provides the function ortho2 as an improved version of orthographic. Here we use it to overlay the brain as a semi-transparent layer on top of the original image.

<span class="n">library</span><span class="p">(</span><span class="n">scales</span><span class="p">)</span><span class="w">
</span><span class="n">ortho2</span><span class="p">(</span><span class="n">img</span><span class="p">,</span><span class="w"> </span><span class="n">mask</span><span class="p">,</span><span class="w"> </span><span class="n">col.y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">alpha</span><span class="p">(</span><span class="s2">"red"</span><span class="p">,</span><span class="w"> </span><span class="m">0.3</span><span class="p">))</span><span class="w">
</span>

plot of chunk ortho2

Show two orthographic displays side by side with double_ortho

The function double_ortho can be used to display two orthographic brain images of the same dimensions next to each other. As a quick demonstration we can look at the original image and the extracted brain side by side.

<span class="n">brain</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">img</span><span class="w">
</span><span class="n">brain</span><span class="p">[</span><span class="o">!</span><span class="n">in_mask</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="kc">NA</span><span class="w">
</span><span class="n">double_ortho</span><span class="p">(</span><span class="n">img</span><span class="p">,</span><span class="w"> </span><span class="n">brain</span><span class="p">)</span><span class="w">
</span>

plot of chunk double_ortho

Beyond MRI

Of course the same functions can be repurposed for other types of imaging data. For example I can look at CT images of the lung, where the data is taken from a current kaggle.com competition.

<span class="n">library</span><span class="p">(</span><span class="n">oro.dicom</span><span class="p">)</span><span class="w">
</span><span class="c1"># generate a NIfTI image from a collection of DICOMs</span><span class="w">
</span><span class="n">all_slices</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">readDICOM</span><span class="p">(</span><span class="s2">"./00cba091fa4ad62cc3200a657aeb957e/"</span><span class="p">)</span><span class="w">
</span><span class="n">nii</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dicom2nifti</span><span class="p">(</span><span class="n">all_slices</span><span class="p">)</span><span class="w">
</span>

An oro.nifti::image plot of the lung:

<span class="n">image</span><span class="p">(</span><span class="n">nii</span><span class="p">,</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">100</span><span class="p">,</span><span class="w"> </span><span class="n">plot.type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"single"</span><span class="p">)</span><span class="w">
</span>

plot of chunk lung_image

An oro.nifti::orthographic plot of the lung:

<span class="n">orthographic</span><span class="p">(</span><span class="n">nii</span><span class="p">,</span><span class="w"> </span><span class="n">xyz</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">200</span><span class="p">,</span><span class="w"> </span><span class="m">220</span><span class="p">,</span><span class="w"> </span><span class="m">100</span><span class="p">))</span><span class="w">
</span>

plot of chunk lung_orthographic

To leave a comment for the author, please follow the link and comment on their blog: Alexej's blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)