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

2024-05-20 18:43:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

每当我试图执行这段代码时,输出窗口就会冻结,只显示输出图像。我希望准确性、敏感性和特异性的价值也能被打印出来。在

在我的代码中,分割的图像腐蚀2和地面真实图像是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()
`

Tags: 图像tophatresfloatcvtnfnprint