Skip to main content

MRI defacing pipeline with skull-stripping and affine registration from cai4cai

Project description

caideface

MRI defacing pipeline with skull-stripping and affine registration from the cai4cai research group (Contextual Artificial Intelligence for Computer Assisted Interventions).

This pipeline anonymises head MRI scans by removing facial features while preserving brain structures, as described in the paper "A Generalisable Head MRI Defacing Pipeline: Evaluation on 2,566 Meningioma Scans" (arXiv:2505.12999).

Pipeline overview

The pipeline consists of three steps:

  1. Reorientation -- Aligns NIfTI scans to LAS canonical orientation (MNI152 standard) using nibabel.
  2. Skull-stripping -- Extracts brain masks using HD-BET, then applies dynamic dilation to preserve peripheral brain structures.
  3. Registration & Defacing -- Registers each scan to the MNI152 template using BRAINSFit (affine), warps a face mask into the scan's space, and applies it to remove facial features.

The MNI152 skull-stripped template and face mask are bundled with the package, so no additional downloads are needed.

Requirements

Python

  • Python >= 3.9

External tools (not pip-installable)

Tool Used in Install
BRAINSFit & BRAINSResample Step 3 Bundled with 3D Slicer

Note: Step 1 (reorientation) no longer requires FSL -- it uses nibabel's orientation tools to reorient scans to LAS (equivalent to fslreorient2std).

Finding BRAINSFit and BRAINSResample

These executables are included with 3D Slicer. Common locations:

  • macOS: /Applications/Slicer.app/Contents/lib/Slicer-5.8/cli-modules/BRAINSFit
  • Linux: /path/to/Slicer/lib/Slicer-5.8/cli-modules/BRAINSFit

Replace 5.8 with your installed Slicer version if different. To verify the executables are found and working:

# Check they exist
ls /Applications/Slicer.app/Contents/lib/Slicer-5.8/cli-modules/BRAINSFit
ls /Applications/Slicer.app/Contents/lib/Slicer-5.8/cli-modules/BRAINSResample

# Check they run (should print usage/help info)
/Applications/Slicer.app/Contents/lib/Slicer-5.8/cli-modules/BRAINSFit --help
/Applications/Slicer.app/Contents/lib/Slicer-5.8/cli-modules/BRAINSResample --help

You can also build them from source via BRAINSTools.

Installation

We recommend using a conda environment:

conda create -n caideface python=3.10 -y
conda activate caideface
pip install caideface

Or install from GitHub:

pip install "caideface @ git+https://github.com/cai4cai/defacing_pipeline.git#subdirectory=caideface"

Or install from source:

git clone https://github.com/cai4cai/defacing_pipeline.git
cd defacing_pipeline/caideface
pip install -e .

Note: caideface requires numpy<2 (enforced automatically). Some dependencies (HD-BET / nnU-Net) are not yet compatible with NumPy 2.x.

Usage

CLI -- Full pipeline

Run all three steps in one command:

caideface run ./input_nifti ./output \
  --brainsfit /path/to/BRAINSFit \
  --brainsresample /path/to/BRAINSResample

This creates three subdirectories under ./output:

  • reoriented/ -- Step 1 outputs
  • hdbet/ -- Step 2 outputs (skull-stripped, masks, dilated)
  • defaced/ -- Step 3 outputs (final defaced scans)

Options

Flag Default Description
--device auto-detected cpu or cuda for HD-BET
--no-tta on Disable HD-BET test-time augmentation (faster but less accurate)
--dilation-mm 14.0 Brain mask dilation in mm
--background 0 Fill value for defaced regions (0 for MRI, -1024 for CT)
--template bundled Custom MNI152 skull-stripped template
--face-mask bundled Custom face mask in MNI152 space
--steps all Run specific steps: reorient, skull_strip, deface (comma-separated)
-v off Verbose/debug logging

CLI -- Individual steps

Run each step separately for more control:

# Step 1: Reorientation
caideface reorient ./raw_nifti ./reoriented

# Step 2: Skull-stripping
caideface skull-strip ./reoriented ./hdbet --device cpu

# Step 3: Registration & Defacing
caideface deface ./reoriented ./hdbet ./defaced \
  --brainsfit /path/to/BRAINSFit \
  --brainsresample /path/to/BRAINSResample

Output structure

output/
├── reoriented/
│   ├── reorientation_log.csv
│   └── <subject>/<scan>.nii.gz
├── hdbet/
│   ├── hd_bet_log.csv
│   └── <subject>/
│       ├── hd_bet_<scan>.nii.gz           # Skull-stripped
│       ├── hd_bet_mask_<scan>.nii.gz      # Dilated brain mask
│       └── hd_bet_dilated_<scan>.nii.gz   # Dilated skull-stripped
└── defaced/
    ├── not_defaced_scans.csv              # Only if failures occurred
    └── <subject>/
        └── hd_bet_dilated_<scan>_masked.nii.gz  # Final defaced scan

Existing transforms

If you have pre-computed registration transforms (e.g. from 3D Slicer), place a file named Transform_to_template.txt in the same directory as the dilated skull-stripped scan. The pipeline will use it instead of running BRAINSFit. Both plain 4x4 text matrices and ITK/Slicer transform formats are supported.

Citation

If you use this tool, please cite:

@article{caideface2025,
  title={A Generalisable Head MRI Defacing Pipeline: Evaluation on 2,566 Meningioma Scans},
  year={2025},
  url={https://arxiv.org/abs/2505.12999}
}

If you use HD-BET (skull-stripping, Step 2), please also cite:

@article{Isensee2019,
  author={Isensee, F. and Schell, M. and Tursunova, I. and Brugnara, G. and Bonekamp, D. and Neuberger, U. and Wick, A. and Schlemmer, H. P. and Heiland, S. and Wick, W. and Bendszus, M. and Maier-Hein, K. H. and Kickingereder, P.},
  title={Automated brain extraction of multi-sequence MRI using artificial neural networks},
  journal={Human Brain Mapping},
  year={2019},
  pages={1--13},
  doi={10.1002/hbm.24750}
}

License

This project is licensed under the MIT License -- see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

caideface-0.2.0.tar.gz (6.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

caideface-0.2.0-py3-none-any.whl (6.6 MB view details)

Uploaded Python 3

File details

Details for the file caideface-0.2.0.tar.gz.

File metadata

  • Download URL: caideface-0.2.0.tar.gz
  • Upload date:
  • Size: 6.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for caideface-0.2.0.tar.gz
Algorithm Hash digest
SHA256 080a41e5979d7d8fb70b90de0013535ba6e5254694450e1511dfb3e570b13496
MD5 8e3b190548a7d62bbbc3ce1688500f93
BLAKE2b-256 fdc6fda97822ad9bfcfec0b4f13c886e727724c3ce3d28431a2a1c1cd485d47a

See more details on using hashes here.

File details

Details for the file caideface-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: caideface-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for caideface-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95f84bfab7477126cedd516155536387183746e7a186026955da0039c0793984
MD5 94a878b4967453555929254d989cbb52
BLAKE2b-256 616f6a807d1c5a636c870172970a01e62f0721e703caacb6659a990ba721210e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page