python - 无法计算分割图像的准确性、敏感性和特异性
问题描述
我无法计算分割图像的准确性、敏感性和特异性。我已经用自己的算法对图像进行了分割,并希望通过将分割图像与地面实况图像进行比较来获得分割图像的准确性。
每当我尝试执行此代码时,输出窗口都会冻结并仅显示输出图像。我也希望打印准确度、灵敏度和特异性的值。
在我的代码中,分段图像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()
`
解决方案
可能是因为 cv.waitKey(0) 或 cv2.destroyAllWindows() 的原因,程序还没来得及显示敏感度等值就结束了。您可以使用 pdb 模块https://docs.python.org/2/library/pdb.html来调试您的 python 程序,以便找出问题所在。
推荐阅读
- solr - Solr Query - 如何修复非预期结果(可能是特定于文化的)?
- git - 设置默认推送目标
- android - 如何阻止 Okhttp Authenticator 被多次调用?
- r - 我的分叉仓库上的 master 分支是否会自动更新 github 中原始仓库上的 master 分支
- html - 为什么 e.target.value 反映的是最新值?
- javascript - 为什么 Jest 仍然需要一个模拟模块?
- ruby-on-rails - Rails 5 API 中的必需参数验证
- r - 基于部分字符串匹配的子集数据帧
- python-3.x - Pyspark:仅使用转换删除没有不同值的列
- python - 对于给定的点,如何在点列表中找到最近的点?