How to combine multiple ROI masks (.nii files) into one .nii file

46 vues (au cours des 30 derniers jours)
Chia-Yueh Chang
Chia-Yueh Chang le 14 Avr 2023
HI
How to combine multiple ROI masks (.nii files) into one .nii file?
for example, if I have the right amygdala.nii and left amygdala.nii, how can I combine them into amygdala with both sides?
furthermore, if I have the right, left, anterior and posterior side of insula, how can I combine them into one file as insula?
I've tried the script below,
but while I check the "add overlay function" with MRIcroGL, there's nothing:
clear all;
clc;
ROIFolder=['D:\000ROIMask\AMG'];
cd(ROIFolder);
% ROIFolder=fullfile(pwd, '\functional');
files=dir(fullfile(ROIFolder, '*.nii'));
fileNames={files.name};
for k = 1:numel(fileNames)
fname = fullfile(ROIFolder, fileNames{k});
z(k) = load_nii(fname);
y(:,:,:,k) = z(k).img;
end
% Create the ND Nifti file
output = make_nii(y);
% Save it to a file
save_nii(output, 'myfile.nii')
Thank you so much for your help...
Rainbow

Réponses (1)

Sarthak
Sarthak le 18 Avr 2023
Hi Chia-Yueh Chang,
As per my understanding your approach seems correct. However, you are not specifying the combined ROI mask using any logical operations, which could be the reason why you are not seeing any overlay when checking with MRIcroGL.
To combine the multiple ROI masks into one mask, you need to use logical operations (e.g., or, and, etc.) on the voxel values of the loaded .nii files.
After loading your files, you may modify your code as
% Combine the ROI masks using logical OR operation along the 4th dimension
combined_mask = any(y, 4);
% Create a new NIfTI structure for the combined mask
output = make_nii(combined_mask);
% Save the combined mask as a new .nii file
save_nii(output, 'myfile.nii');
Please note that the resulting combined mask may need to be thresholded or processed further depending on your specific use case and requirements.
Attaching some documentation for your reference

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by