• Stars
    star
    389
  • Rank 110,500 (Top 3 %)
  • Language
    Shell
  • License
    BSD 3-Clause "New...
  • Created over 8 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

How to 3D print your brain from a T1 MRI image.

3D print your brain

GitHub issues GitHub pull-requests GitHub contributors GitHub Commits GitHub size

So, you want to 3D print your own brain? The following is a step by step guide that will tell you exactly how to do that. You can either run the steps by yourself or use the amazing 3Dprinting_brain.sh script, developed by Sofie Van Den Bossche, James Deraeve, Thibault Sanders and Robby De Pauw that is located in the script folder.

In short, the script will allow you to create a 3D model of your brain, all coming from a structural image (T1) like this:

Note: To create a 3D surface model of your brain we will use FreeSurfer, meshlab and FSL. Therefore you should make sure that those are already installed on your system.

Step 1 - Specify Variables

Let's first specify all necessary variables that we need for this to work:

# Main folder for the whole project
export EXPERIMENT_DIR=/users/mnotter/3dbrain

# Path to the FreeSurfer folder
export SUBJECTS_DIR=$EXPERIMENT_DIR/freesurfer

# Name of the subject
export subject=sub001

# Path to the structural image
export subjT1=$EXPERIMENT_DIR/${subject}/struct.nii.gz

Step 2 - Create Surface Model with FreeSurfer

Assuming that you have your structural image in NIfTI format, run the following code:

mkdir -p $SUBJECTS_DIR/${subject}/mri/orig
mri_convert ${subjT1} $SUBJECTS_DIR/${subject}/mri/orig/001.mgz
recon-all -subjid ${subject} -all -time -log logfile -nuintensitycor-3T -sd $SUBJECTS_DIR -parallel

Note: This step might take some time. Between 3-9h. By default it runs in parallel across 4 cores, this can be overridden by adding the flag -openmp N after -parallel, where N stands for the number of CPUs to use.

Step 3 - Create 3D Model of Cortical Areas

The following code will take the reconstructed surface model of both hemisphere's, concatenate them and save them under cortical.stl

mris_convert --combinesurfs $SUBJECTS_DIR/${subject}/surf/lh.pial $SUBJECTS_DIR/${subject}/surf/rh.pial \
             $SUBJECTS_DIR/cortical.stl

Now let's look at the data with meshlab. Therefore use the following code:

meshlab $SUBJECTS_DIR/cortical.stl

Note: Should you run into issues with the meshlab commands, check out this useful comment from smeisler.

You should see something like this:

Note: If you have the following display message, just accept with OK.

This version of your surface reconstruction is still rather rough. So lets smooth it abit. Therefore, go to

Filters
    > Smoothing, Fairing, and Deformation
        > ScaleDependent Laplacian Smooth

This should open up the following window:

Just set Smoothing steps to 100 and perc on under delta (abs and %) to 0.100. And finally press Apply. You should now have something that looks like this:

After this step, click on File and Export Mesh and save the whole thing without Binary encoding, i.e.:

Press OK and close meshlab again.

Step 4 - Extract the Subcortial Areas of Interest

Now, FreeSurfer creates a very nice 3D model of the surface. Which unfortunately it doesn't do for subcortical structures, cerebellum and brainstem. Assuming that you want to print those areas too, we have to create a 3D surface model of them first. One way to do this is to use FreeSurfer's segmentation file aseg.mgz.

# First, convert aseg.mgz into NIfTI format
mri_convert $SUBJECTS_DIR/${subject}/mri/aseg.mgz $SUBJECTS_DIR/subcortical.nii

# Second, binarize all Areas that you're not interested and inverse the binarization
mri_binarize --i $SUBJECTS_DIR/subcortical.nii \
             --match 2 3 24 31 41 42 63 72 77 51 52 13 12 43 50 4 11 26 58 49 10 17 18 53 54 44 5 80 14 15 30 62 \
             --inv \
             --o $SUBJECTS_DIR/bin.nii

# Third, multiply the original aseg.mgz file with the binarized files
fslmaths $SUBJECTS_DIR/subcortical.nii \
         -mul $SUBJECTS_DIR/bin.nii \
         $SUBJECTS_DIR/subcortical.nii.gz

Note: To figure out the value of the areas of no interest in the second step open aseg.mgz in the NIfTI viewer of your choice. With FreeSurfer it would be as follows: freeview -v $SUBJECTS_DIR/${subject}/mri/aseg.mgz -colormap lut

After this step you'll have a NIfTI file that only contains the areas you were interested in. It should look something like this:

Step 5 - Create 3D Model of Subcortical Areas

The next step is now to turn those subcortical regions into a 3D model.

# Copy original file to create a temporary file
cp $SUBJECTS_DIR/subcortical.nii.gz $SUBJECTS_DIR/subcortical_tmp.nii.gz

# Unzip this file
gunzip -f $SUBJECTS_DIR/subcortical_tmp.nii.gz

# Check all areas of interest for wholes and fill them out if necessary
for i in 7 8 16 28 46 47 60 251 252 253 254 255
do
    mri_pretess $SUBJECTS_DIR/subcortical_tmp.nii \
    $i \
    $SUBJECTS_DIR/${subject}/mri/norm.mgz \
    $SUBJECTS_DIR/subcortical_tmp.nii
done

# Binarize the whole volume
fslmaths $SUBJECTS_DIR/subcortical_tmp.nii -bin $SUBJECTS_DIR/subcortical_bin.nii

# Create a surface model of the binarized volume with mri_tessellate
mri_tessellate $SUBJECTS_DIR/subcortical_bin.nii.gz 1 $SUBJECTS_DIR/subcortical

# Convert binary surface output into stl format
mris_convert $SUBJECTS_DIR/subcortical $SUBJECTS_DIR/subcortical.stl

Next, open subcortical.stl with meshlab and apply ScaleDependent Laplacian Smooth as under step 3.

meshlab $SUBJECTS_DIR/subcortical.stl

The output you get should be as follows:

On the left you see the surface reconstruction before smoothing, just after the tesselation and on the right you see the smoothed subcortical surface model after scale dependent laplacian smoothing.

Now, as before: Click on File and Export Mesh and save the whole thing without Binary encoding.

Step 7 - Combine Cortical and Subcortial 3D Models

Now it's a short thing to concatenate the two files, cortical.stl and subcortical.stl into one final.stl file:

echo 'solid '$SUBJECTS_DIR'/final.stl' > $SUBJECTS_DIR/final.stl
sed '/solid vcg/d' $SUBJECTS_DIR/cortical.stl >> $SUBJECTS_DIR/final.stl
sed '/solid vcg/d' $SUBJECTS_DIR/subcortical.stl >> $SUBJECTS_DIR/final.stl
echo 'endsolid '$SUBJECTS_DIR'/final.stl' >> $SUBJECTS_DIR/final.stl

Step 8 - Clean-up Temporary Output

rm $SUBJECTS_DIR/bin.nii \
   $SUBJECTS_DIR/subcortical_bin.nii.gz \
   $SUBJECTS_DIR/subcortical_tmp.nii \
   $SUBJECTS_DIR/subcortical.nii \
   $SUBJECTS_DIR/subcortical.nii.gz \
   $SUBJECTS_DIR/subcortical

Step 9 - Reduce File Size

Use again meshlab to load final.stl.

meshlab $SUBJECTS_DIR/final.stl

Now, as a final step: Export the mesh again, but this time use Binary encoding. This will reduce the data volume dramatically and will make it easier to send or upload the 3D model.

Step 10 - Print 3D Model via Internet

So, now to the final steps. If you're lucky enough and you have you have your own access to a 3D printer, than you probably know what to do next. Lucky you!

If you don't have access to a 3D printer, than there are many options on the internet. I personally used www.shapeways.com. It's very easy to use, you can choose from many different materials and it gives you also the option to resize your model, as well as correct for flawed surface areas.

Step 11 - The Final product

And this is how the final product might look like:

3D print alternative, and more

I'm happy to point you to a nice alternative to this guide, written by Christopher Madan. Make sure to also check out his article!

And for people who are using WSL (windows subsystem for linux), check out Simon Kern's awesome fork of this repo here.

More Repositories

1

nipype_tutorial

Learn Nipype with these tutorial notebooks - go here to see them online -->
Jupyter Notebook
120
star
2

gif_your_nifti

How to create fancy GIFs from an MRI brain image
Python
119
star
3

atlasreader

Python interface for generating coordinate tables and region labels from statistical MRI images
Jupyter Notebook
88
star
4

fmriflows

fmriflows is a consortium of many (dependent) fMRI analysis pipelines, including anatomical and functional pre-processing, univariate 1st and 2nd-level analysis, as well as multivariate pattern analysis.
MATLAB
60
star
5

workshop_pybrain

Jupyter Notebook
55
star
6

nipype-beginner-s-guide

Beginner's guide for Nipype
Python
37
star
7

parcellation_fragmenter

Fragments FreeSurfer parcellation annotation in N-equal sized parcels
Python
27
star
8

workshop_cambridge

Content for the workshop in Cambridge 2018
Jupyter Notebook
15
star
9

create_fake_fmri_results

Small Notebook to create fake fMRI results (for tutorial or class) - run it online at ->
Jupyter Notebook
6
star
10

noah_ages

Contains the code to create a fun gif in which Noah Kalina ages 20 years in 20 seconds.
Jupyter Notebook
6
star
11

traffic_info_github

Script to track github traffic (views and clones) over period of 14 days
Python
5
star
12

wallpaper_clouded_globe

This script generates a satellite image from the whole planet (with cloud coverage) from https://worldview.earthdata.nasa.gov that than can be used as a wallpaper
Shell
5
star
13

nipype_course

Python
4
star
14

nasa_earthdata

Collect different kinds of satellite images from https://worldview.earthdata.nasa.gov
Jupyter Notebook
4
star
15

LINEViewer

Python based EEG analysis tool that provides a rough data overview
Python
4
star
16

nipype_env

A Docker image for a basic nipype environment
Dockerfile
2
star
17

STEN

Statistical interface for EEG analysing
Python
2
star
18

pac2018

Collection of different approaches to tackle the PAC2018 challenge
Jupyter Notebook
1
star
19

pupillometryAnalyzer

The pupillometryAnalyzer script can be used to analyse pupillometry raw data saved in a wks-file. It analyzes the time-series of one or both eyes and creates informative csv- and png-outputs that can be used for further statistical analysis.
MATLAB
1
star
20

workshop_ino_03

Innovation workshop
Jupyter Notebook
1
star
21

miykael

1
star
22

workshop_mumbai

Content for the workshop in Mumbai 2018
Jupyter Notebook
1
star
23

temporal_binding_window

These matlab scripts compute the temporal binding window (TBW) for a group of subject that participated in a behavioral multisensory integration study
MATLAB
1
star
24

xbar_screentime_tracker

Passive tracking of time spent in front of your screen (optimized for Mac OS X)
Python
1
star