import cv2
import glob
import os
import numpy as np
from medpy import metric
def calculate_metric_percase(pred, gt):
# dice = metric.binary.dc(pred, gt)
# jc = metric.binary.jc(pred, gt)
hd = metric.binary.hd95(pred, gt)
asd = metric.binary.asd(pred, gt)
return hd, asd
dice_mean = 0
hd_mean = 0
asd_mean = 0
hd = list()
asd =list()
predlist=glob.glob(os.path.join('./Dataset/data2/predict', "*"))# Predicted folder path
gtlist=glob.glob(os.path.join('./Dataset/data2/val/M', "*"))# Truth folder path
for i in range(len(predlist)):
print(i, os.path.basename(predlist[i]))
img_ground = cv2.imread(predlist[i])
img_predict = cv2.imread(gtlist[i])
x = 0
y = 0
for w in range(256):
for h in range(256):
x += img_ground.item(w, h, 1)/255
y += img_predict.item(w, h, 1)/255
if x + y == 0 or x == 0 or y == 0:
current_hd=0
current_asd=0
else:
current_hd, current_asd = calculate_metric_percase(img_predict, img_ground)
print('hd', current_hd, 'asd', current_asd)
hd_mean += current_hd
hd.append((current_hd))
asd_mean += current_asd
asd.append((current_asd))
hd_mean /=len(predlist)
asd_mean /=len(predlist)
print('Dice',dice_mean)
print('hd',hd_mean,'asd',asd_mean)