Skip to main content

MriSlicer

Namespace: MRILIB  ·  Library: MRI Library

#include <mri/mri_slicer.h>

class MRILIB::MriSlicer

Orthogonal-plane resampler (axial / coronal / sagittal) from MriVolData to MriSliceImage.

Pure static methods no instance state is owned. For a given orientation and slice index, samples the source volume at the matching row/column/depth plane, normalises voxel intensities into 8-bit grayscale via the volume's percentile-clipped window, and emits the result with the slice→RAS transform the viewer needs to draw rulers and crosshairs in millimetres.

src/examples/ex_mri/main.cpp
const Vector3f ras(0.001f, -0.0005f, -0.002f); // metres; voxel (2,3,1)
const Vector3i voxel = MriSlicer::rasToVoxel(vox2ras, ras);
const QVector<MriSliceImage> planes = MriSlicer::extractOrthogonal(voxels, mgh.dims(), vox2ras, ras); // axial, coronal, sagittal

Static Methods​

extractSlice(volData, dims, vox2ras, orientation, sliceIndex)​

Extract a 2D slice from a 3D volume stored as a flat array.

Parameters:

  • volData : const QVector< float > & Flat array of voxel intensities (dimX x dimY x dimZ, x-fastest).

  • dims : const QVector< int > & Volume dimensions {dimX, dimY, dimZ}.

  • vox2ras : const Eigen::Matrix4f & 4x4 voxel-to-RAS transform.

  • orientation : SliceOrientation Slice orientation (Axial, Coronal, or Sagittal).

  • sliceIndex : int Index along the slicing axis.

Returns:


voxelAxisForOrientation(vox2ras, orientation)​

Return the voxel axis used as the fixed slicing axis for an anatomical orientation.

Uses the voxel-to-RAS transform to map anatomical planes onto the source volume's storage axes. For FreeSurfer tkRAS volumes, axial maps to voxel Y and coronal maps to voxel Z, while an identity transform keeps the conventional Z/Y/X mapping.

Parameters:

  • vox2ras : const Eigen::Matrix4f & 4x4 voxel-to-RAS transform.

  • orientation : SliceOrientation Anatomical slice orientation.

Returns:

  • int — Voxel axis index: 0 = X, 1 = Y, 2 = Z.

dimensionForOrientation(dims, vox2ras, orientation)​

Return the volume dimension used by an anatomical slice orientation.

Parameters:

  • dims : const QVector< int > & Volume dimensions {dimX, dimY, dimZ}.

  • vox2ras : const Eigen::Matrix4f & 4x4 voxel-to-RAS transform.

  • orientation : SliceOrientation Anatomical slice orientation.

Returns:

  • int — Number of slices along the orientation's fixed voxel axis.

sliceIndexForOrientation(vox2ras, orientation, voxel)​

Return the slice index for an anatomical orientation from a voxel coordinate.

Parameters:

  • vox2ras : const Eigen::Matrix4f & 4x4 voxel-to-RAS transform.

  • orientation : SliceOrientation Anatomical slice orientation.

  • voxel : const Eigen::Vector3i & Voxel coordinate.

Returns:

  • int — The voxel component that indexes the requested anatomical slice stack.

extractOrthogonal(volData, dims, vox2ras, rasPoint)​

Extract all three orthogonal slices at a given RAS point.

Parameters:

  • volData : const QVector< float > & Flat array of voxel intensities.

  • dims : const QVector< int > & Volume dimensions {dimX, dimY, dimZ}.

  • vox2ras : const Eigen::Matrix4f & 4x4 voxel-to-RAS transform.

  • rasPoint : const Eigen::Vector3f & RAS coordinate to slice through.

Returns:


rasToVoxel(vox2ras, rasPoint)​

Convert RAS coordinate to voxel index.

Parameters:

  • vox2ras : const Eigen::Matrix4f & 4x4 voxel-to-RAS transform.

  • rasPoint : const Eigen::Vector3f & RAS coordinate.

Returns:

  • Eigen::Vector3i — Voxel index (rounded to nearest integer).

voxelToRas(vox2ras, voxel)​

Convert voxel index to RAS coordinate.

Parameters:

  • vox2ras : const Eigen::Matrix4f & 4x4 voxel-to-RAS transform.

  • voxel : const Eigen::Vector3i & Voxel index.

Returns:

  • Eigen::Vector3f — RAS coordinate.

extractSlice(vol, orientation, sliceIndex)​

Extract a 2D slice from an MriVolData volume.

Parameters:

  • vol : const MriVolData & Loaded MRI volume.

  • orientation : SliceOrientation Slice orientation.

  • sliceIndex : int Index along the slicing axis.

Returns:


voxelAxisForOrientation(vol, orientation)​

Return the voxel axis used as the fixed slicing axis for an anatomical orientation.

Parameters:

  • vol : const MriVolData & Loaded MRI volume whose vox2ras-tkr transform defines the axes.

  • orientation : SliceOrientation Slice orientation.

Returns:

  • int — Voxel axis index (0, 1 or 2) held fixed for this orientation.

dimensionForOrientation(vol, orientation)​

Return the volume dimension used by an anatomical slice orientation.

Parameters:

  • vol : const MriVolData & Loaded MRI volume.

  • orientation : SliceOrientation Slice orientation.

Returns:

  • int — Number of voxels along the slicing axis, i.e. the number of available slices.

sliceIndexForOrientation(vol, orientation, voxel)​

Return the slice index for an anatomical orientation from a voxel coordinate.

Parameters:

  • vol : const MriVolData & Loaded MRI volume.

  • orientation : SliceOrientation Slice orientation.

  • voxel : const Eigen::Vector3i & Voxel coordinate (column, row, slice).

Returns:

  • int — Component of voxel along the slicing axis for this orientation.

extractOrthogonal(vol, rasPoint)​

Extract all three orthogonal slices at a given RAS point.

Parameters:

  • vol : const MriVolData & Loaded MRI volume.

  • rasPoint : const Eigen::Vector3f & RAS coordinate to slice through.

Returns:


rasToVoxel(vol, rasPoint)​

Convert RAS coordinate to voxel index using a volume's transform.

Parameters:

  • vol : const MriVolData & Loaded MRI volume.

  • rasPoint : const Eigen::Vector3f & RAS coordinate.

Returns:

  • Eigen::Vector3i — Voxel index (rounded to nearest integer).

voxelToRas(vol, voxel)​

Convert voxel index to RAS coordinate using a volume's transform.

Parameters:

  • vol : const MriVolData & Loaded MRI volume.

  • voxel : const Eigen::Vector3i & Voxel index.

Returns:

  • Eigen::Vector3f — RAS coordinate.

Example​

Used in examples​

ex_mri​

MRI library: MGH/MGZ and NIfTI volumes, voxel/RAS transforms, slicing and COR sets.

src/examples/ex_mri/main.cpp, line 119
const Vector3f ras(0.001f, -0.0005f, -0.002f); // metres; voxel (2,3,1)
const Vector3i voxel = MriSlicer::rasToVoxel(vox2ras, ras);
const QVector<MriSliceImage> planes = MriSlicer::extractOrthogonal(voxels, mgh.dims(), vox2ras, ras); // axial, coronal, sagittal
ok &= expect(voxel == Vector3i(2, 3, 1) && (MriSlicer::voxelToRas(vox2ras, voxel) - ras).norm() < 1e-7f && planes.size() == 3,
QString("RAS (1, -0.5, -2) mm -> voxel (%1, %2, %3); three orthogonal planes").arg(voxel(0)).arg(voxel(1)).arg(voxel(2)));

Authors of this file​