<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/visualization-of-mri-data-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.alexejgossmann.com//MRI_viz/"> Alexej's blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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 <a target="_blank" rel="nofollow" href="https://nifti.nimh.nih.gov/nifti-1/">NIfTI data format</a> as input, and are very user-friendly.</p>
<h3>Example MRI data</h3>
<p>A T1 image obtained from <a target="_blank" rel="nofollow" href="http://www.jannin.org/mritemplate/">http://www.jannin.org/mritemplate/</a> (<a target="_blank" rel="nofollow" href="http://www.jannin.org/mritemplate/data/Template-T1-U8-RALPFH-BR.nii.gz">this one</a> to be precise) is used for example purposes in what follows. We can read it into R using the library <code>oro.nifti</code>.</p>
<div>
<pre><code><span>library</span><span>(</span><span>oro.nifti</span><span>)</span><span>
</span><span>img</span><span> </span><span>&lt;-</span><span> </span><span>readNIfTI</span><span>(</span><span>&quot;Template-T1-U8-RALPFH-BR.nii.gz&quot;</span><span>,</span><span> </span><span>reorient</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>class</span><span>(</span><span>img</span><span>)</span><span>
</span><span>## [1] &quot;nifti&quot;
## attr(,&quot;package&quot;)
## [1] &quot;oro.nifti&quot;
</span><span>dim</span><span>(</span><span>img</span><span>)</span><span>
</span><span>## [1] 182 512 512
</span></code></pre>
</div>
<h3>Visualize brain slices with <em>image</em></h3>
<p>The library <code>oro.nifti</code> provides a version of the generic function <code>image</code>. It allows to plot slices of the brain. The following plots the 225th axial slice of <code>img</code>.</p>
<div>
<pre><code><span>image</span><span>(</span><span>img</span><span>,</span><span> </span><span>z</span><span> </span><span>=</span><span> </span><span>225</span><span>,</span><span> </span><span>plot.type</span><span> </span><span>=</span><span> </span><span>&quot;single&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk image" src="https://i2.wp.com/www.alexejgossmann.com//images/MRI_viz/image-1.png?w=456" data-recalc-dims="1"></p>
<p>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).</p>
<div>
<pre><code><span>image</span><span>(</span><span>img</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk image_all" src="https://i2.wp.com/www.alexejgossmann.com//images/MRI_viz/image_all-1.png?w=456" data-recalc-dims="1"></p>
<h3>Display an orthographic projection with <em>orthographic</em></h3>
<p>The function <code>orthographic</code>, also from the library <code>oro.nifti</code>, gives an excellent overview of the 3D structure of the brain in two dimensions.</p>
<div>
<pre><code><span>orthographic</span><span>(</span><span>img</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk orthographic" src="https://i1.wp.com/www.alexejgossmann.com//images/MRI_viz/orthographic-1.png?w=456" data-recalc-dims="1"></p>
<p>In order to indicate or emphasize a certain region of the brain, <code>orthographic</code> 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 <a target="_blank" rel="nofollow" href="https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/">FSL</a> software and the <a target="_blank" rel="nofollow" href="https://github.com/muschellij2/fslr">fslr</a> R package for this), and then plots the extracted brain as a mask on top of the original image.</p>
<div>
<pre><code><span># skull stripping using FSL's Brain Extraction Tool (BET)
</span><span>library</span><span>(</span><span>fslr</span><span>)</span><span>
</span><span>img_bet</span><span> </span><span>&lt;-</span><span> </span><span>fslbet</span><span>(</span><span>infile</span><span> </span><span>=</span><span> </span><span>img</span><span>,</span><span> </span><span>retimg</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span># plotting extracted brain as a mask on top of original data
</span><span>mask</span><span> </span><span>&lt;-</span><span> </span><span>img</span><span>
</span><span>in_mask</span><span> </span><span>&lt;-</span><span> </span><span>img_bet</span><span> </span><span>></span><span> </span><span>0</span><span>
</span><span>mask</span><span>[</span><span>in_mask</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>1</span><span>
</span><span>mask</span><span>[</span><span>!</span><span>in_mask</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>NA</span><span>
</span><span>orthographic</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>img</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>mask</span><span>,</span><span> </span><span>col.y</span><span> </span><span>=</span><span> </span><span>&quot;lightblue&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk orthographic_mask" src="https://i0.wp.com/www.alexejgossmann.com//images/MRI_viz/orthographic_mask-1.png?w=456" data-recalc-dims="1"></p>
<p>(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.)</p>
<h3>Polish orthographic displays with <em>ortho2</em></h3>
<p>The R package <code>neurobase</code> provides the function <code>ortho2</code> as an improved version of <code>orthographic</code>. Here we use it to overlay the brain as a semi-transparent layer on top of the original image.</p>
<div>
<pre><code><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>ortho2</span><span>(</span><span>img</span><span>,</span><span> </span><span>mask</span><span>,</span><span> </span><span>col.y</span><span> </span><span>=</span><span> </span><span>alpha</span><span>(</span><span>&quot;red&quot;</span><span>,</span><span> </span><span>0.3</span><span>))</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk ortho2" src="https://i2.wp.com/www.alexejgossmann.com//images/MRI_viz/ortho2-1.png?w=456" data-recalc-dims="1"></p>
<h3>Show two orthographic displays side by side with <em>double_ortho</em></h3>
<p>The function <code>double_ortho</code> 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.</p>
<div>
<pre><code><span>brain</span><span> </span><span>&lt;-</span><span> </span><span>img</span><span>
</span><span>brain</span><span>[</span><span>!</span><span>in_mask</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>NA</span><span>
</span><span>double_ortho</span><span>(</span><span>img</span><span>,</span><span> </span><span>brain</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk double_ortho" src="https://i0.wp.com/www.alexejgossmann.com//images/MRI_viz/double_ortho-1.png?w=456" data-recalc-dims="1"></p>
<h3>Beyond MRI</h3>
<p>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 target="_blank" rel="nofollow" href="https://www.kaggle.com/c/data-science-bowl-2017">a current kaggle.com competition</a>.</p>
<div>
<pre><code><span>library</span><span>(</span><span>oro.dicom</span><span>)</span><span>
</span><span># generate a NIfTI image from a collection of DICOMs
</span><span>all_slices</span><span> </span><span>&lt;-</span><span> </span><span>readDICOM</span><span>(</span><span>&quot;./00cba091fa4ad62cc3200a657aeb957e/&quot;</span><span>)</span><span>
</span><span>nii</span><span> </span><span>&lt;-</span><span> </span><span>dicom2nifti</span><span>(</span><span>all_slices</span><span>)</span><span>
</span></code></pre>
</div>
<p>An <code>oro.nifti::image</code> plot of the lung:</p>
<div>
<pre><code><span>image</span><span>(</span><span>nii</span><span>,</span><span> </span><span>z</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span> </span><span>plot.type</span><span> </span><span>=</span><span> </span><span>&quot;single&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk lung_image" src="https://i0.wp.com/www.alexejgossmann.com//images/MRI_viz/lung_image-1.png?w=456" data-recalc-dims="1"></p>
<p>An <code>oro.nifti::orthographic</code> plot of the lung:</p>
<div>
<pre><code><span>orthographic</span><span>(</span><span>nii</span><span>,</span><span> </span><span>xyz</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>200</span><span>,</span><span> </span><span>220</span><span>,</span><span> </span><span>100</span><span>))</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk lung_orthographic" src="https://i0.wp.com/www.alexejgossmann.com//images/MRI_viz/lung_orthographic-1.png?w=456" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.alexejgossmann.com//MRI_viz/"> Alexej's blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/I3jZnKI8ogs"></a></div>