无法计算分割图像的准确性,敏感性和特异性

时间:2018-10-16 16:40:44

标签: python python-3.x numpy opencv image-processing

我无法计算分割图像的准确性,敏感性和特异性。我已经使用自己的算法对图像进行了分割,并希望通过将其与地面真实图像进行比较来获得分割后图像的准确性。

每当我尝试执行此代码时,输​​出窗口就会冻结,并且仅显示输出图像。我也希望打印出准确性,敏感性和特异性的值。

在我的代码中,分割后的图像 erosion2 和地面真实情况图像为 res 。当我进行比较时,两个图像都显示但不显示准确性。

`import cv2 as cv
import numpy as np

img = cv.imread('dataset.tif')
cv.imshow('Input Image',img)

b,g,r= cv.split(img)
cv.imshow('Red Channel',r)
cv.imshow('Green Channel',g)
cv.imshow('Blue Channel',b)
img2= cv.bitwise_not(g)
cv.imshow('Processed Image',img2)

kernel3 = cv.getStructuringElement(cv.MORPH_ELLIPSE,(13,13))

tophat = cv.morphologyEx(img2, cv.MORPH_TOPHAT, kernel3)
cv.imshow('Top hat',tophat)

thres= 12
maxValue = 255
ret,thresh41 = cv.threshold(tophat,thres, maxValue,cv.THRESH_TOZERO)
th, dat = cv.threshold(tophat, thres, maxValue, cv.THRESH_BINARY)
cv.imshow('thresh',dat)
kernel1 = cv.getStructuringElement(cv.MORPH_ELLIPSE,(2,2))
dilation = cv.dilate(dat,kernel1,iterations = 1)
erosion = cv.erode(dilation,kernel1,iterations = 1)
erosion1=cv.GaussianBlur(erosion,(5,5),0)
erosion1=cv.blur(erosion,(5,5),0)
x=cv.subtract(dilation,erosion1)
x = cv.medianBlur(x,5)
cv.imshow("op1",x)
b2=cv.add(erosion,x)
cv.imshow("fin",b2)
erosion2=cv.erode(b2,kernel1,iterations=1)
cv.imshow("result",erosion2)

res = cv.imread('manual1.tiff')
#cv.imshow('GroundTruth Image',img3)
res = cv.cvtColor(res, cv.COLOR_BGR2GRAY)
print(res.shape)
#def calC_accuracy(result, label):
tp = 0
fp = 0
tn = 0
fn = 0
i = 0
j = 0

print(np.unique(erosion2))
print(np.unique(res))
while i < erosion2.shape[0]:
    j = 0
while j < erosion2.shape[1]:
    if label[i,j] == 255:
        if erosion2[i,j] == res[i,j]:
            tp = tp + 1
else:
    fn = fn + 1

if erosion[i,j] == res[i,j]:
    tn = tn + 1
else:
    fp = fp + 1
j = j + 1
i = i + 1
print("TN =",tn,"FP =",fp)
print("FN =",fn,"TP =",tp)
print("Sensitivity = ",float(tp/(tp+fn+1)))
print("Specificity = ",float(tn/(tn+fp+1)))
print("Accuracy = ",float((tn+tp)/(fn+fp+1+tn+tp)))
#print("PPV = ",float(tp/(tp+fp+1)))
#return float(tp/(tp+fp+1))

#cv.imshow('result',erosion)
cv.waitKey(0)
cv2.destroyAllWindows()
`

1 个答案:

答案 0 :(得分:0)

也许是由于cv.waitKey(0)或cv2.destroyAllWindows()造成的,该程序在显示灵敏度和其他值之前结束。您可以使用pdb模块https://docs.python.org/2/library/pdb.html来调试python程序,以便找出问题出在哪里。