SimpleITK计算dice系数及Hausdorff距离(python代码)_愿十四亿神州尽舜尧-CSDN博客
做医学图像分割,需要对分割结果进行评价,常用的有dice系数和Hausdorff距离等,如何在python中用SimpleITK自带函数实现这一功能呢,代码如下:
# -*- coding: utf-8 -*-import numpy as npimport osimport SimpleITK as sitkdef file_name(file_dir): L=[]path_list = os.listdir(file_dir)path_list.sort() #对读取的路径进行排序for filename in path_list:if 'nii' in filename: L.append(os.path.join(filename))return Ldef computeQualityMeasures(lP,lT):quality=dict()labelPred=sitk.GetImageFromArray(lP, isVector=False)labelTrue=sitk.GetImageFromArray(lT, isVector=False)hausdorffcomputer=sitk.HausdorffDistanceImageFilter()hausdorffcomputer.Execute(labelTrue>0.5,labelPred>0.5)quality["avgHausdorff"]=hausdorffcomputer.GetAverageHausdorffDistance()quality["Hausdorff"]=hausdorffcomputer.GetHausdorffDistance()dicecomputer=sitk.LabelOverlapMeasuresImageFilter()dicecomputer.Execute(labelTrue>0.5,labelPred>0.5)quality["dice"]=dicecomputer.GetDiceCoefficient()return qualitygtpath = ''predpath = ''gtnames = file_name(gtpath)prednames = file_name(predpath)labels_num = np.zeros(len(prednames))NUM=[]P=[]for i in range(len(gtnames)):gt = sitk.ReadImage(gtpath + gtnames[i])pred = sitk.ReadImage(predpath + gtnames[i])quality = computeQualityMeasures(pred,gt)
赞 (0)