MRIwrite Mask to NIfTI File
조회 수: 6 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Neuroimaging에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!