MRIwrite Mask to NIfTI File

14 次查看(过去 30 天)
Annabel Sorby-Adams
Annabel Sorby-Adams 2023-10-12
回答: Akshat 2023-10-20
Hi MATLAB,
I have two MRI segmentation masks in NIfTI format (attached). I would like to subtract one mask (lesion) from another (wholebrain) and output a seperate NIfTI file of the result. So far I have the following, but I was wondering if anyone could please reccommend how to output the file per above using MRIwrite?
datadir = '';
d = dir([datadir filesep '*Lesion_Mask.nii.gz']);
disp(['Found ' num2str(length(d)) ' Lesion Masks ' ]);
d = dir([datadir filesep '*Wholebrain_Mask.nii.gz']);
disp(['Found ' num2str(length(d)) ' Wholebrain Masks ' ]);
resultspath = '';
Nd = numel(d);
for i = 1 : Nd
disp(sprintf('Working on case %d of %d: %s',i,Nd,d(i).name))
f = find(d(i).name=='_');
prefix = d(i).name(1:f(1));
lesionmask = fullfile(datadir,[prefix '*Lesion_Mask.nii.gz']);
wholebrainmask = fullfile(datadir,[prefix '*Wholebrain_Mask.nii.gz']);
MMmri = MRIread(wholebrainmask); %read wholebrain mask
LMmri = MRIread(lesionmask); % read lesion mask
MMmri.vol(LMmri.vol>0)=0; % kill lesion mask
end

回答(1 个)

Akshat
Akshat 2023-10-20
Hi Annabel,
As per my understanding of the question, you want to perform some operations on MRI images present with you and write it to a file.
I searched the documentation and did not find any “MRIRead” or “MRIWrite” functions. On the other hand, there is a function to read NIfTI files and write them.
Here is the documentation of niftiread and niftiwrite, the functions I have used to read and write the files.
Hence, I have provided an updated code which is working for your inputs. Also, as of now, I have not done the “subtraction”, as it is not clear to me how to perform the subtraction. You can change the line number 20 in the code as you wish to and subtract the inputs.
datadir = '';
d = dir(['*Lesion_Mask.nii.gz']);
disp(['Found ' num2str(length(d)) ' Lesion Masks ' ]);
d = dir(['*Wholebrain_Mask.nii.gz']);
disp(['Found ' num2str(length(d)) ' Wholebrain Masks ' ]);
resultspath = '';
Nd = numel(d);
for i = 1 : Nd
disp(sprintf('Working on case %d of %d: %s',i,Nd,d(i).name))
f = find(d(i).name=='_');
prefix = d(i).name(1:f(1));
lesionmask = fullfile(datadir,[prefix 'Lesion_Mask.nii.gz']);
wholebrainmask = fullfile(datadir,[prefix 'Wholebrain_Mask.nii.gz']);
MMmri = niftiread(wholebrainmask); %read wholebrain mask
LMmri = niftiread(lesionmask); % read lesion mask
subtractedMask = MMmri; %You can perform your subtraction here.
outputFilePath = fullfile(resultspath, [prefix 'Subtracted_Mask']);
% Write the result to a NIfTI file
niftiwrite(subtractedMask,outputFilePath,"Compressed",true);
end
Hope this helps!
Regards
Akshat Wadhwa

类别

Help CenterFile Exchange 中查找有关 MRI 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by