遥感影像分割各种指标计算代码

通过计算各张影像总的混淆矩阵,然后一一计算OA、Kappa、precision、recall、F1-score、IOU

(注意之前写的代码,公式有处,这里更新如下)

下面直接上菜,谢谢品尝!

  1. import cv2 as cv
  2. import numpy as np
  3. from collections import Counter
  4. import xlwt as excel
  5. from decimal import Decimal
  6.  
  7. def cal_confu_matrix(label, predict, class_num):
  8.     confu_list = []
  9.     for i in range(class_num):
  10.         c = Counter(predict[np.where(label == i)])
  11.         single_row = []
  12.         for j in range(class_num):
  13.             single_row.append(c[j])
  14.         confu_list.append(single_row)
  15.     return np.array(confu_list).astype(np.int32)
  16.  
  17. def metric_evaluate(confu_mat_total, save_path, save_name):
  18.     '''
  19.     :param confu_mat: 总的混淆矩阵
  20.     :return: excel写出混淆矩阵, precision,recall,IOU,f-score
  21.     '''
  22.     class_num = confu_mat_total.shape[0]
  23.     file = excel.Workbook(encoding='utf-8')
  24.     table_name = save_name
  25.     pic_name = table_name + ' metrics:'
  26.     table = file.add_sheet(table_name)
  27.     table_raw = 0  # 表从第一行开始记录数据
  28.     table.write(table_raw, 0, pic_name)
  29.     table_raw += 2
  30.     '''计算归一化到0-1的混淆矩阵;并写出到Excel中'''
  31.     confu_mat = confu_mat_total.astype(np.float32) + 0.0001
  32.     col_sum = np.sum(confu_mat, axis=1)  # 按行求和
  33.     raw_sum = np.sum(confu_mat, axis=0)  # 每一列的数量
  34.     '''计算各类面积比,以求OA值'''
  35.     oa = 0
  36.     for i in range(class_num):
  37.         oa = oa + confu_mat[i, i]
  38.     oa = oa / confu_mat.sum()
  39.     '''Kappa'''
  40.     pe_fz = 0
  41.     for i in range(class_num):
  42.         pe_fz += col_sum[i] * raw_sum[i]
  43.     pe = pe_fz / (np.sum(confu_mat) * np.sum(confu_mat))
  44.     kappa = (oa - pe) / (1 - pe)
  45.     # 将混淆矩阵写入excel中
  46.     TP = []  # 识别中每类分类正确的个数
  47.     table.write(table_raw, 0, 'confusion_matrix:')
  48.     table_raw = table_raw + 1
  49.     name_str = ['Clutter/background', 'Impervious surfaces', 'Building', 'Low vegetation', 'Tree', 'Car']
  50.     for i in range(class_num):
  51.         table.write(table_raw, 1+i, name_str[i])
  52.     for i in range(class_num):
  53.         table_raw = table_raw + 1
  54.         table.write(table_raw, 0, name_str[i])
  55.         TP.append(confu_mat[i, i])
  56.         for j in range(class_num):
  57.             table.write(table_raw, j + 1, int(confu_mat_total[i, j]))
  58.     # 计算f1-score
  59.     TP = np.array(TP)
  60.     FN = raw_sum - TP
  61.     FP = col_sum  - TP
  62.     # 计算并写出precision,recall, f1-score,f1-m以及mIOU
  63.     table_raw = table_raw + 2
  64.     table.write(table_raw, 0, 'precision:')
  65.     # 写出precision
  66.     for i in range(class_num):
  67.         table.write(table_raw, i + 1, Decimal(float(TP[i]/raw_sum[i])).quantize(Decimal("0.000")))
  68.     table_raw += 1
  69.     table.write(table_raw, 0, 'Recall:')
  70.     # 写出recall
  71.     for i in range(class_num):
  72.         table.write(table_raw, i + 1, Decimal(float(TP[i]/col_sum[i])).quantize(Decimal("0.000")))
  73.     f1_m = []
  74.     iou_m = []
  75.     table_raw += 1
  76.     table.write(table_raw, 0, 'f1-score:')
  77.     for i in range(class_num):
  78.         # 写出f1-score
  79.         f1 = TP[i] * 2 / (TP[i] * 2 + FP[i] + FN[i])
  80.         f1_m.append(f1)
  81.         iou = TP[i] / (TP[i] + FP[i] + FN[i])
  82.         iou_m.append(iou)
  83.         table.write(table_raw, i + 1, Decimal(f1).quantize(Decimal("0.000")))
  84.     table_raw += 1
  85.     table.write(table_raw, 0, 'OA:')
  86.     table.write(table_raw, 1, Decimal(float(oa)).quantize(Decimal("0.000")))
  87.     table_raw += 1
  88.     table.write(table_raw, 0, 'Kappa:')
  89.     table.write(table_raw, 1, Decimal(float(kappa)).quantize(Decimal("0.000")))
  90.     f1_m = np.array(f1_m)
  91.     table_raw += 1
  92.     table.write(table_raw, 0, 'f1-m:')
  93.     table.write(table_raw, 1, Decimal(float(np.mean(f1_m))).quantize(Decimal("0.000")))
  94.     iou_m = np.array(iou_m)
  95.     table_raw += 1
  96.     table.write(table_raw, 0, 'mIOU:')
  97.     table.write(table_raw, 1, Decimal(float(np.mean(iou_m))).quantize(Decimal("0.000")))
  98.     file.save(save_path + table_name + '.xls')
     

你可能感兴趣的:(Python,DL论文)