(self, features1, features2)
return good_matches
Match the features.　Output the matched indices.
        Returns:
            good_matches {dict}: a dict which matches the 
                `index of features2` to `index of features1`.
Match the features.　Output the matched indices.
        Returns:
            good_matches {dict}: a dict which matches the 
                `index of features2` to `index of features1`.
Match the features . Output the matched indices . Returns : good_matches { dict } : a dict which matches the index of features2 to index of features1 .
def _match_features(self, features1, features2):
        ''' Match the features.　Output the matched indices.
        Returns:
            good_matches {dict}: a dict which matches the 
                `index of features2` to `index of features1`.
        '''
        features1, features2 = np.array(features1), np.array(features2)

        #cost = lambda x1, x2: np.linalg.norm(x1-x2)
        def calc_dist(p1, p2): return (
            (p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)**0.5

        def cost(sk1, sk2):

            # neck, shoulder, elbow, hip, knee
            joints = np.array([2, 3, 4, 5, 6, 7, 10, 11, 12,
                               13, 16, 17, 18, 19, 22, 23, 24, 25])

            sk1, sk2 = sk1[joints], sk2[joints]
            valid_idx = np.logical_and(sk1 != 0, sk2 != 0)
            sk1, sk2 = sk1[valid_idx], sk2[valid_idx]
            sum_dist, num_points = 0, int(len(sk1)/2)
            if num_points == 0:
                return 99999
            else:
                for i in range(num_points):  # compute distance between each pair of joint
                    idx = i * 2
                    sum_dist += calc_dist(sk1[idx:idx+2], sk2[idx:idx+2])
                mean_dist = sum_dist / num_points
                mean_dist /= (1.0 + 0.05*num_points)  # more points, the better
                return mean_dist

        # If f1i is matched to f2j and vice versa, the match is good.
        good_matches = {}
        n1, n2 = len(features1), len(features2)
        if n1 and n2:

            # dist_matrix[i][j] is the distance between features[i] and features[j]
            dist_matrix = [[cost(f1, f2) for f2 in features2]
                           for f1 in features1]
            dist_matrix = np.array(dist_matrix)

            # Find the match of features1[i]
            matches_f1_to_f2 = [dist_matrix[row, :].argmin()
                                for row in range(n1)]

            # Find the match of features2[i]
            matches_f2_to_f1 = [dist_matrix[:, col].argmin()
                                for col in range(n2)]

            for i1, i2 in enumerate(matches_f1_to_f2):
                if matches_f2_to_f1[i2] == i1 and dist_matrix[i1, i2] < self._dist_thresh:
                    good_matches[i2] = i1

            if 0:
                print("distance matrix:", dist_matrix)
                print("matches_f1_to_f2:", matches_f1_to_f2)
                print("matches_f1_to_f2:", matches_f2_to_f1)
                print("good_matches:", good_matches)

        return good_matches
def _match_features ( self , features1 , features2 ) : features1 , features2 = np . array ( features1 ) , np . array ( features2 ) #cost = lambda x1, x2: np.linalg.norm(x1-x2) def calc_dist ( p1 , p2 ) : return ( ( p1 [ 0 ] - p2 [ 0 ] ) ** 2 + ( p1 [ 1 ] - p2 [ 1 ] ) ** 2 ) ** 0.5 def cost ( sk1 , sk2 ) : # neck, shoulder, elbow, hip, knee joints = np . array ( [ 2 , 3 , 4 , 5 , 6 , 7 , 10 , 11 , 12 , 13 , 16 , 17 , 18 , 19 , 22 , 23 , 24 , 25 ] ) sk1 , sk2 = sk1 [ joints ] , sk2 [ joints ] valid_idx = np . logical_and ( sk1 != 0 , sk2 != 0 ) sk1 , sk2 = sk1 [ valid_idx ] , sk2 [ valid_idx ] sum_dist , num_points = 0 , int ( len ( sk1 ) / 2 ) if num_points == 0 : return 99999 else : for i in range ( num_points ) : # compute distance between each pair of joint idx = i * 2 sum_dist += calc_dist ( sk1 [ idx : idx + 2 ] , sk2 [ idx : idx + 2 ] ) mean_dist = sum_dist / num_points mean_dist /= ( 1.0 + 0.05 * num_points ) # more points, the better return mean_dist # If f1i is matched to f2j and vice versa, the match is good. good_matches = { } n1 , n2 = len ( features1 ) , len ( features2 ) if n1 and n2 : # dist_matrix[i][j] is the distance between features[i] and features[j] dist_matrix = [ [ cost ( f1 , f2 ) for f2 in features2 ] for f1 in features1 ] dist_matrix = np . array ( dist_matrix ) # Find the match of features1[i] matches_f1_to_f2 = [ dist_matrix [ row , : ] . argmin ( ) for row in range ( n1 ) ] # Find the match of features2[i] matches_f2_to_f1 = [ dist_matrix [ : , col ] . argmin ( ) for col in range ( n2 ) ] for i1 , i2 in enumerate ( matches_f1_to_f2 ) : if matches_f2_to_f1 [ i2 ] == i1 and dist_matrix [ i1 , i2 ] < self . _dist_thresh : good_matches [ i2 ] = i1 if 0 : print ( "distance matrix:" , dist_matrix ) print ( "matches_f1_to_f2:" , matches_f1_to_f2 ) print ( "matches_f1_to_f2:" , matches_f2_to_f1 ) print ( "good_matches:" , good_matches ) return good_matches