-
Notifications
You must be signed in to change notification settings - Fork 5
/
dice.py
38 lines (30 loc) · 1.36 KB
/
dice.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
"""
Thanks to voxelmorph: Learning-Based Image Registration, https://github.com/voxelmorph/voxelmorph for this code.
If you use this code, please cite the respective papers in their repo.
"""
import numpy as np
import matplotlib.pyplot as plt
x = np.load('../outputs/lddmm_outputs/generated_segments.npy') #generated output
y = np.load('../../registrationProject/data/testtargetMask.npy') #groundtruth
def dice(array1, array2):
"""
Computes the dice overlap between two arrays for a given set of integer labels.
Parameters:
array1: Input array 1.
array2: Input array 2.
"""
labels = np.arange(1,87) #this should be the number of regions in the mask. For example, if it is a whole organ mask, like liver,this will be binary, so it will have only 1.
dicem = np.zeros(len(labels))
for idx, label in enumerate(labels):
top = 2 * np.sum(np.logical_and(array1 == label, array2 == label))
bottom = np.sum(array1 == label) + np.sum(array2 == label)
bottom = np.maximum(bottom, np.finfo(float).eps) # add epsilon
dicem[idx] = top / bottom
return dicem
dice_all = []
for i in range(0,##): #iterate over all masks, so place the number of images in ##.
d = dice(x[i,:,:,:,:],y[i,:,:,:,:])
dice_all.append(d)
dice_all = np.array(dice_all)
np.save('', dice_all) #give path to save output
print(np.mean(dice_all))