4.56522

4.6 | 24 ratings Rate this file 410 downloads (last 30 days) File Size: 88.62 KB File ID: #8797

Tools for NIfTI and ANALYZE image

by Jimmy Shen

 

23 Oct 2005 (Updated 06 Jan 2010)

Code covered by the BSD License  

Load, save, make, reslice, and view both NIfTI & ANALYZE data on any platform

Download Now | Watch this File

File Information
Description

http://www.rotman-baycrest.on.ca/~jimmy/NIfTI for detail descriptions

http://www.rotman-baycrest.on.ca/~jimmy/NIfTI/FAQ.htm for frequently asked questions

http://www.rotman-baycrest.on.ca/~jimmy/UseANALYZE.htm if you are using ANALYZE images
 
Basic Programs:

1. load_nii.m: Load N-Dimensional NIfTI data (where N can be from 3 to 7) or ANALYZE data (where N can be from 3 to 4), and apply header info (e.g. affine geometric transform, voxel intensity scaling, etc.) to the data. If your data has more than 3-Dimension (e.g. time series etc.), you can also specify a range to extract only 1 or several volumes.

2. save_nii.m: Save N-Dimensional NIfTI data (where N can be from 3 to 7) that is loaded by "load_nii.m" or made by "make_nii.m".

3. make_nii.m: Make N-Dimensional NIfTI data (where N can be from 3 to 7) based on the N-Dimensional matrix and other optional parameters (e.g. voxel_size, origin, etc.). Using "save_nii" command, the NIfTI data that is made by "make_nii" can be saved into a NIfTI file.

4. make_ana.m: Make 3D ANALYZE data based on the 3D matrix and other optional parameters (e.g. voxel_size, origin, etc.). Using "save_untouch_nii" command, the ANALYZE data that is made by "make_ana" can be saved into an ANALYZE file in order to be compatible with some ANALYZE only programs.

5. reslice_nii.m: Reslice 3D (or 4D) NIfTI data or ANALYZE data with affine matrix M in .mat file. The program will base on the affine matrix, which is especially useful for oblique images with non-orthogonal rotation or shearing that cannot be loaded with "load_nii.m". You can also specify voxel_size, etc. It will not cause negative effect, as long as you remember not to do slice time correction after using "reslice_nii.m".

6. view_nii.m: View 3D (or 4D) NIfTI data or ANALYZE data that is loaded by "load_nii.m" or made by "make_nii.m". Activation map, ROI, etc. can be overlaid on top of a background image (see above picture). Plotted view can be embedded into your existing figure window. See detail at http://www.rotman-baycrest.on.ca/~jimmy/NIfTI/examples.txt.

7. load_untouch_nii.m: Load N-Dimensional NIfTI data (where N can be from 3 to 7) or ANALYZE data (where N can be from 3 to 4), but do not apply any changes indicated in the header. (Warning: Do not apply "view_nii.m" to the dataset loaded with "load_untouch_nii.m".

8. save_untouch_nii.m: Save N-Dimensional NIfTI data (where N can be from 3 to 7) or ANALYZE data (where N can be from 3 to 4) that is loaded by "load_untouch_nii.m" or made by "make_ana.m". If you do not modify the loaded dataset, the header and data in the new saved file should be the same as those in the original file.

Other Programs:
 
1. collapse_nii_scan.m: Integrate multiple single-scan NIfTI or ANALYZE files into a multiple-scan NIfTI file.
 
2. expand_nii_scan.m: Break a multiple-scan NIfTI file into multiple single-scan NIfTI files.
 
3. flip_lr.m: Convert ANALYZE or NIfTI file to an L-R flipped NIfTI file. (Warning: please use this program with caution!).
 
4. get_nii_frame.m: Return number of time frames of a NIfTI dataset.
 
5. load_nii_ext.m: Load NIfTI header extension.
 
6. mat_into_hdr.m: Integrate the affine matrix in old SPM MATLAB file into its .hdr header file to become NIfTI format file.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
MRI Analyze tools
This submission has inspired the following:
Siemens DICOM sort and convert to NIfTI, Bipolar Colormap

MATLAB release MATLAB 5.3.1 (R11.1)
Zip File Content  
Other Files
affine.m,
bipolar.m,
bresenham_line3d.m,
collapse_nii_scan.m,
examples.txt,
expand_nii_scan.m,
extra_nii_hdr.m,
flip_lr.m,
get_nii_frame.m,
license.txt,
load_nii.m,
load_nii_ext.m,
load_nii_hdr.m,
load_nii_img.m,
load_untouch0_nii_hdr.m,
load_untouch_nii.m,
load_untouch_nii_hdr.m,
load_untouch_nii_img.m,
make_ana.m,
make_nii.m,
mat_into_hdr.m,
reslice_nii.m,
rri_file_menu.m,
rri_orient.m,
rri_orient_ui.m,
rri_xhair.m,
rri_zoom_menu.m,
save_nii.m,
save_nii_ext.m,
save_nii_hdr.m,
save_untouch0_nii_hdr.m,
save_untouch_nii.m,
save_untouch_nii_hdr.m,
unxform_nii.m,
verify_nii_ext.m,
view_nii.m,
view_nii_menu.m,
xform_nii.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (61)
14 Nov 2005 Anita D

this program works good when i output the nii file as int, but when i output the nii file as unint, the image is distorted.

15 Nov 2005 Jimmy Shen

Hi Anita: Thanks for the feedback. However, I checked but did not find any problem to load or save nii file with non-integer datatype. For example, you can download a typical NIFTI data (zstat1.nii) from: http://nifti.nimh.nih.gov/nifti-1/data This file is in single (float32) datatype. You can output to nii structure using: nii=load_nii('zstat1.nii'), and output to a new file using: save_nii(nii, 'newfile.nii'). If you still have problem, please email me, and let me take a look at your data.

24 Nov 2005 zhi yang  
13 Feb 2006 Jimmy Shen

Hi Henry:

This is an advanced feature, and it is already included in the toolbox.

Here is the brief guide to use this feature:

1. Use "ana = load_nii(ana.img)" to load your analyze image to "ana" struct, and make sure that variable "ana.img" is either in "single" or "double". If not, use step 2 to convert it.

2. To convert variable "ana.img" to "single", you need to use "make_nii" command: "ana = make_nii(ana.img, ana.hdr.dime.pixdim(2:4), ana.hdr.hist.originator(1:3), 16)".

3. Use "thresh = load_nii(thresh.img)" to load your thresholded image to "thresh" struct, and extract its value and index to "opt" struct: "opt.setvalue.idx = find(thresh.img); opt.setvalue.val = thresh.img(find(thresh.img));". You can see more detail to use "setvalue" by command: "help view_nii".

4. Add other values to "opt" struct: "opt.command = 'init'; opt.useinterp = 0;".

5. Plot thresholded image with underlaid analyzed image by command: "view_nii(ana, opt);".

6. Don't click "interp on/off" menu. It doesn't work properly. If you would like to see the interpolated one, use: "opt.useinterp = 1; view_nii(ana, opt);" instead.

Please let me know if you can plot your thresholded image with underlaid analyzed image.

Jimmy

-----Original Message-----
I have been using the Nifti toolbox for Matlab recently and was wondering whether it is possible to make an overlay of a thresholded image onto the analyzed image. In effect that would mean that all 0 values of the thresholded image should be transparent so that the underlying functional image can be seen.

Thanks a lot for your help in advance and for making such a useful tool available!

Yours sincerely,

Henry Lütcke

Biomedizinische NMR Forschungs GmbH

am Max-Planck Institut für Biophysikalische Chemie

Am Fassberg 11

37077 Göttingen

Germany

07 Mar 2006 Jimmy Shen

How to get original information from NIFTI hdr?

Hi Ged:

Thank you very much for your input.

Actually both "scl_slope" and "scl_inter" parameters have been correctly
read and applied to the "img" variable of the output NIFTI structure (see
"xform_nii.m" file): nii.img = scl_slope * double(nii.img) + hdr.dime.scl_inter;

The reason to display "hdr.dime.scl_slope" as zero is a feature of my
package. It signifies that the "img" variable has already been modified. You
can also find that "hdr.hist.qform_code" and "hdr.hist.sform_code" will be
set to zero after proper affine transformation.

The original value of "hdr" can be easily found by: hdr =
load_nii_hdr(filename); You should have correct value (0.003922) there.

Jimmy

-----Original Message-----

Hi Jimmy,

I've found your NiFTI toolbox very useful, but I'm having some
problems with some data stored in uint8 .nii with a scaling factor.
the data appears to load in as a binary image, and the value of
nii.hdr.dime.scl_slope is zero, instead of the correct value
(0.003922) which I can see using e.g. spm5 or avwhd from FSL.

Many thanks for any help you can offer,

Ged.

07 Mar 2006 Jimmy Shen

How scl_slope affect data_type?

Hi Ged:

Thanks for the URL you sent me. The way Jonas handling "scl_slope" is to convert image data type to "double", which makes sense, although it is not specified on NIFTI documentation (see URL below): http://nifti.nimh.nih.gov/nifti-1/documentation/nifti1fields/nifti1fields_pages/scl_slopeinter.html

I modified my "xform.m". So it will convert the image data to "double" in the case that "scl_slope" is used. I am updating this file to MATLAB Central File Exchange, and NIFTI_20060307 will appear there soon.

Thanks again for your valuable input,

Jimmy

-----Original Message-----

Hi Jimmy,

Thanks for getting back to me. Unfortunately something else must be wrong in that case, since the image in Matlab has only the values 0 or 1, while the image viewed in FSLview or SPM has a range of values between zero and 1, the image type is uint8.

Best,
Ged.

15 Mar 2006 Craig Hamilton

Very useful, well done. How about support for complex datatypes?

14 May 2006 r Huang

Hi Jimmy,

Your software is very useful. Many thanks for your great offer.

Could you please indicate (1) how to overlay 2 or 3 other images together, (2) how to choose different threshold?

Thanks in advance

rhuang

20 Sep 2006 jeff a

best utilities to read dynamic analyze files i have found

26 Dec 2006 abbas kader

A good toolbox for the viewing the Nifti formate file in MATLABe especillay when u did processing in FSL and you wnat to interpretted in MATLAB
Thanks alot

02 May 2007 Ning Cao

(Possible) Bug Report:

A good toolbox for loading, saving and viewing Analyze MR images. Thank you for improving it from time to time.

I just want to report a possible bug in 20070426 version.

When viewing a 4D image using view_nii, the "value at crosshair" and "value at cursor" always display the value of the first 3D volume.

view_nii.m L3036, the code 'imgvalue = double(img(sag,cor,axi));' making the imgvalue always the value of the first 3D volume.

26 Jul 2007 Stella zheng

The best tool I have used for Analyze image reading and writing. Thanks a lot for your contribution and sharing.

28 Jul 2007 Stella Zheng

Dear everybody:

I have found the followingproblems in the using this toolbox.
Data: t1_icbm_normal_1mm_pn0_rf0.rawb is downloaded from brainweb,then converted to Analyze format(t1_icbm_normal_1mm_pn0_rf0.img and t1_icbm_normal_1mm_pn0_rf0.hdr )(without *.mat).

Following procedures are applied:

nii_001 = load_nii('t1_icbm_normal_1mm_pn0_rf0', [], 1)
save_nii(nii_001,'001', 1);
nii_002 = load_nii('001', [], 1)

It is expected that the header information should be same, but there is following difference:

1. In nii_001, Originator in nii_001.original.hdr.hist is same as in nii.hdr.hist as [0 0 0 0 0].

2.In nii_002, Originator in nii_001.original.hdr.hist is [256 0 0 0 0], while it is [-2 -2 -2 0 0] in nii_001.hdr.hist.

3. The origin of t1_icbm_normal_1mm_pn0_rf0.img shown in MRIcro is [0 0 0], origin of 001.img shown in MRIcro is [2 2 2]

4. rot_orient=[],flip_orient=[] in nii_001.hdr.hist and nii_002.hdr.hist, but not in their origianl header.

5. when 001.img is converted to *.mgz in FreeSurfer, the error message is as below

mri_convert ./orig/001.img ./orig/001.mgz
reading from ./orig/001.img...
INFO: reading as a two-file NIFTI
nifti1Read(): unknown space units 0 in ./orig/001.hdr

Highly appreciate your help if somebody can help to solve the problem.

Best regards,
Stella,

SCE,Nanyang Technoligical Univeristy
Singapore,

28 Jul 2007 Stella Zheng

Sorry.

2.In nii_002, Originator in nii_002.original.hdr.hist is [256 0 0 0 0], while it is [-2 -2 -2 0 0] in nii_002.hdr.hist.

28 Jul 2007 Stella zheng

1. I have manually set the nii_01.hdr.dime.xyzt_units=2;It can be converted by freesurfer now. Thanks!
2. The other problems may be due to the orignal Analyze image is created with Big endian, while this toolbox writes the new Analyze file with little endian. I am not sure about it and how to solve it.

30 Jul 2007 Jimmy Shen

Stella,

Here are the answers:

1. It is correct that "nii_002.original.hdr.hist.originator" is [256 0 0 0 0], because "t1_icbm_normal_1mm_pn0_rf0" is in Analyze 7.5 format and "001" is in NIfTI format that does not have obvious "originator" field. please let me know if you are still not clear.

2. "nii_01.hdr.dime.xyzt_units" is just an information field. When it is 1, xyz are in "meters"; when it is 2, xyz are in "millimeters"; when it is 3, xyz are in "micrometers"; when it is 0, xyz units are unknown and can only be interpreted by the author. I don't know why this field helped you, and wonder if it is the correct way.

3. It is very strange that you got "[-2 -2 -2 0 0]" for "nii_002.hdr.hist.originator". I need to look at both "t1_icbm_normal_1mm_pn0_rf0.img/hdr" & "001.img/hdr" in order to further answer this question. Would you please upload them to your FTP site and send me the link?

31 Aug 2007 Mark Bolding

I use this all of the time. I rocks.

03 Sep 2007 Assaf RMATHALBazon

After a cursory inspection this seems like a great tool.

16 Jun 2008 Stefan Haufe  
24 Jun 2008 mmei rezk  
04 Jul 2008 so si  
08 Aug 2008 Marianna Jakab

Please take out the ":" of the link below, that takes you to an invalid page.

Two example datasets are provided under NIfTI web site http://nifti.nimh.nih.gov/nifti-1/data: "avg152T1_LR_nifti.nii" and "avg152T1_RL_nifti.nii".
whereas http://nifti.nimh.nih.gov/nifti-1/data is a very nice and informative page.
Thanks.

06 Oct 2008 Simon Robinson  
05 Dec 2008 Simon Robinson

These are great tools for reading and writing NIfTI - a core part of all my programs and very well supported. Thanks.

04 Mar 2009 Nicole

Hi Jimmy,

Your code has been very useful to me; I've been using make_nii and save_nii. My question is regarding the AC origin for make_nii. First of all, I presume the coordinates should be in mm, like the voxel size. Then, I was wondering if AC origin refers to the position of the central slice. If so, for the 3rd coordinate (referring to axial slices), does negative (e.g. -20) indicate an upward translation (i.e. more superior slice)? This is the convention that I'm used to, but I am not sure it applies to make_nii as well.

Thanks!

Nicole

05 Mar 2009 Jimmy Shen

Hi Nicole:

First: 'origin' parameter in 'make_nii.m' is in voxel, which is relative to the first voxel of 'img' parameter, i.e. img(1,1,1);

Second: It would be much clearer if I use an example to explain this:

img=125:249; img=reshape(img,[5,5,5]);
nii=make_nii(img,[1,1,1],[3,3,2]);
view_nii(nii);

Yes, we know AC Origin means [0 0 0] in millimeter. However, you must tell the NIfTI data where is your [0 0 0] millimeter, so other people can also know it when they open your NIfTI data. Therefore, parameter 'origin' can only be expressed in voxel to locate the exact position of your 3D matrix to be the AC Origin.

Jimmy

05 Mar 2009 Nicole

Hi Jimmy,

I'm sorry to repeat my question, but the entries of AC origin are still not clear to me, mainly because I don't know what is meant by "AC origin".

I now know that voxels are being counted (not the position in mm). Now, is it the origin in question related to the x, y, z offset of the slab of slices in the brain (i.e. the offset when positioning the slices during the scan), or is it the origin within the slab of slices itself? I presume it is the former.

If it is the former, then I'm not sure why it has to be relative to img(1,1,1), as opposed to the overall offset of the slab. I have my offset in mm in the readout, phase encode, and slice directions. Can I just convert these values to numbers of voxels?

Finally, is the z coordinate negative upwards, or negative downwards? (i.e. a more superior slice would have a larger negative z coordinate than a more inferior slice)

As a side note, I'm only using make_nii and save_nii, to then analyze the data in FSL.

Hopefully I've made my confusion clearer this time. Thanks for your help,

Nicole

05 Mar 2009 Jimmy Shen

Hi Nicole:

Here's the definition of AC Origin:
http://en.wikipedia.org/wiki/Anterior_commissure

In order to use 'make_nii.m', you already have a 3D matrix, you can only tell the AC Origin by your eye, instead of any conversion.

Because the data generated by 'make_nii.m' is in RAS orientation, the negative z coordinate is downwards, i.e. z coordinates is from Inferior to Superior.

Jimmy

12 Jun 2009 Maddy

Hi Jimmy,

Wonderful piece of work and a very useful one at that! I would be pleased to know if I can use it for my specific case.

 A rod-like observation chamber that we have inserted in the skull shows up as a circle in some of the initial slices of MRI. What I want to do is to project the exact outline of the circle onto a few other deeper slices, so that it can be found, where exactly the projection of the chamber lies in the successive slices. Thereby, the region that would be encircled if the rod-like chamber were to be inserted further inside can also be found. Simply put, it is like overlaying a circle of specified diameter and position onto each successive slice.

I tried doing it with the view_nii function with the circle saved in .jpeg/.fig formats, giving the figure handle and the nii, using the description in the help section of the view function. But I had trouble with the figure properties, especially the function ‘set’. I get errors in almost every figure property, starting from ‘menu bar’ at line number 291 in the program. I am a beginner in matlab. I was actually surprised I could use other functions, which is why I say that your file is so user-friendly. I would be happy to know how I can go about doing it…

To sum up, all I want to know is, if there is a way of plotting a circle,( given its radius and position in those co-ordinates), onto successive slices.

12 Jun 2009 Jimmy Shen

Hi Maddy:

Yes, it is easy to do so with my tools. Here’s the steps:

1. Load nii file;
2. Find an unused intensity level. If not available, add another level;
3. Find the index of the circle and circles in successive slices;
4. Mark them with the value that you choose in Step 2;
5. Create a colormap file;
6. View brain with circles;

If you are still having problem to create circles, please upload the 2 nii file to your ftp server. The first nii file should be the original volume, the second one should be the one with initial circle. Please also tell me exactly what slices do you want the circle to be drawn, and what color do you want the circle to be drawn. I will then reply you with the modified nii containing the circles.

Jimmy

30 Jun 2009 Kris Thielemans

Hi Jimmy

I have a question regarding the license. The matlab front-end says this is BSD license, and so does your license.txt. However, a few files (including load_nii.m and save_nii.m) contain the lines

% Part of this file is copied and modified under GNU license from
% MRI_TOOLBOX developed by CNSP in Flinders University, Australia

It seems to me that those files should then by distributed under the GNU license as well. Could you please clarify that? Sorry for the trouble.

Kris

30 Jun 2009 Kris Thielemans

Hi Jimmy

there seems to be a problem in view_nii when using data that is (natively?) in single format. When I switch to "interp off" and click in one of the panels to move the cross-hair, the other panels get blank, and I see "Warning: CData must be double or uint8.." in my matlab prompt. I can fix this problem by doing something like:

nii.img=double(nii.img);

No other problems so far, so many thanks for the nice software!

Kris

02 Jul 2009 Pieter Vandemaele

Hi Jimmy,

I got some problems using the headers with your nice tool.

When I load a 4-D nifti file header with load_nii_hdr and compare it with the header from the .hdr field using load_nii, I get different pixdim values:
load_nii_hdr:

nifti_hdr = load_nii_hdr('nifti_file');
nifti_image = load_nii('nifti_file');

nifti_hdr.dime.pixdim is [1 4.5313 4.5313 4.5000 200 1 1 1]
nifti_image.hdr.dime.pixdim is [1 4.5212 4.5312 4.4900 200 1 1 1]

The difference of the pixdim arrays is [0 0.01 0 0.01 0 0 0 0]

Did I found a bug or is there a bug in my head?

Pieter

03 Jul 2009 Pieter Vandemaele

Hi Jimmy,

You can ignore my previous question, I did read the FAQ on you site...
I hope you will have time to update/expand the program, it is very usefull.

Pieter

03 Jul 2009 Jimmy Shen

Hi Pieter:

It is because you took the advantage of my default "tolerance", which allows 10% distortion. You can use my "reslice.m" program, and specify the voxel size to exactly what you want.

Jiimmy

03 Jul 2009 Jimmy Shen

Hi Kris:

Thanks for your feedback. I have fixed the problem. Instead if using nii.img=double(nii.img); I only made double the three 2D images that are displayed. If the original type is uint8, I will leave it as is.

Again, thanks for the feedback. I have just uploaded the new programs, and you can see it here next week.

Jimmy

07 Aug 2009 prateep mukherjee

Hi Jimmy,

How can we find the maximum and minimum depth,width and height in a 3d .nii image using your tool , assuming that the image is in RAS orientation ?

Also, using this tool, can we find the distance of a pixel from the origin? Please give some hint in this regard.

 The images I am using right now are "avg152T1_RL_nifti.nii" and "avg152T1_LR_nifti.nii" from http://nifti.nimh.nih.gov/nifti-1/data.

Thanks,
Prateep.

07 Aug 2009 Jimmy Shen

Hi Prateep:

when you load an image file, e.g.:
nii = load_nii('yourimg.nii')

nii.hdr.dime.pixdim(2:4) will be the width, height, and depth of a voxel.

If you view it with view_nii(nii), click menu "View" and then click "Image Information", the "voxel size" is the width, height, and depth of a voxel.

Since both Analyze & NIfTI image should have the same voxel size for all voxels in an image, the maximum and minimum size should be the same.

The distance in voxel from origin can be obtained by subtract value at "[XYZ] at origin" with value at "[XYZ] at crosshair". For distance in millimeter from origin, since value at "[XYZ] at origin" is [0 0 0], you can just read value at "[XYZ] at crosshair".

Please feel free to ask more questions,

Jimmy

16 Sep 2009 M Var

Hi.

The programs are great, but I seem to have a problem creating NIFTI files with this program. The created NIFTI files often are completely blank with only the highest value pixel set to 1. This can be sorted by changing the scale of the inout matrix (i.e. multiplying all the values by 100), but it is hard to determine what the right scaling is for all images when I don't know what the mechanism behind this error is.

Do you have an ideas that may help?

Many thanks,
Marta

16 Sep 2009 Jimmy Shen

Hi Marta:

How did you create your NIfTI file? Here's an example that is reproducable:

nii=make_nii(rand(91,109,91)*255);
max(nii.img(:))
save_nii(nii,'mynifti.nii')

As you can see, the highest value of voxel is never set to 1, and you can open 'mynifti.nii' with any NIfTI compatible tools.

Please feel free to ask any questions.

Jimmy

28 Oct 2009 Wen-Tung Wang

Hi, Jimmy:
I tried to save a comlpex 2D matrix (an MRI image) in Analyze format using your scripts:
datatype=32;
ana = make_ans(myMat,[],[],datatype);
save_untouch_nii(ana,'C:\Data\test');
When trying to read the test.img using ImageJ, it gave error message "Data type 32 not supported".
Is it that I didn't use the scripts correctly?
Thank you.
Wen-Tung

29 Oct 2009 Jimmy Shen

Hi Wen-Tung:

First, thank you for the rating.

Your script is correct, but I didn't get any error message. You said " ... trying to read the test.img using ImageJ, ...". Is it a typo?

Here's an example that I tested, and you can duplicate the process:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
img1=rand(3,3)
img2=rand(3,3)
myMat=complex(img1,img2)
datatype=32;
ana = make_ana(myMat,[],[],datatype);
save_untouch_nii(ana,'C:\Data\test');
ana=load_untouch_nii('C:\Data\test')
nii=load_nii('C:\Data\test')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Did you get error message when testing the above script?

Jimmy

30 Oct 2009 Wen-Tung Wang

Thank you for the note, Jimmy.
First, sorry that I didn't write it clearer in my previous posting. ImageJ is a freeware from NIH (http://rsbweb.nih.gov/ij/index.html) that have numerous users in medical imaging community. ImageJ has a plugin (http://rsbweb.nih.gov/ij/plugins/index.html) that can open/save image in Analyze format. I was trying to save an MRI image, after some processing using Matlab scripts, in Analyze format and view it using ImageJ.
I tried your very nice example, it works well. Yet, it is still not clear to me why ImageJ cannot read the output file. Using datatype=64, ImageJ can read the file, but this is not the correct datatype and there are sinusoidal modulations across the image.
Thank you.
Wen-Tung

30 Oct 2009 Alex

Dear Jimmy,

I would like to create large NIfTI files using a computer with relatively small memory. It seems I could do this by processing several slices at a time, and then reprocessing with collapse_nii_scan. This seems a bit clumsy and I guess it would also believe that collapsed parts were a time series rather than stitching together one large volume? Is there a tool to stream slices into a NIfTI file, supposing that I can provide some header info from the beginning?

Thanks,
Alex.

31 Oct 2009 Jimmy Shen

Hi Wen-Tung:

The problem is probably in a module that is used by ImageJ. I tried MRIcron, and got the same result as you described. i.e. when hdr.dime.datatype=32 & hdr.dime.bitpix=64, it does not support. when hdr.dime.datatype=64 & hdr.dime.bitpix=128, it works.

In order to make sure what datatype ANALYZE format is supposed to support, I double checked the Analyze document:
http://eeg.sourceforge.net/ANALYZE75.pdf
It says that Analyze format should only support 32/64, rather than 64/128.

I also tried other popular software like AFNI. It turns out that 32/64 is supported by AFNI. When I tried 64/128, it complains "Unsupported ANALYZE datatype =64", which is correct.

Therefore, if you have to use datatype=32 complex, you have to find correct software to use. AFNI is one of them that I have just tested.

All the best!
Jimmy

31 Oct 2009 Jimmy Shen

Hi Alex:

Theoretically, with the header information in hand, it is feasible to do slice collapse/expand in addition to scan collapse/expand. Currently, I didn't find such a tool, but may consider your suggestion in the future.

Thanks for your feedback.
Jimmy

06 Nov 2009 Francesca

Dear Jimmy,

i'm just a beginner with all this "nifti imaging world" but I've already done enough for understanding that you've done a very great and smart job!thanks!

Well, i have a problem with loading a .nii file. (obtained by exporting a .ROI file with MRIcro). I'll post the error that i get:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>> ROI=load_nii('lCC_19850624TRTN_2x2x2_b1000_a_fa.nii');
??? Error using ==> reshape
To RESHAPE the number of elements must not change.

Error in ==> load_nii_img>read_image at 266
      img = squeeze(reshape(img, [hdr.dime.dim(2:4) length(img_idx)]));

Error in ==> load_nii_img at 62
   [img,hdr] = read_image(hdr,filetype,fileprefix,machine,img_idx,old_RGB);

Error in ==> load_nii at 66
   [nii.img,nii.hdr] = ...

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Could you help me?

Thanks a lot and all the best!

francesca

06 Nov 2009 Jimmy Shen

Hi Francesca:

First, thank you very much for the rating, and I really appreciate it.

It seems that "lCC_19850624TRTN_2x2x2_b1000_a_fa.nii'" is corrupted. Have you tried to open it successfully under MRIcro? If so, please upload this file and let me take a look at it?

In addition, from the error message that you posted, it seems that you are using a very old version of my tools. I noticed this by the line number showing the error. However, I still believe that the error is caused by the corrupted image.

Please let me know,
Jimmy

06 Nov 2009 Felipe Salinas

Sorry for not rating this sooner....

08 Nov 2009 Wei Liu

Hi Jimmy,

When reading a nifti file I found the voxel gray values are different with other tools. What I did is:
- 'untouch load' the file: fmri_mean = load_untouch_nii(.../meanfM00223_004');
- print voxel value of mri_mean.img(32,32,23). The value is 9506.
- But when I open the same file with fslview, I found the same voxel has different value. And other voxels are also different. the fslview snapshot is attached. Because in fslview the voxel coordinates begins from 0 to dim-1, the same voxel in fslview has coordinate [31 31 22].

- Use the application 'Imageviewer' from itk, and found the voxel value is same with fslview. Also attached snapshot. Same to fslview the coordinate are [31 31 22] for the same voxel.

- check the header of the nifti file by fsl tool 'fslhd':
datatype 4
nbyper 2
bitpix 16

That means it's int16.

-Check the fmri_mean structure in matlab, and found the data type is 4:
       datatype: 4
            bitpix: 16

But why did your nifti tool give different gray value with other packages? I put all the snapshot and the nifti files at www.sci.utah.edu/~weiliu/q01/ for your reference.

By the way this is excellent tool. Thanks!
Wei

08 Nov 2009 Wei Liu

OK I think I found the parameter for scaling: they are hdr.dime.scl_slope and hdr.dime.scl_inter. Now I know how these packages works, and I can scale the voxel intensity myself if I want. Sorry for the cursory questions, and thanks for the good tool! -Wei

09 Nov 2009 Francesca

Hi Jimmy,

you're right, I've tried to open the .nii file with MRIcro and an error about expected header dimensions appears. The wrog step is probably when i try to export .roi to .nii, but actually there's no other way to do it with MRIcro. I'll try to find some other way!

Thanks a lot for your help and thanks again for your nifti tool!

francesca

09 Nov 2009 Jimmy Shen

Hi Wei:

Thanks for the rate.

When you use "load_untouch_nii.m", it just load the img and hdr, but does not apply any changes (e.g. scl_slope, affine matrix, etc) indicated in the header. Therefore, I always suggest people to use "load_nii.m" in most cases. Please check the description of "load_untouch_nii.m" & "load_nii.m" on http://www.rotman-baycrest.on.ca/~jimmy/NIfTI.

You are right, the unexpected value was caused by the "scl_slope" parameter, which is properly applied when you use "load_nii.m". However, I am not sure whether you have noticed that this is a radiological image, i.e. Right on LHS and Left on RHS. Here, L/RHS means Left/RightHandSide.

When you use "load_nii.m", it will do all the proper flipping and rotation according to the affine matrix in the header, and the img will always be in RAS orientation (i.e. x: L->R, y: P->A, z: I->S). In your particular case, instead of use [32 32 23], you should use [33 32 23], since x axis is flipped, i.e. new_x = max_x(64) - old_x(32) + 1 = 33.

Other software may choose to do so with proper labeling around Axial / Sagittal / Coronal view, e.g. FSL does so.

Please feel free to let me know if you have further question.

Jimmy

10 Nov 2009 Michael Zeineh

Hi Francesca and Jimmy,

Funny, I have run into the same problem with MRIcro. I found a workaround. If with MRIcro you save the roi as a separate analyze header (rather than the embedded header), then it will open with MRIcro. However, this will still not open with Jimmy's tools. However, if you rename the .img file to .nii, then both Jimmy's tool will load it.

Jimmy, I am happy to email some limited sample images if you want.

Michael

10 Nov 2009 Jimmy Shen

Hi Michael:

First, I am wondering how you could simply rename an .img file to a .nii file. NIfTI supports both extensions, but for .img extension, you need to save the header separately in .hdr file. So there is no way for people to just "... rename the .img file to .nii file ..." like you said.

Second, I looked into Francesca's data, and the size of the image data does not match the size that is indicated in the header. For such a file, I cannot image any software can process it.

I am glad to take a look at your samples. Would you please upload them onto your server, and post the link here?

Thanks,
Jimmy

24 Nov 2009 David

This is a fantastic and highly useful piece of software.

I'm just wondering if there is anyway to load just one or more specific slices from analyze and nifti formats? I know it is straightforward to select the desired slices from nii.img after load, but I would like to avoid the memory usage with the full 4D, 5D,tsc load.

Thank you for sharing this work Jimmy

06 Jan 2010 Jimmy Shen

Hi David:

I just got chance to look into this issue.

Since it is too complicated to modify "load_nii.m" to load specific slices of NIfTI and Analyze formats, I modified "load_untouch_nii.m" only. That means, you will take care of image orientation based on its header information, because "load_untouch_nii.m" does not apply any changes to the IMG matrix that is indicated in the HDR.

I hope this could still somehow help you to load large dataset.

Jimmy

25 Jan 2010 Anne-Lene

Hi!

I am a beginner in using nifti so this may seem like a stupid question, but i hope you can help me.

I have used fsl prelude to unwrap my mri images, and they are now in the nifti format. I now need to compare these to images with images that I have unwrapped with a matlab algoritm that i made myself.

Can i use your tools to look at the nifti images separatley and perhaps convert them to another datatype that i can use in matlab?

I tought maybe your tip in the help save_nii could help me, but i don't see how to use it, do i need img/hdr files instead of nii?

I will be very greatful for some guidance.

Anne-Lene

25 Jan 2010 Jimmy Shen

Hi Anne-Lene:

No questions are stupid.

Yes. You can use my tool to look at NIfTI images separately and also convert them to another datatype.

Since NIfTI supports both img/hdr and nii extensions, so my tool supports any of them.

Most help can be obtained by using help command. e.g. help make_nii.

There are several other resources that you can get help:
http://www.rotman-baycrest.on.ca/~jimmy/NIfTI
http://www.rotman-baycrest.on.ca/~jimmy/NIfTI/FAQ.htm
http://www.rotman-baycrest.on.ca/~jimmy/NIfTI/examples.txt

Command "expand_nii_scan" is used to break a multiple-scan NIfTI file into multiple single-scan NIfTI files, so you can look at them separately.

If you want to convert img data type, you can use "save_nii" after use "make_nii" command. If you are familiar with NIfTI structure, you can modify "hdr.dime" directly, and then use "save_nii" to save.

If you have further question, please make it more specific, so I can help you.

Jimmy

26 Jan 2010 Anne-Lene

I have looked at those links and tried expand_nii_scan, and I'm afraid I still don't understand. My file has the nii extension and when I use expand I get another nii file, I don't see the difference between that and my original file, when I use view_nii.

I saw that using make_nii I could convert images with the img/hdr extension, but I couldn't get it to work on my file with nii extension.

Anne-Lene

26 Jan 2010 Jimmy Shen

Hi Anne-Lene:

Don't worry. The following tips will give you some more help.

If you did not see difference after using "expend_nii_scan', it means that you only have 1 scan (1 volume) in your file. Since you said "...look at the nifti images separatley...", I thought you have several images (volumes or scans) in 1 file, and would like to treat them separately.

The first step you need to do is to load your image into MATLAB by "load_nii" command (Please look at "help load_nii" before using it).

Let's say you have an image "file1.nii" (or "file1.img/hdr"). By command:
nii = load_nii('file1.nii');
or
nii = load_nii('file1.img');
You can get a 3D matrix in "nii.img", and its header information in "nii.hdr".

You mentioned that you would like to change the image into another datatype. Here's how:

You can either change the header information directly (if you are familiar with the NIfTI header) then use "save_nii", or to use "make_nii" to create a new NIfTI structure (like nii).

For example:
datatype = 16; % change datatype to float32
nii2 = make_nii(nii.img, nii.hdr.dime.pixdim(2:4), nii.hdr.hist.originator(1:3), datatype);
save_nii(nii2, 'file2.nii'); % either to use nii ext
save_nii(nii2, 'file2.img'); % or to use img/hdr ext
By doing this, you changed it to Float32 data type, and saved it into "file2.nii" (or "file2.img/hdr").

If you have a MATLAB algorithm that you made by yourself as you mentioned, I think you will be more interested in the img matrix ("nii.img"). The "nii.hdr" is the header information to describe how "nii.img" is oriented, what is the voxel size, and originator, and many more.

Finally, I have to correct you that "make_nii" will not convert image to any extension. It only makes a NIfTI structure from N-D matrix and some other parameters (voxel size, originator, data type, etc.). Please double check the help, and make sure that you know how to use it.

If you have further question, please be more specific (e.g. make a point list), so I know how to help you.

Jimmy

Please login to add a comment or rating.
Updates
09 Nov 2005

Images whose cardinal planes are slightly off Cartesian coordinates will also be loaded. See detail in 'xform_nii.m'.

11 Nov 2005

I didn't see the 'xform_nii.m' was updated in File Exchange. So, I uploaded again with another zip filename.

17 Nov 2005

It now supports both new_RGB24 (RGB triple) and old_RGB24 (used by Analyze 6.0 from AnalyzeDirect Inc). Help file "view_nii_hlp.txt" is also modified.

17 Nov 2005

It now supports both new_RGB (RGB triple) & old_RGB (used by Analyze 6.0 from AnalyzeDirect Inc). Help file "view_nii_hlp.txt" is also modified.

22 Nov 2005

Updated help file "view_nii_hlp.txt"

22 Nov 2005

Updated help file "view_nii_hlp.txt".

22 Nov 2005

Menu bar is added to convert image to standard RAS orientation and to display image information.

28 Nov 2005

Modified display format for RGB24 data value.

28 Nov 2005

Updated code

29 Nov 2005

NIFTI_20051128 is tested from MATLAB version R11 to R14 on both PC & Linux

15 Dec 2005

1. fix a bug for the initial display of coronal & sagittal view
2. added a function to expand a multiple-scan NIFTI file into multiple single-scan NIFTI files

24 Feb 2006

Fixed a bug to reorder "pixdim" field when performing sform/qform transform. Thanks to Jeff Gunter from Mayo Clinic who reported this bug.

27 Feb 2006

1. Fixed a bug to reorder "pixdim" and "originator" field for some NIFTI data. 2. Added "rot_orient" and "flip_orient" fields under "hdr.hist" if package make conversion of the NIFTI data into RAS orientation system.

01 Mar 2006

set "rot_orient" and "flip_orient" fields under "hdr.hist" to empty for old ANALYZE 7.5 format or if the NIFTI data is already in RAS orientation.

08 Mar 2006

Image data type will be converted to "double" in the case that "scl_slope" is used.

13 Mar 2006

"nii.hdr.scl_slope" will take "avw.hdr.roi_scale" value, which is a non-ANALYZE7.5 field but exclusively used by SPM.

23 Mar 2006

It now supports both COMPLEX64 and COMPLEX128 data type.

23 Mar 2006

It now supports both COMPLEX64 and COMPLEX128 data type.

20 Apr 2006

Size of some image data is incorrectly larger than what specified in "hdr.dime.dim" field. So it caused problem to load image data. Image data is now always loaded according to the size that is specified in its header.

06 Jun 2006

1. There was a bug in converting some permuted images to RAS orientation. It has been fixed now. Thanks to Jeff Gunter from Mayo Clinic who reported this bug.

2. hdr.dime.glmax and glmin are updated if hdr.dime.scl_slope is non-zero.

28 Jun 2006

1. "unxform_nii" from Jeff is included to provide an option of putting image orientation back to its original status. 2. "expand_nii_scan" is modified so the expanded image files will be appended with a 4-digit number.

05 Jul 2006

Thanks to Michael Harms from WUSTL who reported and fixed some bugs in loading and making RGB data. He also added codes to save AnalyzeDirect compatible RGB data.

27 Jul 2006

Due to round-off of quaternions (quatern_b/c/d), sqrt(1.0-(b*b+c*c+d*d)) may become complex because value inside sqrt may go below 0 (e.g. -1e-6). Instead of complex, now 0 will be used in this case. Thanks to Jeff Gunter who reported this issue.

25 Sep 2006

Added "mat_into_hdr.m" script to convert the ANALYZE 7.5 SPM Reoriented image file into NIfTI format, and integrate the affine matrix in its matlab file into its header file.

18 Dec 2006

Improved data scaling process, and fixed a bug in calculating origin position. Detail will be posted on http://www.rotman-baycrest.on.ca/~jimmy/NIFTI

21 Dec 2006

1. "Save displayed image as ..." is added in the "File" menu of "view_nii" window. 2. Removed integer rounding for "hdr.hist.originator". Please check more detail in: http://www.rotman-baycrest.on.ca/~jimmy/NIFTI/#update.

23 Apr 2007

1. Automatically integrate affine matrix in .mat file of earlier versions of SPM into .hdr structure. 2. fixed a bug in "mat_into_hdr.m". Please check more detail in: http://www.rotman-baycrest.on.ca/~jimmy/NIFTI/#update

27 Apr 2007

1. Modified save_nii command, so earlier versions of SPM can also open it with correct originator. 2. Modified http://www.rotman-baycrest.on.ca/~jimmy/NIFTI/FAQ.htm with several categories

03 May 2007

Thanks to Ning Cao from UKY who reported and fixed a bug in view_nii.m file. Please check more detail at: http://www.rotman-baycrest.on.ca/~jimmy/NIFTI/#update

04 Feb 2008

Thanks to Jeff Gunter from Mayo who suggested to display a warning message instead of throwing an error for a very oblique image. Especially thanks to his code of checking qform rotation before applying voxel size modulation.

09 Apr 2008

Added a new feature to load and save NIfTI's header extension (added "load_nii_ext.m" and moified "save_nii.m"), and fixed a bug when loading qform data, which was introduced on 01-FEB-2008.

09 Jul 2008

Thanks to the feedback of Holger Krause from Heinrich-Heine University Dusseldorf, the bug in "mat_into_hdr.m" when input file containing a path has been fixed.

20 Aug 2008

1. New program reslice_nii.m is provided to perform any 3D affine transform and to generate an isotropic image. 2. New program flip_lr.m is provided for L-R flipping. Please check more detail at: http://www.rotman-baycrest.on.ca/~jimmy/NIFTI/#update

22 Aug 2008

Added a "verbose" parameter for "reslice_nii.m" to provide an opportunity to disable the progress display.

29 Aug 2008

Added "load_untouch_nii.m" and "save_untouch_nii.m" program pair. See detail in Description above and FAQ at http://www.rotman-baycrest.on.ca/~jimmy/NIfTI/FAQ.htm.

05 Sep 2008

Extended Load / Save / Make programs to N-Dimension, where N can be from 3 to 7 for NIfTI data and from 3 to 4 for ANALYZE data.

23 Oct 2008

1. Properly handle fread/'*char' problem with foreign characters in MATLAB 7 and above; 2. Reordered input parameters for 'reslice_nii.m' program;

24 Oct 2008

update didn't show up

04 Dec 2008

Fixed a bug in fliplr.m when reading files.

05 Dec 2008

Default data type for 'make_nii.m' now follows the data type of 'img' matrix, instead of float32.

17 Mar 2009

1. Thanks to the feedback of Bryce Wilkins from USC, a typo in one of my message display is fixed.

2. Thanks to the suggestion of Ashish Raj from Cornell, 'expand_nii_scan.m' is modified and 'collapse_nii_scan.m' is added.

25 Mar 2009

Thanks to the feedback of Kate Fissell from University of Pittsburgh, transformation matrix M will no longer be saved by save_untouch_nii.m.

18 Jun 2009

Just receive warning from MATLAB: "If I do not license my code with BSD, my code will be removed."

03 Jul 2009

Fixed several bugs, and posted informative table:

http://www.rotman-baycrest.on.ca/~jimmy/UseANALYZE.htm

09 Sep 2009

While an N-Dimensional matrix can be saved into a NIfTI file using "make_nii / save_nii" command pair, a 3D matrix can also be saved into an ANALYZE file using "make_ana / save_untouch_nii" in order to be compatible with some ANALYZE only programs.

05 Jan 2010

"load_untouch_nii" now supports loading specific slices of NIfTI or Analyze format.

06 Jan 2010

1. "load_untouch_nii" now supports loading specific slices of NIfTI or Analyze format.
2. MATLAB ask me to remove any "under GNU license" phrase in any file.

Tag Activity for this File
Tag Applied By Date/Time
medical Jimmy Shen 22 Oct 2008 08:03:56
reslice Jimmy Shen 22 Oct 2008 08:03:56
isotropic Jimmy Shen 22 Oct 2008 08:03:56
image Jimmy Shen 22 Oct 2008 08:03:56
header Jimmy Shen 22 Oct 2008 08:03:56
niftl Jimmy Shen 22 Oct 2008 08:03:56
analyze Jimmy Shen 22 Oct 2008 08:03:56
ras Jimmy Shen 22 Oct 2008 08:03:56
flip Jimmy Shen 22 Oct 2008 08:03:56
las Jimmy Shen 22 Oct 2008 08:03:56
analyze Cristina McIntire 05 Dec 2008 14:32:46
image Cristina McIntire 05 Dec 2008 14:32:46
medical Cristina McIntire 05 Dec 2008 14:32:46
image Jayant Mishra 11 Aug 2009 05:21:44
analyze Terry 14 Dec 2009 19:21:17
nifti Terry 14 Dec 2009 19:21:17
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com