Brain Image File Conversion/Import Tips for MATLAB

Moo K. Chung  mkchung@wisc.edu
Department of Biostatistics and Medical Informatics
Waisman Laboratory for Brain Imaging and Behavior
University of Wisconsin-Madison


Analyze file format

December 5, 2007

Please d/l and install
http://www.sph.sc.edu/comd/rorden/mricro.html
It is the simplest way to view medical images one slice at a time.
Get image dimension from MRICRO and run the following code
 
fid=fopen('F213-18-07.img');
for i=1:711
    [a,count]=fread(fid,512*512,
'int8');
    if i==427
        b=a;
    end;
end;
b=reshape(b,512,512);
imagesc(b)
 
It will load the 427th image slice into MATLAB.
Now compare the loaded image with MRIcro display.


AFNI file format

December 5, 2007

Converting MINC to BRIK

/apps/linux/afni/3dMINCtoAFNI  
This is the part of AFNI package 

3. Converting BRIK to MINC

/apps/linux/afni/3dAFNItoMINC 
This is the part of AFNI package


4. Loading BRIK into MATLAB
This is based on the AFNI MATLAB package written by Ziad Saad of NIH.


BrikName='jacob2';
Opt.Format = 'matrix';
[err, image, Info, ErrMessage] = BrikLoad (BrikName, Opt);

% image = 181 x 217 x 181

NIFTI file format

October 6, 2014

To read and write NIFTI file formats in MATLAB, you need to download the following package. To read a NIFTI file with file name sample.nii, run

nii = load_nii('sample.nii)
 
Image is stored in nii.img. Image dimension can be obtained by running

size(nii.img)

Saving volume image into NIFTI file is a bit complicated. Header information (image size, number formats etc) should be manually entered. Alteratively, you can copy and paste the header information from other NIFTI file formats. If you want to save image variable vol to NIFTI file name vol.nii, run

output=nii
output.img = vol
save_nii(output, 'vol.nii')