1、Jaccard系数
import numpy as np
import SimpleITK as sitk
#read image
def getdata(file):
data = sitk.ReadImage(file)
img = sitk.GetArrayFromImage(data)
return img
def binary(data,label):
seg = np.where(data == label, 1, 0)
return seg
#seg, gt are the binary segmentation and ground truth areas, respectively.
def Jaccard(seg, gt, label = 1):
seg = binary(seg,label)
gt = binary(gt,label)
associate = seg + gt
inter = np.where(associate==2, 1, 0)
union = np.where(associate==0, 0, 1)
inter_size = np.sum(inter == 1)
union_size = np.sum(union == 1)
if union_size == 0:
print('label{:d}_Dice_Ratio: {:.4f}%'.format(label,100))
return 1
dice = inter_size/union_size
print('label{:d}_Dice_Ratio: {:.4f}%'.format(label,dice*100))
return dice
if __name__ == '__main__':
path_seg = 'G:/result18/HGG/Brats18_2013_2_1.nii.gz'
path_gt = 'G:/Brats18_2013_2_1/Brats18_2013_2_1_seg.nii.gz'
seg = getdata(path_seg)
gt = getdata(path_gt)
labels = [0,1,2,3,4] #金标准里标签类别,0是背景区域
Dice_Ratio_average = 0
for label in labels:
Dice_Ratio_average += Jaccard(seg, gt, label)
print('Dice_Ratio_average: {:.4f}%'.format(Dice_Ratio_average/len(labels)*100))
未完待续~