NIfTI: The Standard Format for Brain MRI and fMRI Data
What Is NIfTI?
NIfTI — Neuroimaging Informatics Technology Initiative — is an open file format designed specifically for storing brain imaging data from MRI, fMRI, PET, DTI, and other neuroimaging modalities. Developed in 2004 by a working group under the NIH, NIfTI was created to replace the aging ANALYZE 7.5 format and establish a single, unambiguous standard for sharing volumetric brain data across research labs worldwide.
Today, NIfTI is the lingua franca of neuroimaging: virtually every major scanner vendor, analysis pipeline, and open dataset uses it. Files come in two flavors:
.nii— a single file containing both the header and the image data concatenated together.hdr/.imgpaired files — the legacy two-file layout inherited from ANALYZE (header separate from raw voxel data).nii.gz— gzip-compressed.nii, by far the most common form in practice (reduces file size 60–80% for typical brain volumes)
Internal Structure of a NIfTI File
A .nii file begins with a 348-byte header (identical in size to ANALYZE 7.5 for backward compatibility), followed by a 4-byte extension indicator, optional extensions, and then the raw voxel data stored in row-major order (fastest index = x).
Key header fields you'll encounter:
| Field | Bytes | Purpose |
|---|---|---|
sizeof_hdr |
4 | Must be 348 (NIfTI-1) or 540 (NIfTI-2) |
dim[8] |
16 | Array dimensions: dim[0]=rank, dim[1–7]=sizes |
pixdim[8] |
32 | Voxel sizes in mm (and TR in seconds for dim[4]) |
datatype |
2 | Voxel data type: INT16, FLOAT32, COMPLEX64, etc. |
scl_slope / scl_inter |
8 | Linear rescaling: stored_value × slope + intercept |
qform_code / sform_code |
4 | Coordinate system declarations |
quatern_* / qoffset_* |
28 | Quaternion-based scanner coordinates (qform) |
srow_x/y/z |
48 | Affine matrix rows mapping voxels to mm (sform) |
The sform and qform fields are what make NIfTI powerful: they encode the precise orientation and position of the volume in a real-world coordinate space (scanner-native, MNI152 standard space, Talairach, etc.), eliminating the left-right ambiguity that plagued ANALYZE.
NIfTI-2 (2011) extends dim and pixdim from 16-bit integers / 32-bit floats to 64-bit, enabling volumes larger than 32,767 voxels per dimension — critical for high-resolution 7T MRI datasets.
Coordinate Systems: qform vs. sform
NIfTI uses two optional coordinate transforms, each tagged with a code:
- qform (codes 1–4): Encodes the scanner's acquisition geometry as a quaternion rotation plus three offsets. Code 1 = scanner anatomical coordinates; code 2 = aligned to another scan; codes 3–4 = Talairach or MNI space.
- sform (code 1–4): A full 4×3 affine matrix — more flexible, used for arbitrary warps or after preprocessing to standard space.
The convention: if sform_code > 0, use sform; if only qform_code > 0, use qform. Software like FSL, FreeSurfer, and SPM all respect this hierarchy.
Voxel Data Types
NIfTI supports a rich set of datatype values:
UINT8(2),INT16(4),INT32(8) — integer anatomical MRI (typical T1 from scanner is INT16)FLOAT32(16) — statistical maps (t-maps, z-maps, beta images from GLM)FLOAT64(64) — double-precision for intermediate computationsCOMPLEX64(32) /COMPLEX128(1792) — raw k-space or spectroscopy dataRGB24(128) — color-coded FA (fractional anisotropy) maps from DTI
The scl_slope and scl_inter fields let scanners store data as INT16 while preserving the full physical range in FLOAT after rescaling — a common pattern for scanner output.
Working with NIfTI in Python: nibabel
nibabel is the go-to Python library:
import nibabel as nib
import numpy as np
# Load a .nii.gz
img = nib.load('sub-01_T1w.nii.gz')
# Image data as NumPy array
data = img.get_fdata() # returns float64, applies scl_slope/inter automatically
print(data.shape) # e.g. (256, 256, 176) for a 1mm isotropic T1
print(img.header['pixdim'][1:4]) # voxel sizes in mm
# Affine matrix (4×4, sform or qform depending on codes)
aff = img.affine
print(aff)
# Modify and save
new_img = nib.Nifti1Image(data * 2, affine=aff, header=img.header)
nib.save(new_img, 'doubled.nii.gz')
# Access specific header fields
print(img.header.get_data_dtype()) # e.g. float64
print(img.header.get_zooms()) # (1.0, 1.0, 1.0, 2.0) — mm + TR
For 4D fMRI data (x, y, z, time), data.shape will be (64, 64, 36, 240) for a typical 6-minute run at 2×2×3 mm with TR=1.5 s and 240 volumes.
Standard Neuroimaging Pipelines That Use NIfTI
- BIDS (Brain Imaging Data Structure) — the community standard for organizing NIfTI files:
sub-01/ses-01/anat/sub-01_ses-01_T1w.nii.gz,sub-01/ses-01/func/sub-01_ses-01_task-rest_bold.nii.gz - fMRIPrep — preprocessing pipeline for fMRI; inputs and outputs exclusively NIfTI
- FSL — Oxford's neuroimaging suite; native NIfTI throughout (FEAT, MELODIC, BET, FLIRT)
- SPM — Statistical Parametric Mapping (MATLAB); uses NIfTI for all analyses since SPM8
- FreeSurfer — cortical surface reconstruction; imports/exports NIfTI alongside MGH format
- MRtrix3 — diffusion tractography; reads NIfTI, stores streamlines in
.tck - AFNI — Analysis of Functional NeuroImages; supports NIfTI natively alongside BRIK/HEAD
Converting to NIfTI: dcm2niix
Raw MRI output from scanners is in DICOM. The standard tool for DICOM→NIfTI conversion is dcm2niix (Chris Rorden):
dcm2niix -z y -f "%p_%s" -o ./nifti/ ./dicom_folder/
Options: -z y produces .nii.gz; -f sets the filename template using DICOM metadata tags (protocol name %p, series number %s). dcm2niix also writes a .json sidecar with scanner parameters (TR, TE, FlipAngle, SliceTiming) for BIDS compliance.
Practical Tips
- Always check orientation before analysis: use
fslorient,fslhd, or nibabel to verify the sform/qform before feeding into pipelines. Wrong orientation is the single most common source of subtle errors. - Prefer
.nii.gzfor storage and sharing; most tools handle compressed NIfTI transparently via zlib. - Use
fslinfo/fslhd(FSL) for quick header inspection on the command line. - NIfTI-1 dim limit: if any dimension > 32,767, you need NIfTI-2. This matters for ultra-high-res atlases and large group-level data arrays.
- 4D volumes: temporal fMRI data is a single 4D NIfTI — do not split into per-volume files unless specifically required.
- MNI space: standard group analysis aligns individual brains to MNI152 template; post-registration NIfTI files should carry
sform_code=4and the MNI affine.
NIfTI's combination of simplicity, rich coordinate metadata, and universal toolchain support makes it the cornerstone of modern human and animal neuroimaging research.
Related conversions
Frequent conversions across the catalogue: