From MRI to Mesh: Finite Element Brain Model Creation
This exemplar provides a reproducible workflow for structural magnetic resonance (MR) images to be transformed into a finite element brain mesh in LS-DYNA keyword (.k) format. It combines image preprocessing, segmentation and meshing in one clear MRI-to-mesh workflow. It also contains optional post-processing to enable a downstream FE simulations.

This brain mesh creation pipeline was scientifically developed by Dr Mazdak Ghajari, Dr Harry Duckworth, Mr Vahid Darvish, and Ms Emily Chan, all in the HEAD Lab at Imperial College London.
This exemplar was developed at Imperial College London by Ms Emily Chan in collaboration with Dr Miruna Serina from Research Software Engineering and Dr Jianliang Gao from Research Computing & Data Science at the Early Career Researcher Institute.
With suitable MRI-derived inputs, this pipeline allows users to turn an individual's brain anatomy into a subject-specific finite element mesh.
Disciplinary Background π¬
In brain biomechanics research, subject-specific anatomical models are often generated from structural MRI data and converted into finite element meshes that can be used to simulate tissue deformation under mechanical loading. These models are widely used traumatic brain injury (TBI) research, where understanding how anatomy influences brain strain can help interpret injury mechanisms and improve predictive modelling. At the HEAD Lab, researchers have used the mesh generation pipeline to generate hundreds of brain FE models, enabling large-scale FE simulations in TBI research, particularly how anatomical differences of the brain affects injury.
Learning Outcomes π
After completing this exemplar, you will be able to:
- Process and visualise neuroimaging data using FSL, a useful open-source neuroimaging software library.
- Interpret the structure of an LS-DYNA mesh file, understand how nodes, elements, parts, and model definitions.
- Generate a subject-specific volumetric finite element brain mesh from structural MRI-derived inputs.
- (Optional) Understand the basic requirements for setting up an LS-DYNA simulation, including the role of supporting files needed alongside the generated mesh.
Prerequisites β
Academic π
- Basic familiarity with Python, including using libaries and calling functions.
- Basic familiarity with simple Bash commands in Jupyter notebook environment, such as navigating directories, writing shell commands, and handling files (e.g.
cd,cp,echo). - A general awareness of finite element modelling is helpful, but not required.
System π»
- Python 3.9 and above
- Jupyter Notebook
- FSL v6.0.7 and above, installed (requires ~20GB disk space) and configured properly
- Sufficient disk space (estimate 2GB) for intermediate image files, and generated meshes
- Ls-PrePost for visualising output brain mesh
Software Tools π οΈ
- Programming language: Python, with some Bash commands used within Jupyter notebooks.
- LS-Prepost: a GUI tool for pre- and post-processing of finite element models.
- FSL - FMRIB Software Library: a library for brain imaging data analysis.
FSL must be correctly configured before running the notebooks. In particular, the FSLDIR environment variable must be set, and the FSL command-line tools must be available from your terminal.
After installing FSL, follow the official FSL configuration instructions. For bash or zsh, this usually means adding the following lines to your shell configuration file, such as ~/.bashrc, ~/.bash_profile, or ~/.zshrc:
# FSL Setup
FSLDIR=~/fsl
PATH=${FSLDIR}/share/fsl/bin:${PATH}
export FSLDIR PATH
. ${FSLDIR}/etc/fslconf/fsl.sh
Getting Started π
The main step-by-step materials are stored together in docs/. This folder contains both the explanatory Markdown pages and the executable notebooks used in the MRI-to-mesh pipeline.
- Fulfil the Prerequisites.
- Follow
01_Introduction.mdfor background information, required inputs, and the expected folder structure. - Run
02_ImageProcess.ipynbto complete image processing and image segmentation. - Follow
03_FSLeyes.mdto inspect selected image-processing outputs in FSLeyes and check whether the segmentation worked as expected. - Run
04_MeshCreation.ipynbto generate, smooth, and refine the finite element brain mesh. - Follow
05_VisualiseMesh.mdto inspect the generated brain mesh in LS-PrePost by rotating, slicing, and checking the model geometry. - Optional: follow
06_Simulation.mdto prepare supporting files for downstream finite element simulation.
Apart from running the notebooks in your local environment, they can also be run directly in GitHub Codespaces using the instructions in Try code on GitHub with Codespaces.
Try the notebooks on GitHub Codespaces
You can run the notebooks directly in GitHub Codespaces without setting up the environment locally. The steps are shown in the image below.
- In the repository, click the green Code button, open the Codespaces tab, and click the three-dot "..."* menu.
- Select New with options... from the menu.
- Under Machine type, choose 8-core, 32 GB RAM, 64 GB storage or higher (this requires signing up to GitHub Student Developer Pack if you have not already done so).
- Review the selected settings, then click Create codespace.
- Wait for GitHub Codespaces to launch VS Code in the browser and finish configuring the environment.
- Open the notebook you want to run from the
notebooks/. Click Select Kernel, then choose Python Environments.... - Select the Recommended Python environment, for example the
baseenvironment, to run the notebook.
View neuroimages in Codespaces
There are two options available to view neuroimages.
- Option1 (maybe slow) : Use Trame to view images
- Find trame_nii_viewer.py and modify the line (16)
/workspaces/ReCoDE-brain-mesh-creation/data/subjects/avg-male/tmpto the imaging files directory you want to view. Then save the updated trame_nii_viewer.py. -
in a Codespace terminal, run the following command:
A new tab will be opened on your web browser to view images. (be aware: if it is slow to load images, please refresh the Trame viewer tab)fslpython trame_nii_viewer.py -
Option2 (fast): Use
fsleyes rendercommand to view images -
In Codespace terminal, run the following command:
fsleyes render --outfile output_T1.png data/subjects/sub0045/img/fs_seg/T1.nii.gz -cm brain_colours_nih- Modify the output filename
output_T1.pngas needed. This will be the exported PNG image. - Modify the input file path
data/subjects/sub0045/img/fs_seg/T1.nii.gzi f you want to render another NIfTI file. - Modify
-cm brain_colours_nihif you want to use a different colourmap. More colourmap codes can be found at fsleyes command
- Modify the output filename
Project Structure ποΈ
.
βββ data/ # Input data, intermediate files, and generated outputs
β βββ subjects/
β βββ sub0045/ # Clean example subject for users to run through the workflow
β β βββ img/
β β βββ fs_seg/ # FreeSurfer-derived segmentation inputs
β β βββ T1.nii.gz # T1-weighted structural MRI, used as input
β β βββ aseg.nii.gz # Automated segmentation label map derived from the T1 image
β β βββ brain.nii.gz # Skull-stripped T1 volume containing brain tissue only
β β
β βββ sub0045_example/ # Completed example subject with intermediate and final outputs
β βββ img/
β β βββ fs_seg/ # FreeSurfer-derived segmentation inputs
β β βββ T1.nii.gz
β β βββ aseg.nii.gz
β β βββ brain.nii.gz
β βββ bet/ # Example BET outputs
β βββ fast/ # Example FAST outputs
β βββ output/ # Example generated brain mesh and supporting files
β
βββ docs/ # Markdown documentation and visual guides
β βββ 01_Introduction.md # Background, inputs, and preprocessing overview
β βββ 02_ImageProcess.ipynb # Code for MRI preprocessing and image segmentation workflow
β βββ 03_FSLeyes.md # Guide for inspecting image-processing outputs
β βββ 04_MeshCreation.ipynb # Code for Mesh generation, smoothing, and refinement workflow
β βββ 05_VisualiseMesh.md # Guide for inspecting generated mesh outputs
β βββ 06_Simulation.md # Notes on preparing meshes for FE simulation
β βββ assets/ # Images and media used in the documentation
β
βββ src/ # Source code and supporting tools used by the workflow
β βββ brain_mesh_creation/ # Python package for mesh-generation utilities
β β βββ __init__.py
β β βββ mesh_utils.py # Python utilities for mesh processing and refinement
β β βββ bmctk.py # Script/module for brain mesh creation
β βββ dependencies/ # Non-Python supporting files required by the workflow
β βββ rs/
β βββ a.out # Executable for mesh smoothing
β βββ look_up_table.txt # Lookup table used during mesh creation
β βββ material_properties.k # FE material property file for the brain model
β
βββ trame_nii_viewer.py # Interactive web viewer script for inspecting NIfTIs
βββ mkdocs.yml # Configuration file for MkDocs
βββ pyproject.toml # Project metadata and dependency configuration
βββ requirements.txt # Optional pinned dependencies for local setup
βββ LICENSE.md # Project license
βββ README.md # This file; project overview and usage instructions
sub0045 by default. The sub0045_example folder is provided for reference and comparison. You should use the sub0045 folder when following the notebooks.
Roadmap πΊοΈ
Core π§©
- Install prerequisites and setup environment
- FSL installation
- LS-Prepost installation
- Image processing and segmentation
- Does the FAST segmentation produce reasonable result?
- Does the BET segmentation produce reasonable result?
- Is the generated geometry file
pre_model.nii.gzlook reasonable in FSLeyes?
- Mesh creation and smoothing
- Does the generated mesh look reasonable in LS-PrePost?
Extensions π
(Requires deeper understanding of finite element analysis)
- Understand the supporting files for FE simulations using the generated mesh
- Material property of the brain
- Centre of gravity of the brain
- Parts and sets of the brain model
- Acceleration, the mechanical loading apply to the brain model
- Run file, the configuration of FE simulation
Best Practice Notes π
- During image processing and segmentation steps, regularly check the intermediate outputs using FSLeyes if you are running the project locally, or follow this if you are using with Codespaces.
Estimated Time β³
| Task | Estimated time |
|---|---|
| Introduction | 10 min |
| Environment setup | 30 min |
| Image processing | 1 hour |
| Image visualisation | 1 hour |
| Mesh creation | 1 hour |
| Mesh visualisation | 1 hour |
| Extension | 30 min |
| Total Estimated Time | 5 hours |
Licence π
This project is licensed under the BSD-3-Clause license.