How can I calculate the iou and dice for each slices in a CT volume to obtain the mean for all the volume?
1 view (last 30 days)
Show older comments
This is my code:
load("25_1_50epochs.mat")
destinationSkullTest11 = "C:\Users\User\Documents\SEM 7\FYP\Dataset\U-net\Image Test";
destinationMasksTest11 = "C:\Users\User\Documents\SEM 7\FYP\Dataset\U-net\Mask Test";
destinationPredictionFolder = "C:\Users\User\Documents\SEM 7\Deep Learning\Predicted Test";
pixelLabelID = [1 0];
classNames = ["fracture" "background"];
SkullFiles = dir(destinationSkullTest11);
SkullDirectoryNames = {SkullFiles([SkullFiles.isdir]).name};
SkullDirectoryNames = SkullDirectoryNames(~ismember(SkullDirectoryNames,{'.','..'}));
MaskFiles = dir(destinationMasksTest11);
MaskDirectoryNames = {MaskFiles([MaskFiles.isdir]).name};
MaskDirectoryNames = MaskDirectoryNames(~ismember(MaskDirectoryNames,{'.','..'}));
for z = 1:length(SkullDirectoryNames)
fullfileSkull = fullfile(destinationSkullTest11,SkullDirectoryNames(z));
fullfileMask = fullfile(destinationMasksTest11,MaskDirectoryNames(z));
imdstest11 = imageDatastore(fullfileSkull,"FileExtensions",".png","LabelSource","foldernames");
pxdstest11 = pixelLabelDatastore(fullfileMask,classNames,pixelLabelID,"FileExtensions",".png");
mkdir(fullfile(destinationPredictionFolder, MaskDirectoryNames{z}));
targetFolder = fullfile(destinationPredictionFolder, MaskDirectoryNames(z));
pxdsResults11 = semanticseg(imdstest11,net,"WriteLocation",targetFolder);
for t = 1:length(pxdsResults11.Files)
gt = readimage(pxdstest11,t);
pr = readimage(pxdsResults11,t);
confmat = segmentationConfusionMatrix(pr,gt);
% IoU = TP / (TP + FP + FN)
numeiou(t,:) = confmat(1,1);
denoiou(t,:) = confmat(1,1) + confmat(1,2) + confmat(2,1);
numeiouall = sum(numeiou);
denoiouall = sum(denoiou);
% Dice = 2TP / (2TP + FP + FN)
numedice(t,:) = 2*confmat(1,1);
denodice(t,:) = 2*confmat(1,1) + confmat(1,2) + confmat(2,1);
numediceall = sum(numedice);
denodiceall = sum(denodice);
end
miou(z,:) = sum(numeiouall)/sum(denoiouall);
mdice(z,:) = sum(numediceall)/sum(denodiceall);
end
meaniou = mean(miou);
meandice = mean(mdice);
3 Comments
Image Analyst
on 29 May 2022
Can you use your MATLAb drive? See the 9 spot menu item on the right of your icon in the upper right corner of this page.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!