Python精度评价
py
import cv2
import numpy as np
from sklearn.metrics import cohen_kappa_score, confusion_matrix
label = cv2.imread("Algorithm/4_OUT.bmp", cv2.IMREAD_GRAYSCALE)
detection = cv2.imread("Algorithm/changemap.jpg", cv2.IMREAD_GRAYSCALE)
# 匹配shape保持一致
detection = cv2.resize(detection, (label.shape[1], label.shape[0]))
# 图像二值化
label_bin = np.where(label > 0, 1, 0)
detection_bin = np.where(detection > 0, 1, 0)
# 计算交集、并集
intersection = np.logical_and(label_bin, detection_bin)
union = np.logical_or(label_bin, detection_bin)
# 计算准确率、召回率、F1-score
tn, fp, fn, tp = confusion_matrix(label_bin.flatten(), detection_bin.flatten()).ravel()
accuracy = (tp + tn) / (tp + tn + fp + fn)
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1_score = 2 * precision * recall / (precision + recall)
# 输出结果
print(accuracy)
print(precision)
print(recall)
print(f1_score)
# 计算
img1 = label_bin
img2 = detection_bin
# 计算混淆矩阵
confusion = confusion_matrix(img1.ravel(), img2.ravel())
# 计算总体精度
overall_accuracy = np.trace(confusion) / np.sum(confusion)
# 计算kappa系数
kappa = cohen_kappa_score(img1.ravel(), img2.ravel())
# 计算过检率和漏检率
false_alarm = confusion[1, 0] / np.sum(confusion[1, :])
missed_detection = confusion[0, 1] / np.sum(confusion[0, :])
# 输出结果
print('Overall accuracy: ', overall_accuracy)
print('Kappa coefficient: ', kappa)
print('False alarm rate: ', false_alarm)
print('Missed detection rate: ', missed_detection)
python
def evevalue_it(img1, img2):
# img1 the true picture
# img2 the detected pictrue
# 图像检测评价
# 两个算法通过对比黑色表示变化部分
# 不一定黑色就是变化部分
x00 = 0
x10 = 0
x01 = 0
x11 = 0
(row, cloumn) = img1.shape
for i in range(row):
for j in range(cloumn):
if img1[i][j] == 0 and img2[i][j] == 0:
x00 += 1
if img1[i][j] == 255 and img2[i][j] == 255:
x11 += 1
if img1[i][j] == 255 and img2[i][j] == 0:
x10 += 1
if img1[i][j] == 0 and img2[i][j] == 255:
x01 += 1
print(x00, x11, x10, x01)
TRC = x00+x10
print(TRC)
TRU = x01+x11
print(TRU)
TDC = x00+x01
print(TDC)
TDU = x10+x11
T = TRC+TDC
# 总错误率
total_erro = (x01+x10)/T
# 总误错率
fake_erro = x01/TRU
# 误漏率
dn_find_erro = x10/TRC
# 整体精度
total_accucy = (x00+x11)/T
print("总错误率为%f" % total_erro+"总误错率为%f" % fake_erro+"漏错率为%f" %
dn_find_erro, "整体精度为%f" % total_accucy, sep=";")