Skip to main content

brain extraction in python

Project description


A re-implementation of FSL's Brain Extraction Tool in Python.

Follows the algorithm as described in:

Smith SM. Fast robust automated brain extraction. Hum Brain Mapp.
2002 Nov;17(3):143-55. doi: 10.1002/hbm.10062. PMID: 12391568; PMCID: PMC6871816.

This code was originally made for a course project.


To install, use pip to install this repo:

# install from pypi
pip install brainextractor

# install repo with pip
pip install git+

# install from local copy
pip install /path/to/local/repo

NOTE: It is recommended to use brainextractor on Python 3.7 and above.


To extract a brain mask from an image, you can call:

# basic usage
brainextractor [input_image] [output_image]

# example
brainextractor /path/to/test_image.nii.gz /path/to/some_output_image.nii.gz

You can adjust the fractional intensity with the -f flag:

# with custom set threshold
brainextractor [input_image] [output_image] -f [threshold]

# example
brainextractor /path/to/test_image.nii.gz /path/to/some_output_image.nii.gz -f 0.4

To view the deformation process (as in the video above), you can use the -w flag to write the surfaces to a file. Then use brainextractor_render to view them:

# writes surfaces to file
brainextractor [input_image] [output_image] -w [surfaces_file]

# load surfaces and render
brainextractor_render [surfaces_file]

# example
brainextractor /path/to/test_image.nii.gz /path/to/some_output_image.nii.gz -w /path/to/surface_file.surfaces

brainextractor_render /path/to/surface_file.surfaces

If you need an explanation of the options at any time, simply run the --help flag:

brainextractor --help

If you need to call Brainextractor directly from python:

# import the nibabel library so we can read in a nifti image
import nibabel as nib
# import the BrainExtractor class
from brainextractor import BrainExtractor

# read in the image file first
input_img = nib.load("/content/MNI.nii.gz")

# create a BrainExtractor object using the input_img as input
# we just use the default arguments here, but look at the
# BrainExtractor class in the code for the full argument list
bet = BrainExtractor(img=input_img)

# run the brain extraction
# this will by default run for 1000 iterations
# I recommend looking at the run method to see how it works

# save the computed mask out to file

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

brainextractor-0.2.2.tar.gz (13.4 kB view hashes)

Uploaded Source

Built Distribution

brainextractor-0.2.2-py3-none-any.whl (13.8 kB view hashes)

Uploaded Python 3

Supported by

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