天天看點

Python 執行個體分割評估 實作Dice, BestDice, FBDice, AbsDiffFGLabels計算

1、AbsDiffFGLabels: 兩張圖檔的前景label的Dice

import numpy as np


def AbsDiffFGLabels(inlabel, gtlabel):
    '''
    inLabel: label image to be evaluated. Labels are assumed to be consecutive numbers.
    gtLabel: ground truth label image. Labels are assumed to be consecutive numbers.
    score: Absolute value of difference of the number of foreground labels
    '''
    
    #check if label images have same size
    if (inlabel.shape == gtlabel.shape):
        maxInLabel = int(max(map(max,inlabel)))
        minInLabel = int(min(map(min,inlabel)))
        maxGtLabel = int(max(map(max,gtlabel)))
        minGtLabel = int(min(map(min,gtlabel)))
        
        score = (maxInLabel-minInLabel) - (maxGtLabel-minGtLabel)
        score_abs = abs(score)
        return score_abs, score
    else:
        return -1
           

2、BestDice: 對應label的切割最好結果的累計評分

def Dice(inlabel, gtlabel, i, j):
    # calculate Dice score for the given labels i and j.
    inMask = (inlabel==i)
    gtMask = (gtlabel==j)
    insize = np.sum(inMask)
    gtsize = np.sum(gtMask)
    overlap = np.sum(inMask & gtMask)
    return 2*overlap/float(insize+gtsize)
    

def BestDice(inlabel, gtlabel):
    '''
    inLabel: label image to be evaluated. Labels are assumed to be consecutive numbers.
    gtLabel: ground truth label image. Labels are assumed to be consecutive numbers.
    score: Dice score
    '''
    if (inlabel.shape == gtlabel.shape):
        maxInLabel = int(max(map(max,inlabel)))
        minInLabel = int(min(map(min,inlabel)))
        maxGtLabel = int(max(map(max,gtlabel)))
        minGtLabel = int(min(map(min,gtlabel)))
        score = 0 # initialize output
        
        # loop all labels of inLabel.
        for i in range(minInLabel, maxInLabel+1):
            sMax = 0
            # loop all labels of gtLabel.
            for j in range(minGtLabel, maxGtLabel+1):
                s = Dice(inlabel, gtlabel, i, j)
                # keep max Dice value for label i.
                sMax = max(s, sMax)
            score += sMax # sum up best found values.
        score = score/float(maxInLabel-minInLabel+1)
        return score
    else:
        return 0
           

3、FBDice 前景背景切割情況Dice系數

def FBDice(inlabel, gtlabel):
    if (inlabel.shape == gtlabel.shape):
        
        minInLabel = int(min(map(min,inlabel)))
        minGtLabel = int(min(map(min,gtlabel)))
        inMask = (inlabel > minInLabel)
        gtMask = (gtlabel > minGtLabel)
        inSize = np.sum(inMask)
        gtSize = np.sum(gtMask)
        overlap = np.sum(inMask & gtMask)
        return 2*overlap/float(inSize + gtSize)
    else:
        return 0