MriMghIO
Namespace: MRILIB · Library: MRI Library
#include <mri/mri_mgh_io.h>
class MRILIB::MriMghIO
Stateless decoder for FreeSurfer MGH and MGZ volume containers.
Parses the 284-byte big-endian header, decodes the column-major voxel buffer for every supported MRI type (uchar, int, float, short) and reads the optional tag footer (scan parameters, talairach.xfm path) into an MriVolData. .mgz inputs are inflated in-memory through zlib's MAX_WBITS+16 gunzip mode before parsing, so callers never need to know whether they are looking at the compressed or uncompressed variant.
Format reference: https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/MghFormat
Ported from make_mgh_cor_set() in MNE C mne_make_cor_set by Matti Hamalainen.
MriVolData mgh;
QVector<FiffCoordTrans> extraTransforms; // e.g. Talairach, if the footer names one
MriMghIO::read(dataDir + "/gradient.mgz", mgh, extraTransforms);
const Matrix4f vox2ras = mgh.computeVox2Ras(); // scanner RAS, metres
const QVector<float> voxels = mgh.voxelDataAsFloat(); // x fastest
Static Methods
read(mgzFile, volData, additionalTrans, subjectMriDir, verbose)
Reads a FreeSurfer MGH or MGZ file.
Parses the header geometry (dimensions, voxel sizes, direction cosines, center RAS), reads voxel data into per-slice MriSlice structures, and extracts footer tags including the Talairach .xfm path.
For .mgz files, automatic gzip decompression is performed via zlib.
Parameters:
-
mgzFile : const QString & Path to the .mgz or .mgh file.
-
volData : MriVolData &
MriVolDatastructure to populate. -
additionalTrans : QVector< FiffCoordTrans > & Surface RAS -> RAS and, if the footer names a talairach.xfm, the Talairach chain of
FiffCoordTransSet. -
subjectMriDir : const QString & Path to subject's mri/ directory (for resolving relative .xfm paths).
-
verbose : bool If true, print progress information.
Returns:
- bool — True on success, false on error.
Example
- Full source:
src/examples/ex_mri/main.cpp - Build target:
ex_mri(runs under CTest)
Used in examples
ex_mri
MRI library: MGH/MGZ and NIfTI volumes, voxel/RAS transforms, slicing and COR sets.
MriVolData mgh;
QVector<FiffCoordTrans> extraTransforms; // e.g. Talairach, if the footer names one
MriMghIO::read(dataDir + "/gradient.mgz", mgh, extraTransforms);
const Matrix4f vox2ras = mgh.computeVox2Ras(); // scanner RAS, metres
const QVector<float> voxels = mgh.voxelDataAsFloat(); // x fastest
Matrix4f vox2rasMm = vox2ras;
vox2rasMm.block<3, 4>(0, 0) *= 1000.0f;
ok &= expect(mgh.dims() == QVector<int>({6, 5, 4}) && (vox2rasMm - nibabelAffine).cwiseAbs().maxCoeff() < 1e-4f && voxels.size() == 120 && voxels[2 + 6 * (3 + 5 * 1)] == 82.0f,
"MGZ: 6x5x4 voxels, vox2ras and voxel (2,3,1) = 82 match nibabel");
Authors of this file
- Christoph Dinh <christoph.dinh@mne-cpp.org>