Skip to content

Commit 01bb596

Browse files
committed
Apply voxel intensity scaling to .nii loaded untouched
NiftiMod is using load_untouch_nii for some time leavign the data untransformed. However the voxel intensities should be scaled
1 parent 347e558 commit 01bb596

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

lib/NiftiOOP/NiftiMod.m

+4-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@
8585
function [image, header] = load(this)
8686
if( ~this.isLoaded && ~strcmp(this.filepath,'')) % it is not already isLoaded and we were not called with empty string (by Matlab callingn our get functions without command after initiating!)
8787
disp(['Loading ' this.filepath ' from disk...']);
88-
% nifti = load_nii(this.filepath, [], [], [], [], [], 0.1, 'q'); % load with 0.1 tolerance, prefer the q-form over the sform!
89-
nifti = load_untouch_nii(this.filepath); % load with 0.1 tolerance, prefer the q-form over the sform!
88+
%nifti = load_nii(this.filepath, [], [], [], [], [], 0.1, 'q'); % load with 0.1 tolerance, prefer the q-form over the sform!
89+
nifti = load_untouch_nii(this.filepath); % load untouched, i.e. keep orginial data orientation (dont apply any transform)
90+
nifti = applyNiiIntensityScaling(nifti); %however, the voxel intesities should be scaled correctly now here (as they
91+
%would by load_nii otherewise)
9092

9193
this.header = nifti.hdr; % header should maybe kept all the time ..
9294

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function nii = applyNiiIntensityScaling(nii)
2+
% Code extracte from Jimmy Shen's (jimmy@rotman-baycrest.on.ca) xform_nii.m
3+
% method, which is part of the nifti toolbox
4+
%
5+
% Andreas Husch, 2019
6+
7+
% if scl_slope field is nonzero, then each voxel value in the
8+
% dataset should be scaled as: y = scl_slope * x + scl_inter
9+
% I bring it here because hdr will be modified by change_hdr.
10+
%
11+
if nii.hdr.dime.scl_slope ~= 0 & ...
12+
ismember(nii.hdr.dime.datatype, [2,4,8,16,64,256,512,768]) & ...
13+
(nii.hdr.dime.scl_slope ~= 1 | nii.hdr.dime.scl_inter ~= 0)
14+
15+
nii.img = ...
16+
nii.hdr.dime.scl_slope * double(nii.img) + nii.hdr.dime.scl_inter;
17+
18+
if nii.hdr.dime.datatype == 64
19+
20+
nii.hdr.dime.datatype = 64;
21+
nii.hdr.dime.bitpix = 64;
22+
else
23+
nii.img = single(nii.img);
24+
25+
nii.hdr.dime.datatype = 16;
26+
nii.hdr.dime.bitpix = 32;
27+
end
28+
29+
nii.hdr.dime.glmax = max(double(nii.img(:)));
30+
nii.hdr.dime.glmin = min(double(nii.img(:)));
31+
32+
% set scale to non-use, because it is applied in xform_nii
33+
%
34+
nii.hdr.dime.scl_slope = 0;
35+
36+
end
37+
38+
% However, the scaling is to be ignored if datatype is DT_RGB24.
39+
40+
% If datatype is a complex type, then the scaling is to be applied
41+
% to both the real and imaginary parts.
42+
%
43+
if nii.hdr.dime.scl_slope ~= 0 & ...
44+
ismember(nii.hdr.dime.datatype, [32,1792])
45+
46+
nii.img = ...
47+
nii.hdr.dime.scl_slope * double(nii.img) + nii.hdr.dime.scl_inter;
48+
49+
if nii.hdr.dime.datatype == 32
50+
nii.img = single(nii.img);
51+
end
52+
53+
nii.hdr.dime.glmax = max(double(nii.img(:)));
54+
nii.hdr.dime.glmin = min(double(nii.img(:)));
55+
56+
% set scale to non-use, because it is applied in xform_nii
57+
%
58+
nii.hdr.dime.scl_slope = 0;
59+
60+
end
61+
end

0 commit comments

Comments
 (0)