MRI数据中断开对象的评估指标

时间:2017-09-01 19:02:42

标签: python image-processing metrics image-segmentation medical

我正在尝试评估在3D MRI体积(脑)中划分区域的算法的准确性。我一直在使用Dice,Jaccard,FPR,TNR,Precision等等,但我只是按像素方式完成此操作(I.E. FNs =假负像素数)。是否有一个python包(或伪代码)在病变级别执行此操作?例如,将TP计算为我的算法检测到的病变数量(grd trth中的3d断开对象)?这样,病变的大小对准确度指标的影响不大。

2 个答案:

答案 0 :(得分:1)

您可以使用scipy的label在图像中查找连接的组件:

from scipy.ndimage.measurements import label
label_pred, numobj_pred = label(my_predictions)
label_true, numobj_true = label(my_groundtruth)

然后使用您选择的指标对它们进行比较。

PS:或scikit-image,带有演示here

答案 1 :(得分:0)

这是我为完成这项任务而编写的代码。如果有人发现任何错误,请告诉我。

def distance(p1, p2,dim):
    if dim==2: return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
    elif dim==3: return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2+ (p2[2] - p1[2])**2)
    else: print 'error'

def closest(true_cntrd,pred_pts,dim):
    dist_list=[]
    for pred_pt in pred_pts:
        dist_list.append( distance(true_cntrd, pred_pt,dim) )
    min_idx = np.argmin(dist_list)
    return pred_pts[min_idx],min_idx

def eval_disconnected(y_true,y_pred,dim):
    y_pred=y_pred>0.5
    label_pred, numobj_pred = label(y_pred)
    label_true, numobj_true = label(y_true)

    true_labels,pred_labels=np.arange(numobj_true+1)[1:],np.arange(numobj_pred+1)[1:]
    true_centroids=center_of_mass(y_true,label_true,true_labels)
    pred_centroids=center_of_mass(y_pred,label_pred,pred_labels)

    if len(pred_labels)==0:
        TP,FN,FP=0,len(true_centroids),0
        return TP,FN,FP

    true_lbl_hit_list=[]
    pred_lbl_hit_list=[]
    for (cntr_true,lbl_t) in zip(true_centroids,np.arange(numobj_true+1)[1:]):
        closest_pred_cntr,idx = closest(cntr_true,pred_centroids,dim)
        closest_pred_cntr=tuple(int(coor) for coor in closest_pred_cntr)
        if label_true[closest_pred_cntr]==lbl_t: 
            true_lbl_hit_list.append(lbl_t)
            pred_lbl_hit_list.append(pred_labels[idx]  )
    pred_lbl_miss_list = [pred_lbl for pred_lbl in pred_labels if not(pred_lbl in pred_lbl_hit_list)]
    true_lbl_miss_list = [true_lbl for true_lbl in true_labels if not(true_lbl in true_lbl_hit_list)]
    TP=len(true_lbl_hit_list) # all the grd truth labels that were predicted 
    FN=len(true_lbl_miss_list) # all the grd trth labels that were missed
    FP=len(pred_lbl_miss_list) # all of the predicted labels that didn't hit
    return TP,FN,FP