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  :

nifti1_io.cpp
nifti2.h
nifti2_io.h
nifti_tool.h
nifticdf.h
znzlib.h
nifti1.h
nifti1_io.h
nifti2_io.cpp
nifti_tool.cpp
nifticdf.cpp
znzlib.cpp

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);

Example

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

My_nii_read.cpp

  • 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: https://github.com/layerfMRI/LAYNII

Usage of My_nii_read.cpp

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

 git clone https://github.com/layerfMRI/laynii

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:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s