MRIwrite Mask to NIfTI File

11 Ansichten (letzte 30 Tage)
Annabel Sorby-Adams
Annabel Sorby-Adams am 12 Okt. 2023
Beantwortet: Akshat am 20 Okt. 2023
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

Antworten (1)

Akshat
Akshat am 20 Okt. 2023
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

Kategorien

Mehr zu Neuroimaging finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by