MRI Images And Data Structures
Author: Syeda Warda Taqdees
Contributor/s: Dr. Christina Tuke Flanders, Sila Genc
Learning Objectives
After completing this lesson, students should be able to
- Identify MRI image files stored in DICOM format.
- Load, analyze and identify the contents of MRI image files.
- Understand MATLAB 'structure' data type.
- Create and export NIFTI image files for FSL and MRtrix tools.
Learning objectives for sign posting
All MRI datasets contain images and details of experimental setup used for data acquisition and reconstruction. MRI images come in a variety of formats depending on the company of the scanner used for imaging. The most commonly encountered format, DICOM, is supported by Siemens, GE and Philips scanners. Bruker scanners support 2dseq format.
In this course, we will focus on DICOM images. However, most of the course content can be translated to MRI images in other formats with minor modifications.
DICOM File Strucutre
DICOM images are identified by the extension '.dcm'. A DICOM file consists of a 'Header' and a 'Dataset'.
The header contains information on the encapsulated Dataset. It consists of a File Preamble, a DICOM prefix, and the File Meta Elements.
Accessing DICOM Metadata
The function 'dicominfo' reads in metadata of a DICOM file.
% Specify data folder and image file name
dataFolder = './MRIData/sT2_TSE_T_301/';
imageName = 'IM-0001-0010.dcm';
% Read in Dicom metadata
dcmMetaData = dicominfo([dataFolder imageName]);
Detailed information about DICOM metadata and how it can be used for certain tasks e.g. memory pre-allocation can be found here.
Alternatively, 'dicomdisp' can be used to examine file structure and contents of a DICOM file.
% View Dicom file structure
dicomdisp([dataFolder imageName]);
Loading DICOM image
DICOM image data is read into MATLAB using 'dicomread' function.
% Read a DICOM image by using dicomread
mriImage1 = dicomread([dataFolder imageName]);
% Converting from uint16 to double
mriImage1 = double(mriImage1);
A quick way to check how a single image looks like is to use 'imagesc'
% Display single slice in grayscale
figure;
imagesc(mriImage1);
colormap('gray')
% Turn off axis
axis off
Generally, a single DICOM file contains a single MRI slice data. To read in MRI data for all slices, each individual DICOM files are loaded separately. 'For' loop is used to read in all data files.
% Reading data in bulk
for iImg = 1:22
mriImage(:,:,iImg) = dicomread([dataFolder sprintf('IM-0001-00%02d.dcm', iImg)]);
end
% Converting MRI data from uint16 to double
mriImage = double(mriImage);
Challenge!
% Challenge 1
% Download an MRI dicom dataset of your choice from internet.
% Load your dataset into MATLAB and display first dicom image of your
% dataset
% Create MRI image matrix from all dicom images in your downloaded dataset.
Structures in MATLAB
Structures are a convenient way of grouping heterogeneous data together into a single entity.
In MATLAB, a dot operator '.' identifies a structure. A structure contains a list of fields. Each field is a variable name that stores a particular piece of data. Structures can be used to store images and metadata.
% Structs are defined using '.'
Person1.FirstName = 'Jane';
Person1.LastName = 'Doe';
Structures can store heterogeneous data in their fields.
% A single struct can store different data types
Person1.PassportNum = 12345;
Person1.DOB = '01/01/2001';
Alternatively, the function 'struct' creates a structure array.
% Funtion struct creates a structure array
Person2 = struct('FirstName','John','LastName','Doe','PassportNum',67891,'DOB','30/01/2001');
It is possible to have an array of structures.
% An array of structures
Directory(1).Person = Person1;
Directory(2).Person = Person2;
Challenge!
% Challenge 2
% Create a structure called myImg with following fields
% Img: a random image of a 3D object with 3 slices
% (You can use MATLAB's in-build function 'magic' to fill image)
% Dim: a vector containing dimensions of the image
% Date: Date the image was created
% ID: A unique image ID
% Save myImage structure as a .mat file in current directory
Exporting MRI Data
The Neuroimaging Informatics Technology Initiative (NIfTI) file format is the most commonly used volume-file format supported by popular MRI softwares such as FSL, AFNI and MRtrix.
MATLAB has not naturalized NIfTI format yet. This means that it does not provide built-in functions for making, viewing, loading and storing NIfTI files. However, toolboxes for NIfTI files are freely available on MATHWORKS. For this workshop, we have provided a set of NIfTI functions in the folder 'Helper Functions' present in the lesson repository.
To make a NIfTI object in MATALB, the function 'make_nii' is used.
% MRI images can be exported from MATLAB in NIfTI format
mriImgNifti = make_nii(mriImage);
The NIfTI object returned by 'make_nii' can be stored in a file outside MATLAB using 'save_nii'.
% NIfTI object created above can be exported out of MATLAB using save_nii
save_nii(mriImgNifti,'mriNifti.nii');
Similarly, to load image data from a NIfTI file, 'load_nii' is used.
% Re-loading NIfTI file
mriImgReload = load_nii('mriNifti.nii');
Challenge!
% Challenge 3
% Convert 'myImg' created in pervious challenge into NIfTI format
% Save NIfTI structure as .nii file into your current directory