示例#1
0
文件: lap.py 项目: yuchaz/glosim
def best_cost(matrix):
    hun = linear_assignment(matrix)
    cost = 0.0
    #print hun
    for pair in hun:
        cost += matrix[pair[0], pair[1]]
    return cost
示例#2
0
def _disjoint_max_assignment(similarities):
    global sparse

    # form n*n adjacency matrix
    where_true, where_pred = similarities.nonzero()
    where_pred = where_pred + similarities.shape[0]
    n = sum(similarities.shape)
    A = sparse.coo_matrix((np.ones(len(where_true)), (where_true, where_pred)),
                          shape=(n, n))
    try:
        n_components, components = csgraph.connected_components(A,
                                                                directed=False)
    except (AttributeError, TypeError):
        warnings.warn(
            'Could not use scipy.sparse.csgraph.connected_components.'
            'Please update your scipy installation. '
            'Calculating max-score assignment the slow way.')
        # HACK!
        sparse = None
        return _disjoint_max_assignment(similarities)

    if hasattr(similarities, 'toarray'):
        # faster to work in dense
        similarities = similarities.toarray()
    total = 0
    for i in range(n_components):
        mask = components == i
        component_true = np.flatnonzero(mask[:similarities.shape[0]])
        component_pred = np.flatnonzero(mask[similarities.shape[0]:])
        component_sim = similarities[component_true, :][:, component_pred]
        if 0 in component_sim.shape:
            pass
        if component_sim.shape == (1, 1):
            total += component_sim[0, 0]
        else:
            indices = linear_assignment(-component_sim)
            total += component_sim[indices[:, 0], indices[:, 1]].sum()
    return total
示例#3
0
 im_with_keypoints = cv2.drawKeypoints(fgmask, keypoints, np.array([]), (0,0,255))
 if len(bees) == 0: # no bees yet, no matching to do, just add them
     for kp in keypoints:
         bees.append(Bee(kp.pt))
         hive.append(kp.pt)
 else :
     # MUNKRES assignment, slightly better
     freeBees = [True for i in xrange(len(bees))]
     freeKP = [True for i in xrange(len(keypoints))]
     # build cost matrix 
     cost = np.zeros((len(keypoints), len(bees)))
     for i,kp in enumerate(keypoints):
         for j,b in enumerate(bees):
             cost[i,j] = b.dist(kp.pt)
     # proper assignment
     assignment = linear_assignment(cost)
     for ass in assignment :
         if cost[ass[0], ass[1]] < THRESHBEE:
             bees[ass[1]].move(keypoints[ass[0]].pt)
             freeBees[ass[1]] = False
             freeKP[ass[0]] = False
     for i in xrange(len(freeBees)): # lost bees
         if freeBees[i]:
             bees[i].lastSeen += 1
     for i in xrange(len(freeKP)): # new keypoints
         if freeKP[i]:
             bees.append(Bee(keypoints[i].pt))
             hive.append(keypoints[i].pt)                    
 # remove lost bees (not seen for at least 15 frames)
 tmp = []
 for b in bees:
示例#4
0
文件: bees.py 项目: Julienpir/beehive
 im_with_keypoints = cv2.drawKeypoints(fgmask, keypoints, np.array([]), (0,0,255))
 if len(bees) == 0: # no bees yet, no matching to do, just add them
     for kp in keypoints:
         bees.append(Bee(kp.pt))
         hive.append(kp.pt)
 else :
     # MUNKRES assignment, slightly better
     freeBees = [True for i in xrange(len(bees))]
     freeKP = [True for i in xrange(len(keypoints))]
     # build cost matrix
     cost = np.zeros((len(keypoints), len(bees)))
     for i,kp in enumerate(keypoints):
         for j,b in enumerate(bees):
             cost[i,j] = b.dist(kp.pt)
     # proper assignment
     assignment = linear_assignment(cost)
     for ass in assignment :
         if cost[ass[0], ass[1]] < THRESHBEE:
             bees[ass[1]].move(keypoints[ass[0]].pt)
             freeBees[ass[1]] = False
             freeKP[ass[0]] = False
     for i in xrange(len(freeBees)): # lost bees
         if freeBees[i]:
             bees[i].lastSeen += 1
     for i in xrange(len(freeKP)): # new keypoints
         if freeKP[i]:
             bees.append(Bee(keypoints[i].pt))
             hive.append(keypoints[i].pt)                    
 # remove lost bees (not seen for at least 15 frames)
 tmp = []
 for b in bees:
示例#5
0
文件: lap.py 项目: yuchaz/glosim
def best_pairs(matrix):
    return linear_assignment(matrix)