def nn_closer(img0, img1, labels0, labels1, DISPLACEMENT=30, MASSTHRES=0.25): labels = -labels1.copy() rps0 = regionprops(labels0, img0) rps1 = regionprops(labels1, img1) if not rps0 or not rps1: return labels0, labels dist = cdist([i.centroid for i in rps0], [i.centroid for i in rps1]) massdiff = calc_massdiff(rps0, rps1) binary_cost = (dist < DISPLACEMENT) * (abs(massdiff) < MASSTHRES) binary_cost = pick_closer_cost(binary_cost, dist) binary_cost = pick_closer_cost(binary_cost.T, dist.T).T idx1, idx0 = find_one_to_one_assign(binary_cost) for i0, i1 in zip(idx0, idx1): labels[labels1 == rps1[i1].label] = rps0[i0].label labels0[labels0 == rps0[i0].label] = -rps0[i0].label return labels0, labels
def nearest_neighbor(img0, img1, labels0, labels1, DISPLACEMENT=20, MASSTHRES=0.2): """ labels0 and labels1: the positive values for non-tracked objects and the negative values for tracked objects. """ labels = -labels1.copy() rps0 = regionprops(labels0, img0) rps1 = regionprops(labels1, img1) if not rps0 or not rps1: return labels0, labels dist = cdist([i.centroid for i in rps0], [i.centroid for i in rps1]) massdiff = calc_massdiff(rps0, rps1) binary_cost = (dist < DISPLACEMENT) * (abs(massdiff) < MASSTHRES) idx1, idx0 = find_one_to_one_assign(binary_cost) for i0, i1 in zip(idx0, idx1): labels[labels1 == rps1[i1].label] = rps0[i0].label labels0[labels0 == rps0[i0].label] = -rps0[i0].label return labels0, labels