-
Notifications
You must be signed in to change notification settings - Fork 10
/
cldice_metric.py
79 lines (61 loc) · 2.13 KB
/
cldice_metric.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# centerline-in-mask-dice-coefficient or clDice
# connectivity-preserving metric to evaluate tubular and linear structure segmentation based on intersecting skeletons with masks.
from skimage import io, morphology, filters, util
import numpy as np
import os
from skimage.viewer import ImageViewer
import matplotlib.pyplot as plt
def binarize(img):
if len(np.unique(img)) > 1:
img = img > filters.threshold_otsu(img)
return img
def cl_score(v, s):
return np.sum(v*s) / np.sum(s)
# helper function for data visualization
def visualize(**images):
"""PLot images in one row."""
n = len(images)
plt.figure(figsize=(16, 5))
for i, (name, image) in enumerate(images.items()):
plt.subplot(1, n, i + 1)
plt.xticks([])
plt.yticks([])
plt.title(' '.join(name.split('_')).title())
plt.imshow(image)
plt.show()
def clDice(v_p, v_l):
if len(np.unique(v_l)) > 1:
if len(np.unique(v_p)) > 1:
v_p = v_p > filters.threshold_otsu(v_p)
v_l = v_l > filters.threshold_otsu(v_l)
s_l = morphology.skeletonize(v_l)
s_p = morphology.skeletonize(v_p)
# visualize(
# Ground_Truth = v_l,
# gt_skeleton = s_l,
# Prediction = v_p,
# pr_skeletong = s_p
# )
tprec = cl_score(v_p, s_l)
tsens = cl_score(v_l, s_p)
clDice = 2*tprec*tsens/(tprec+tsens+0.001)
return clDice, 0
else:
return 0, 0
else:
return 1, 1
# # example usage
# base_dir = 'C:\\Users\\kaniourasp\\Downloads\\Thesis\\APLS\\New folder\\road_visualizer-master\\data\\nteloSample'
# gt_file = os.path.join(base_dir, '2_gt.png')
# pred_file = os.path.join(base_dir, '2_pred.png')
# gt = io.imread(gt_file)[:, :, 0]
# pred = io.imread(pred_file)[:, :, 0]
# # change foreground with background if needed
# binary_gt = util.invert(binarize(gt))
# binary_pred = util.invert(binarize(pred))
# io.imshow(binary_gt)
# io.show()
# io.imshow(binary_pred)
# io.show()
# measure = clDice(binary_pred, binary_gt)
# print("measure: ", measure)