Various Methods for Reading Images in MATLAB

Table of Contents:(1) DPABI Package’s y_ReadAll Function(2) DPABI Package’s y_Read Function(3) SPM Package’s <span><span>spm_vol</span></span> + <span><span>spm_read_vols</span></span> Function

Briefly introduce the difference between NIfTI 3D and 4D files:

1. 3D Files: Contains only one three-dimensional image

  • Pixels in the x, y, and z directions

  • Example: Brain structure scan

Each 3D image is referred to as a “volume.”

2. 4D Files: Composed of multiple 3D images stacked together

  • The fourth dimension is time

  • Example: Each time point in fMRI is a 3D image

(1) DPABI Package’s y_ReadAll Function

DPABI package download link:https://rfmri.org/DPABIy_ReadAll is a MATLAB function used to read NIfTI images. It can handle a single file, a directory, or a list of multiple files, and outputs the results as a unified 4D matrix.

[Data, VoxelSize, FileList, Header] = y_ReadAll(InputName)

<span><span>InputName: Supports various input formats:</span></span><span><span>Single file:</span></span><span><span>.nii</span></span>, <span><span>.nii.gz</span></span>, or <span><span>.img/.hdr</span></span> files.Directory: The directory can contain either a 4D file or a set of 3D images.

File List (cell array): Each element is a path to a 3D image file or a single 4D file path. The function automatically determines whether the input is a single file, a directory, or a file list.

% Read a single compressed NIfTI filenii_path = 'S1.nii.gz';[Data, VoxelSize, FileList, Header] = y_ReadAll(nii_path);% Read all NIfTI files in a directorydir_path = '/data/sub01';[Data, VoxelSize, FileList, Header] = y_ReadAll(dir_path);% Read multiple file listfile_list = {'S1.nii.gz', 'S2.nii.gz', 'S3.nii.gz'};[Data, VoxelSize, FileList, Header] = y_ReadAll(file_list);

<span><span>Output Parameters:</span></span>

  • <span>Data</span>: 4D image matrix.

  • <span>VoxelSize</span>: Voxel size (x, y, z).

  • <span>FileList</span>: List of read files (cell array).

  • <span>Header</span>: A structure containing image information, with fields including:

    • <span>fname</span>: File name

    • <span>dim</span>: Dimensions of the volume in x, y, z

    • <span>dt</span>: Data type

    • <span>mat</span>: 4×4 affine matrix

    • <span>pinfo</span>: Scaling and offset information for each plane

(2) DPABI Package’sy_Read Function<span><span>y</span></span><span><span>_Read</span></span><span> Function is a more basic NIfTI file reading function than </span><span><span> </span></span><code><span><span>y_ReadAll</span></span>, mainly used to read a single file or a specified volume of a single 4D file.y_Read reads files through SPM’s NIfTI tools and can handle <span><span>.nii</span></span>, <span><span>.nii.gz</span></span>, <span><span>.img/.hdr</span></span> files.

 [Data, Header] = y_Read(FileName, VolumeIndex)

Input Parameters:

  • <span>FileName</span>: File path and name, supports the following formats:

    • <span>.nii</span>

    • <span>.nii.gz</span>

    • <span>.img/.hdr</span>

  • <span>VolumeIndex</span> (optional): Used to select which 3D image (volume) to read:

    • Can be an integer (1,2,3…) indicating which image to read

    • or <span>'all'</span> (default), indicating to read all volumes

Output Parameters:

  • <span>Data</span>: Image data matrix

    • If only one volume is read → 3D matrix

    • If all volumes are read → 4D matrix

  • <span>Header</span>: Image header information (structure, consistent with SPM), fields include:

    • <span>fname</span><span>e</span>: File name

    • <span>dim</span>: Volume x, y, z dimensions

    • <span>dt</span>: Data type and byte order

    • <span>mat</span>: 4×4 affine matrix

    • <span>pinfo</span>: Scaling and offset information for each plane

Function Features:

  1. Automatically handles compressed files: If it is a <span><span>.nii.gz</span></span>, it will decompress before reading, then delete the decompressed temporary file.

  2. Flexible reading of volumes:

  • <span>VolumeIndex = 1</span> → Read the first volume

  • <span><span>VolumeIndex = 'all'</span></span> → Read all volumes

  • Based on SPM’s <span>nifti</span> and <span>spm_vol</span>, ensuring compatibility and correct information.

  • Example:

    % Read the entire 4D NIfTI file[Data, Header] = y_Read('S1.nii.gz');% Read only the 3rd volume[Data, Header] = y_Read('S1.nii.gz', 3);

    (3) SPM Package’s<span><span>spm_vol</span></span> + <span><span>spm_read_vols</span></span> Function

    spm_vol

    • Function: Reads the Header information of a NIfTI file (dimensions, affine matrix, data type, etc.), without reading image data.

    • Input: File name (string or cell array)

    • Output: Structure <span>V</span>, fields include:

      • <span>fname</span>: File name

      • <span>dim</span>: Volume dimensions <span>[X Y Z]</span>

      • <span>mat</span>: Affine matrix (voxel→mm)

      • <span>dt</span>: Data type

      • <span>pinfo</span>: Plane scaling and offset information

    Example::

    V = spm_vol('S1.nii');disp(V.dim);

    spm_read_vols

    • Function: Reads image data based on <span><span>spm_vol</span></span> and returns a 3D or 4D matrix, with an option to set 0 values to <span>NaN</span>.

    • Input:

      • <span>V</span>: Structure returned by <span><span>spm_vol</span></span>

      • <span>mask</span> (optional):true/false, whether to convert 0 to NaN

    • Output:

      • <span>Y</span>: Image data matrix

      • <span>XYZmm</span> (optional): Spatial coordinates (mm) for each voxel

    Example::

    Y = spm_read_vols(V);      % Read imageY_masked = spm_read_vols(V,true); % 0 to NaN

    Usage Process:

    1. <span>V = spm_vol('file.nii');</span> → Get Header

    2. <span>Y = spm_read_vols(V);</span> → Read image data

    In summary:<span><span>spm_vol</span></span> only looks at information, while <span><span>spm_read_vols</span></span> actually reads the data.

    Leave a Comment