--- title: How to Convert a Brain fMRI to a 3D Model date: 2019-08-23 --- # How to Convert a Brain fMRI to a 3D Model AUTHOR'S NOTE: I just want to start this out by saying I do not know what I am doing here. I am not an expert, nothing in this blogpost should be construed as medical advice. I just wanted to see what kind of pretty pictures I could get out of an fMRI data file. So this week I flew out to Stanford to participate in a study that involved a fMRI of my brain while I was doing some things. I asked for (and recieved) a data file from the fMRI so I could play with it and possibly 3D print it. This blogpost is the record of my journey through various software to get a fully usable 3D model out of the fMRI data file. ## The Data File I was given [christine_brain.nii.gz][firstniifile] by the researcher who was operating the fMRI. I looked around for some software to convert it to a 3D model and [/r/3dprinting][r3dprinting] suggested the use of [FreeSurfer][freesurfer] to generate a 3D model. I downloaded and installed the software then started to look for something I could do in the meantime, as this was going to take something on the order of 8 hours to process. ### An Animated GIF I started looking for the file format on the internet by googling "nii.gz brain image" and I stumbled across a program called [gif\_your\_nifti][gyn]. It looked to be mostly pure python so I created a virtualenv and installed it in there: ``` $ git clone https://github.com/miykael/gif_your_nifti $ cd gif_your_nifti $ virtualenv -p python3 env $ source env/bin/activate (env) $ pip3 install -r requirements.txt (env) $ python3 setup.py install ``` Then I ran it with the following settings to get [this first result (30 MB)][firstgif]. ``` (env) $ gif_your_nifti christine_brain.nii.gz --mode pseudocolor --cmap plasma ``` It looked weird though, that's because the fMRI scanner I used has a different rotation to what's considered "normal". The gif\_your\_nifti repo mentioned a program called `fslreorient2std` to reorient the fMRI image, so I set out to install and run it. ### FSL After some googling, I found [FSL's website][fsl] which included an installer script and required registration. 37 gigabytes of downloads and data later, I had the entire FSL suite installed to a server of mine and ran the conversion command: ``` $ fslreorient2std christine_brain.nii.gz christine_brain_reoriented.nii.gz ``` This produced a slightly smaller [reoriented file][secondniifile] I reran gif\_your\_nifti on this reoriented file and got [this result (30 MB)][secondgif] which looked a _lot_ better. ### FreeSurfer By this time I had gotten back home and [FreeSurfer][freesurfer] was done installing, so I registered for it (god bless the institution of None) and put its license key in the place it expected. I copied the reoriented data file to my Mac and then set up a `SUBJECTS_DIR` and had it start running the numbers and extracting the brain surfaces: ``` $ cd ~/tmp $ mkdir -p brain/subjects $ cd brain $ export SUBJECTS_DIR=$(pwd)/subjects $ recon-all -i /path/to/christine_brain_reoriented.nii.gz -s christine -all ``` This step took 8 hours. Once I was done I had a bunch of data in `$SUBJECTS_DIR/christine`. I opened my shell to that folder and went into the `surf` subfolder: ``` $ mris_convert lh.pial lh.pial.stl $ mris_convert rh.pial rh.pial.stl ``` Now I had standard stl files that I could stick into [Blender][blender]. ### Blender Importing the stl files was really easy. I clicked on File, then Import, then Stl. After guiding the browser to the subjects directory and finding the STL files, I got a view that looked something like this:
— Cadey Ratio 🌐 (@theprincessxena) August 22, 2019
— Cadey Ratio 🌐 (@theprincessxena) August 23, 2019