Using a standalone Nifti I/O in C++

In this blog post, I describe how you can build your own standalone C++ program to analyze nii data without any dependencies beyond C++.  Hence, it will work without the  libraries from other fMRI software packages, e.g. odin, afni. The only tricky part is to use a stand alone nii I/O to write your own C++ evaluation programs. Here, I do it by incorporating the few files that i borrowed from the afni source code.

What you need

Bob Cox and Rick Reynolds wrote few a nii I/O that I incorporated. The original version is in the AFNI sources. I collected all the necessary files and adapted them for my taste. There are only 11 necessary files  :


Linking those allows you to use nii_datatype and load nii files in your own C++ program with the function

nifti_image * nim=NULL;
nim = nifti_image_read(filename, 1);


The following “Hello World” toy-example will guide you through the important steps:


  • it reads in a nii file,
  • accesses the data,
  • manipulates the individual voxels
  • writes out the manipulated data as nii

It can be found here:

Usage of My_nii_read.cpp

1.) download the all the files with from github E.g. with the command:

 git clone

2.) compile it with:

 make My_nii_read 

3.) execute it with:

 ./My_nii_read -input input_example.nii -output output.nii -cutoff 3 

Big thanks to Rick Reynolds and Daniel Glenn for help and advice.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s