1
1
from typing import Callable
2
2
3
3
from cv2 import imread , cvtColor , COLOR_RGB2GRAY
4
- from numpy import ndarray , logical_and , load
4
+ from medpy .metric .binary import dc
5
+ from numpy import ndarray , load , abs as npabs , max as npmax , sum as npsum
5
6
from rich .progress import Progress
6
7
7
8
from utils import get_items
8
9
9
10
10
11
def calculate_dcs (a : ndarray , b : ndarray ) -> float :
11
- a , b = a .astype (bool ), b .astype (bool )
12
- return float (2 * logical_and (a , b ).sum () / (a .sum () + b .sum ()))
12
+ return dc ((a / npmax (a )) == 1 , (b / npmax (b )) == 1 )
13
13
14
14
15
15
def calculate_nsd (a : ndarray , b : ndarray ) -> float :
16
- return abs (a - b ).sum () / max (a .sum (), b .sum ())
16
+ a , b = (a / npmax (a )).astype (int ), (b / npmax (b )).astype (int )
17
+ sum_diff = npsum (npabs (a - b ))
18
+ max_sum = max (npsum (a ), npsum (b ))
19
+ return sum_diff / max_sum
17
20
18
21
19
22
def evaluate (src : str , val : str , method : Callable [[ndarray , ndarray ], float ]) -> float :
@@ -25,8 +28,7 @@ def evaluate(src: str, val: str, method: Callable[[ndarray, ndarray], float]) ->
25
28
for path in get_items (val ):
26
29
if not path .endswith (".npy" ):
27
30
continue
28
- r += method (load (f"{ val } /{ path } " ),
29
- cvtColor (imread (f"{ src } /case_{ str (i ).zfill (4 )} .png" ), COLOR_RGB2GRAY ) / 256 )
31
+ r += method (load (f"{ val } /{ path } " ), cvtColor (imread (f"{ src } /case_{ str (i ).zfill (4 )} .png" ), COLOR_RGB2GRAY ))
30
32
i += 1
31
33
progress .update (task , advance = 1 )
32
34
return r / i
0 commit comments