def pr_object(detect, truth, overlap=10):
# we assume that both truth and detect volumes are separate objects
from scipy import stats
# TODO: 64-bit support
# manual relabel (could be slow!)
utruth = np.unique(truth)
utruth = utruth[utruth > 0]
udetect = np.unique(detect)
udetect = udetect[udetect > 0]
tp = 0.0
fp = 0.0
fn = 0.0
# TODO: removing only greatest match
# for each truth object find a detection
for t in utruth: # background is ignored
match = detect[truth == t]
match = match[match > 0] # get rid of spurious values
match = stats.mode(match)
if match[1] >= overlap:
tp += 1
# any detected objects can only be used once, so remove them here.
# detect = mahotas.labeled.remove_regions(detect, match[0])
detect[detect == match[0]] = 0
else:
fn += 1
# detect_left, fp = mahotas.labeled.relabel(detect)
fp = np.unique(detect)
fp = fp[fp > 0]
fp = len(fp)
precision = 0
recall = 0
if tp + fp > 0:
precision = tp/(tp+fp)
if tp + fn > 0:
recall = tp/(tp+fn)
if (precision == 0) or (recall == 0):
f1 = 0
else:
f1 = (2*precision*recall)/(precision+recall)
print(precision)
print(recall)
print(f1)
return precision, recall, f1
评论列表
文章目录