Brain volume visualization with fsbrain

In this document, we demonstrate some high-level functions for loading and visualizing brain volume data. While this package has been developed with FreeSurfer in mind, many of the visualization functions work with 3D matrices and do not make any assumptions on where the data comes from.

Please note that the volume visualization functions presented here are a very recent addition to fsbrain, and that they are still in a very early development stage. I am very grateful for any bug reports or comments, please see the project website.

Example data used in this document

We will use example data that comes with fsbrain throughout this manual. Feel free to replace the subjects_dir, subjects_list and subject_id with data from your study.

subjects_dir = fsbrain::get_optional_data_filepath("subjects_dir");
subject_id = 'subject1';       # for function which use one subject only

Loading volume data

If all you want is the raw voxel data, you could load a FreeSurfer volume in MGH or MGZ format like this:

brain_data = subject.volume(subjects_dir, subject_id, "brain");

In many cases, you will need the header information as well, e.g., to be able to compute RAS coordinates for the voxels. If that’s the case, load the volume with header:

brain = subject.volume(subjects_dir, subject_id, "brain", with_header = TRUE);
brain_data = brain$data;

Volume visualization

A brain volume file contains one or more three-dimensional (3D) single channel (grayscale) images, typically encoded as integer arrays representing intensity values at each voxel. A 3D image is sometimes called a z-stack of 2D-images, or more general as a collection of 2D image slices. The fsbrain library offers two different ways to view 3D images: a lightbox mode, which shows a grid of several 2D slices, and a true 3D view that renders voxels or meshes computed from them.

An important thing to keep in mind when visualizing volume data is the range of your intensity values. They may be in range 0-255, 0-65535, or 0-1. Most visualization functions expect input values to be normalized to the range 0-1, so you may need to perform a normalization step (see the examples below).

Basic Lightbox view for raw MRI data

Here is an example that loads a brain volume, computes a bounding box, and visualizes some slices along the first axis in lightbox view:

brain = subject.volume(subjects_dir, subject_id, 'brain') / 255;
bounded_brain =, apply=TRUE);

The bounding box serves to remove background at the borders of the image tiles and is optional. See the help for for details on how to control it.

Figure 1: Lightbox view.
Figure 1: Lightbox view.

Have a look at optional arguments to the functions to see how to modify the threshold used when computing the bounding box, how to define which slices are displayed, and how to change the slice axis.

Notice that we scaled the raw data by 255 in this example, as the volume file contains values in the theoretical range 0-255. If none of the voxels actually reaches the max value of 255, you could scale by the actual max value for improved visual contrast.

Advanced lightbox view

The new function is a convenient wrapper around volvis.lightbox and should be preferred over the latter. It is especially convenient for displaying statistical results with a colormap, and optionally with a background for orientation within the brain anatomy.

Please have a look at the help and examples for until this vignette has been updated, it’s worth it.

Here are some example commands for visualization of continuous data images (raw MRI scans, statistical results, etc). They assume that your subjects_dir is ~/data/study1 and that your subject_id is subject1."~/data/study1/subject1/mri/brain.mgz");"~/data/study1/subject1/mri/brain.mgz", bbox_threshold = 1L);"~/data/study1/subject1/mri/brain.mgz", background = "~/data/study1/subject1/mri/T1.mgz");"~/data/study1/subject1/mri/brain.mgz", background = "#FEFEFE", background_color="#FEFEFE");
Figure 2: Output for command 2 above.
Figure 2: Output for command 2 above.