SimpleITK计算dice系数及Hausdorff距离(python代码)

做医学图像分割,需要对分割结果进行评价,常用的有dice系数和Hausdorff距离等,如何在python中用SimpleITK自带函数实现这一功能呢,代码如下:

# -*- coding: utf-8 -*-

import numpy as np
import os
import SimpleITK as sitk

def 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 L 

def 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 quality

gtpath = ''
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)
    

 

你可能感兴趣的:(代码)