如何找到分割图像的精度和召回率?

时间:2017-05-01 01:49:33

标签: matlab image-segmentation

我试过这个code

function [Fvalue,precision,recall,accuracy,JaccardIndex,TP,FP,TN,FN,FPrate,TPrate,MCC] = compareBinaryImages( reference, toTest )
%COMPAREBINARYIMAGES Compute various similarity metrics between two binary images
%   reference = grouth truth binary image
%   toTest = binary image to be compared to the reference image

if(ndims(reference)~=2 && ndims(toTest)~=2) 
    error('Inputs must be two 2-dimensional matrices'); 
end; 

%  TP = numel(find(reference==1 & toTest==1)==1); % True positive
%  FP = numel(find(reference==0 & toTest==1)==1); % False positive
%  TN = numel(find(reference==0 & toTest==0)==1); % True negative
%  FN = numel(find(reference==1 & toTest==0)==1); % False negative


 TP = nnz(reference==1 & toTest==1); % True positive
 FP = nnz(reference==0 & toTest==1); % False positive
 TN = nnz(reference==0 & toTest==0); % True negative
 FN = nnz(reference==1 & toTest==0); % False negative

P = TP + FN; % Total positive for the true class (= reference)
N = FP + TN; % TOtal negative for the true class (= reference)

FPrate = FP/N; % False positive rate
TPrate = TP/P; % True positive rate

precision = TP/(TP+FP);
recall = TP/P;
accuracy = (TP+TN)/(P+N);

MCC = (TP*TN-FP*FN)/sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN));
2/((1/precision)+(1/recall));

% Alternative form for Fvalue
2*(precision*recall/(precision+recall));

% Avoid getting a division by 0 if only negatives and perfect detection
if(TN==numel(reference))
    'gaga'
    Fvalue = 1;
    warning('FValue was set to 1 as all pixels were true negatives');
else
    Fvalue=2*TP/(FP+TP+P);
end

2*TP/((FP+TP)+(TP+FN));

JaccardIndex = TP / (FP+TP+FN);

end

我的测试图片为enter image description here

我的参考图片是enter image description here

但我得到的结果是NaN。这有什么问题?

1 个答案:

答案 0 :(得分:1)

附加函数compareBinaryImages需要二进制输入,即输入仅包含0和1的图像。对图像进行二值化并将此函数应用于二进制图像,得到几个数字输出,其中不包括Nan:

testingImg = imread('testing.png');
referenceImg = imread('reference.png');
% grayscale
testingGray = rgb2gray(testingImg);
referenceGray = rgb2gray(referenceImg);
% binarize
testingBw = testingGray == 255;
referenceBw = referenceGray == 255;
% compare *binary* images
[Fvalue,precision,recall,accuracy,JaccardIndex,TP,FP,TN,FN,FPrate,TPrate,MCC] = ...
    compareBinaryImages(referenceBw, testingBw);