1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import cv2 import numpy as np import time
gt = cv2.imread("/home/harvey/Datasets/DeepCrack/OurResult/11125-1_gt.png", cv2.IMREAD_UNCHANGED) p_msk = cv2.imread("/home/harvey/Datasets/DeepCrack/OurResult/11125-1_pred.png", cv2.IMREAD_UNCHANGED) thres = 10 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
def rolling_cal(ground, pred_msk, thres=10): H, W = ground.shape total = H * W filter_size = 5 msk_t = (pred_msk > thres).astype(np.int) ground = (ground > 0).astype(np.int) H, W = ground.shape gt = np.pad(ground, (2, 2), 'constant', constant_values=(0, 0)) tp, fp, fn = 0.0, 0.0, 0.0 for r in range(0, H): for c in range(0, W): gti = gt[r:r + filter_size, c:c + filter_size] cur_pexl = msk_t[r, c] gt_patch = (gti * kernel).sum() if cur_pexl != 0 and gt_patch != 0: tp += 1 elif cur_pexl != 0 and gti[2][2] == 0: fp += 1 elif cur_pexl == 0 and gti[2][2] != 0: fn += 1 return tp, fp, fn, total
start = time.time() tp, fp, fn, total = rolling_cal(gt, p_msk, 10) print("time:", time.time() - start) print(tp, fp, fn, total) recall = tp / (tp + fn) precision = tp / (tp + fp) f1 = 2*tp / (2*tp + fp + fn) print(precision, recall, f1)
|