MATLAB: Normalize the intensity of the data within an ROI to a zero-mean, unit-variance space

MATLABmatlab functionmedical imagingneuroimaging

Hi,
I have nifti neuroimaging data that I want to normalise within an ROI to a zero-mean, unit-variance space.
I tried using the code below however kept on getting an error;
ROI = niftiread('CHB_train_Case01_T1_brain_mask.nii');
Raw_brain = niftiread('CHB_train_Case01_T1.nii');
ind = find(ROI==1);
mean_val = mean(Raw_brain(ind));
std_val = std(Raw_brain(ind));
Raw_brain(ind)= Raw_brain(ind)-mean_val;
Raw_brain(ind)= (Raw_brain(ind)-mean_val)/std_val;
niftiwrite(Raw_brain,'Raw_brain_normalised_T1.nii');
the error I seem to be getting is as follows;
Error using var (line 74)
Invalid data type. First input argument must be single or double.
Error in std (line 59)
y = sqrt(var(varargin{:}));
Error in untitled8 (line 5)
std_val = std(Raw_brain(ind));
Would appreciate any guidance/help
Thanks

Best Answer

The error is due to the conflict with data type. Input argument of the std function must be of data type single or double. As per my knowledge the output data type of the niftiread would be unsigned integer (I think unit8 specifically).
Try with the following code:
ROI = niftiread('CHB_train_Case01_T1_brain_mask.nii');
Raw_brain = niftiread('CHB_train_Case01_T1.nii');
ind = find(ROI==1);
mean_val = mean(Raw_brain(ind));
std_val = std(double(Raw_brain(ind)));
Related Question