function [TP, FN, FP, TN] = countTPFNFPTN(segmentedImg, GndTruth, Classfd) % imRGB = imread(['E:\Gopakumar\GopakumarIISTDrive\Dataset\4th IIST Visit\Mal01\Images\GndTruth\Separate\NewEasy\ImgesMinAcrsStck\' '0_5_B.jpg']); % currImgUint8 = imRGB; % load Classfd; load GndTruth; ofstSeg = 7; [X, Y ] = meshgrid(-ofstSeg:ofstSeg, -ofstSeg:ofstSeg); distM = sqrt(X.^2+Y.^2); % [segmentedImg, segWithBndry, WBCs] = getSegmentation(currImgUint8); GndTrthClLbl = segmentedImg(GndTruth); excludeGThCnt = 0; %sepecial case and is due to inaccurate segmentation if (sum(GndTrthClLbl == 0) > 0) %select the closest label from 15x15 neighbourhood. %for each such points [r, c] = find(GndTruth); for i = 1:length(r) if (segmentedImg(r(i), c(i)) == 0) candLbls = segmentedImg(r(i)-ofstSeg:r(i)+ofstSeg, c(i)-ofstSeg:c(i)+ofstSeg); candLblDst = distM .* (candLbls > 0); candLblDst(candLblDst == 0) = Inf; candLblDst = (candLblDst == min(candLblDst(:))); closestLbl = max(candLbls(candLblDst)); %believing that there is a cell in 15x15 loc if (closestLbl > 0) GndTrthClLbl = [GndTrthClLbl; closestLbl]; else %No cell in the visinity excludeGThCnt = excludeGThCnt + 1; end end end end GndTrthClLbl = unique(GndTrthClLbl); if (~isempty(GndTrthClLbl) && GndTrthClLbl(1) == 0); GndTrthClLbl(1) = []; end; ClassfdClLbl = segmentedImg(Classfd); excludeClsfdCnt = 0; %sepecial case and is due to inaccurate segmentation if (sum(ClassfdClLbl == 0) > 0) %select the closest label from 15x15 neighbourhood. %for each such points [r, c] = find(Classfd); for i = 1:length(r) if (segmentedImg(r(i), c(i)) == 0) candLbls = segmentedImg(r(i)-ofstSeg:r(i)+ofstSeg, c(i)-ofstSeg:c(i)+ofstSeg); candLblDst = distM .* (candLbls > 0); candLblDst(candLblDst == 0) = Inf; candLblDst = (candLblDst == min(candLblDst(:))); closestLbl = max(candLbls(candLblDst)); %believing that there is a cell in 15x15 loc if (closestLbl > 0) ClassfdClLbl = [ClassfdClLbl; closestLbl]; else %No cell in the visinity excludeClsfdCnt = excludeClsfdCnt + 1; end end end end ClassfdClLbl = unique(ClassfdClLbl); if (~isempty(ClassfdClLbl) && ClassfdClLbl(1) == 0); ClassfdClLbl(1) = []; end; %count TP and FN TP = 0; FN = 0; for i = 1:length(GndTrthClLbl) curCell = GndTrthClLbl(i); if (sum(curCell == ClassfdClLbl) > 0) TP = TP + 1; else FN = FN + 1; end end FP = length(ClassfdClLbl) - TP; TN = double(max(segmentedImg(:)) - (TP + FN + FP)); % imshow(segWithBndry); end