Niftiinfo do not keep negative values

When I'm importing nifti files using niftiinfo and niftiread, only the positive values from the metadata remains. The original files contain both positive and negative values. I have read the documentation and tried to change the cal_max and cal_min field, but this doesn't solve it.
Any ideas how to fix this? Are there any other functions to load niftifiles in matlab?
Thanks!

7 Comments

Do you have a sample file we could test with?
I am unable to upload a nifti file, they don't accept .nii. My code is below, maybe I have made a mistake.
clc;
infoPhi = niftiinfo('B0mapSophie11.nii'); % Reads the header of the Nifti file
infoPhi.raw.cal_max = 4096; infoPhi.raw.cal_min = -4096; % Change range of intensity values
phi1 = niftiread(infoPhi);
phi = zeros(100, 160, 20);
phi(:,:,:) = permute(phi1( 61:160, 180:-1:21, :), [1 2 3]);
How did you try to upload the file here? Don't use the image icon, but use the paper clip icon (which should work for any file type, unless it is too large). If that doesn't work, maybe try to zip it first?
Did try it zipped and unzipped. I get the same message.
It did work now, sorry. I had to rename it without the .nii.
My code is below
infoPhi = niftiinfo('B0mapSophie11.nii'); % Reads the header of the Nifti file
infoPhi.raw.cal_max = 4096; infoPhi.raw.cal_min = -4096;
phi1 = niftiread(infoPhi);
phi = zeros(100, 160, 20);
phi(:,:,:) = permute(phi1( 61:160, 180:-1:21, :), [1 2 3]);
Thanks for the explanation, but I still don't understand why the negative values dissapear. In the original dataset, the intensity range is [-4096, 4096] and after importing they are [0, 4096].
I tried setting scl_inter = 0 and scl_slope = 1, but nothing changed.

Sign in to comment.

 Accepted Answer

I stepped through the code that reads the image. The data as it is stored in the file has no negative values.
The only items that the code uses from the headers include:
  • offset from beginning of file to data
  • byte order
  • image size
  • image data type
The code does not perform any data scaling or any min() or max() or anything like that: the values you get from the reading function are what is stored in the file.
You yourself should probably be applying the MultiplicativeScaling: 2 and AdditiveOffset: -4096 -- that would get you values in the range -4096 to +4094

2 Comments

Ok. I'm still a bit confused how the function does not change the data. When I open the file in other software (fsleyes and ITK-SNAP) the grey values are in the range of -4096 to 4096. But this is not the case after importing them.
I scaled the values so now I have the preferred intensity range.
Thank you for your explanation and time!
ITK-SNAP and fsleyes are probably applying the scaling and offset; MATLAB is just returning the raw data.

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!