Ejemplo n.º 1
0
/* Compute likely matches between two sets of keypoints */
std::vector<KeypointMatch> MatchKeys(const std::vector<KeypointWithDesc> &k1,
	const std::vector<KeypointWithDesc> &k2,
	bool registered, double ratio)
{
	int num_pts = 0;
	std::vector<KeypointMatch> matches;
	std::vector<KeypointWithDesc> pts;

	int *registered_idxs = NULL;

	//register为0时,pts保存k2的描述子,每一行是一个描述子
	if (!registered) {
		pts = k2;
		num_pts = (int)k2.size();
	}
	//registered为1时,registered_idxs保存k2中那些m_extra>=0的点的索引号
	else {
		registered_idxs = new int[(int)k2.size()];
		for (int i = 0; i < (int)k2.size(); i++) {
			if (k2[i].m_extra >= 0) {
				registered_idxs[num_pts] = i;
				pts.push_back(k2[i]);
				num_pts++;
			}
		}
	}


	for (int i = 0; i < (int)k1.size(); i++) 
	{
		int dist[2];
		int index = BruteForceMatch(k1[i], pts, dist);
		
		if (sqrt(((double)dist[0]) / ((double)dist[1])) <= ratio) {
			//registered为0时,matches保存k1 k2匹配的描述子序号对
			if (!registered) {
				matches.push_back(KeypointMatch(i, index));
			}
			//为1时,rnn_idx[0]为当前pts中与k1 i匹配的,registered_idxs[nn_idx[0]]k2的真正序号
			//这里实际上也是让matches保存k1 k2匹配的描述子序号对
			else {
				KeypointMatch match = KeypointMatch(i, registered_idxs[index]);
				matches.push_back(match);
			}
		}
	}


	int num_matches = (int)matches.size();
	printf("[MatchKeys] Found %d matches\n", num_matches);

	if (registered_idxs != NULL) delete registered_idxs;

	return matches;
}
Ejemplo n.º 2
0
void BaseApp::SetMatchesFromTracks(int img1, int img2)
{
    std::vector<int> &tracks1 = m_image_data[img1].m_visible_points;
    std::vector<int> &tracks2 = m_image_data[img2].m_visible_points;

    std::vector<int> isect = GetVectorIntersection(tracks1, tracks2);
    
    int num_isect = (int) isect.size();

    if (num_isect == 0)
        return;
    
    MatchIndex idx = GetMatchIndex(img1, img2);

    std::vector<KeypointMatch> &matches = m_matches.GetMatchList(idx); 
    // m_match_lists[idx];

    matches.clear();
    matches.resize(num_isect);

    for (int i = 0; i < num_isect; i++) {
        int tr = isect[i];

#if 0
        int num_views = (int) m_track_data[tr].m_views.size();
        int k1 = -1, k2 = -1;

        for (int j = 0; j < num_views; j++) {
            if (m_track_data[tr].m_views[j].first == img1) {
                k1 = m_track_data[tr].m_views[j].second;
            } 

            if (m_track_data[tr].m_views[j].first == img2) {
                k2 = m_track_data[tr].m_views[j].second;
            } 
        }

        assert(k1 != -1 && k2 != -1);
#endif

        std::pair<std::vector<int>::const_iterator, 
            std::vector<int>::const_iterator> p;
        const std::vector<int> &pt1 = m_image_data[img1].m_visible_points;
        p = equal_range(pt1.begin(), pt1.end(), tr);
        assert(p.first != p.second);
        int offset = p.first - pt1.begin();
        int k1 = m_image_data[img1].m_visible_keys[offset];

        const std::vector<int> &pt2 = m_image_data[img2].m_visible_points;
        p = equal_range(pt2.begin(), pt2.end(), tr);
        assert(p.first != p.second);
        offset = p.first - pt2.begin();
        int k2 = m_image_data[img2].m_visible_keys[offset];

        matches[i] = KeypointMatch(k1, k2);
    }
}
Ejemplo n.º 3
0
void BaseApp::SetMatchesFromTracks() 
{
    /* Clear all matches */
    // ClearMatches();
    RemoveAllMatches();

    int num_tracks = (int) m_track_data.size();

    int num_tracks_used = 0;
    for (int i = 0; i < num_tracks; i++) {
	TrackData &t = m_track_data[i];
	
	int num_views = (int) t.m_views.size();
	
	if (num_views < m_min_track_views) 
	    continue; /* Not enough observations */

	if (num_views > m_max_track_views) 
	    continue; /* Too many observations */

	for (int j = 0; j < num_views; j++) {
	    for (int k = 0; k < num_views; k++) {
		if (j == k) continue;
		
		int v1 = t.m_views[j].first;
		int v2 = t.m_views[k].first;
		
		int k1 = t.m_views[j].second;
		int k2 = t.m_views[k].second;
		
		MatchIndex idx = GetMatchIndex(v1, v2);

		// m_matches[idx] = true;
                SetMatch(v1, v2);
		// m_match_lists[idx].push_back(KeypointMatch(k1,k2));

                m_matches.GetMatchList(idx).push_back(KeypointMatch(k1, k2));
	    }
	}

        num_tracks_used++;
    }

    LOGI("[BaseApp::SetMatchesFromTracks] Used %d tracks\n", 
           num_tracks_used);
}
Ejemplo n.º 4
0
/* Compute likely matches between two sets of keypoints */
std::vector<KeypointMatch>
    MatchKeysExhaustive(const std::vector<KeypointWithDesc> &k1, 
                        const std::vector<KeypointWithDesc> &k2, 
                        bool registered, double ratio) 
{
    int num_pts = 0;
    std::vector<KeypointMatch> matches;

    int *registered_idxs = NULL;

    if (!registered) {
	num_pts = (int) k2.size();
    } else {
	registered_idxs = new int[(int) k2.size()];
	for (int i = 0; i < (int) k2.size(); i++) {
	    if (k2[i].m_extra >= 0) {
		registered_idxs[num_pts] = i;
		num_pts++;
	    }
	}
    }

    /* Create a new array of points */
    ann_1_1_char::ANNpointArray pts = ann_1_1_char::annAllocPts(num_pts, 128);

    if (!registered) {
	for (int i = 0; i < num_pts; i++) {
	    int j;

	    for (j = 0; j < 128; j++) {
		pts[i][j] = k2[i].m_d[j];
	    }
	}
    } else {
	for (int i = 0; i < num_pts; i++) {
	    int j;
	    int idx = registered_idxs[i];

	    for (j = 0; j < 128; j++) {
		pts[i][j] = k2[idx].m_d[j];
	    }
	}	
    }
    
    // clock_t start = clock();
    /* Create a search tree for k2 */
    ann_1_1_char::ANNkd_tree *tree = 
        new ann_1_1_char::ANNkd_tree(pts, num_pts, 128, 4);
    // clock_t end = clock();
    
    // printf("Building tree took %0.3fs\n", 
    //        (end - start) / ((double) CLOCKS_PER_SEC));

    /* Now do the search */
    ann_1_1_char::ANNpoint query = ann_1_1_char::annAllocPt(128);
    // start = clock();
    for (int i = 0; i < (int) k1.size(); i++) {
	int j;

	for (j = 0; j < 128; j++) {
	    query[j] = k1[i].m_d[j];
	}

	ann_1_1_char::ANNidx nn_idx[2];
	ann_1_1_char::ANNdist dist[2];

	tree->annkSearch(query, 2, nn_idx, dist, 0.0);

	if (sqrt(((double) dist[0]) / ((double) dist[1])) <= ratio) {
	    if (!registered) {
		matches.push_back(KeypointMatch(i, nn_idx[0]));
	    } else {
		KeypointMatch match = 
		    KeypointMatch(i, registered_idxs[nn_idx[0]]);
		matches.push_back(match);
	    }
	}
    }
    // end = clock();
    // printf("Searching tree took %0.3fs\n",
    //        (end - start) / ((double) CLOCKS_PER_SEC));

    int num_matches = (int) matches.size();

    printf("[MatchKeys] Found %d matches\n", num_matches);

    /* Cleanup */
    ann_1_1_char::annDeallocPts(pts);
    ann_1_1_char::annDeallocPt(query);

    delete tree;

    return matches;
}
Ejemplo n.º 5
0
/* Compute likely matches between two sets of keypoints */
std::vector<KeypointMatch> MatchKeys(const std::vector<KeypointWithDesc> &k1, 
				     const std::vector<KeypointWithDesc> &k2, 
				     bool registered, double ratio) 
{
    ann_1_1_char::annMaxPtsVisit(200);

    int num_pts = 0;
    std::vector<KeypointMatch> matches;

    int *registered_idxs = NULL;
	
    if (!registered) {
		num_pts = (int) k2.size();
    } 
	//registered为1时,registered_idxs保存k2中那些m_extra>=0的点的索引号
	else {
		registered_idxs = new int[(int) k2.size()];
		for (int i = 0; i < (int) k2.size(); i++) {
			if (k2[i].m_extra >= 0) {
				registered_idxs[num_pts] = i;
				num_pts++;
			}
		}
    }

    /* Create a new array of points */
	ann_1_1_char::ANNpointArray pts = ann_1_1_char::annAllocPts(num_pts, DESCRIPTOR_LENGTH);

	//register为0时,pts保存k2的描述子,每一行是一个描述子
    if (!registered) {
		for (int i = 0; i < num_pts; i++) {
			int j;

			for (j = 0; j < DESCRIPTOR_LENGTH; j++) {
				pts[i][j] = k2[i].m_d[j];
		    }
		}
    } 
	//registered为1时,pts保存k2中m_extra>=0的点的描述子
	else {
		for (int i = 0; i < num_pts; i++) {
			int j;
			int idx = registered_idxs[i];

			for (j = 0; j < DESCRIPTOR_LENGTH; j++) {
				pts[i][j] = k2[idx].m_d[j];
		    }
		}	
    }
    
    // clock_t start = clock();
    /* Create a search tree for k2 */
	ann_1_1_char::ANNkd_tree *tree = new ann_1_1_char::ANNkd_tree(pts, num_pts, DESCRIPTOR_LENGTH, 4);
    // clock_t end = clock();
    
    // printf("Building tree took %0.3fs\n", 
    //        (end - start) / ((double) CLOCKS_PER_SEC));

    /* Now do the search */
	ann_1_1_char::ANNpoint query = ann_1_1_char::annAllocPt(DESCRIPTOR_LENGTH);
    // start = clock();
    for (int i = 0; i < (int) k1.size(); i++) {
		int j;

		for (j = 0; j < DESCRIPTOR_LENGTH; j++) {
			query[j] = k1[i].m_d[j];
		}

		ann_1_1_char::ANNidx nn_idx[2];
		ann_1_1_char::ANNdist dist[2];

		tree->annkPriSearch(query, 2, nn_idx, dist, 0.0);

		if (sqrt(((double) dist[0]) / ((double) dist[1])) <= ratio) {
			//registered为0时,matches保存k1 k2匹配的描述子序号对
			if (!registered) {
				matches.push_back(KeypointMatch(i, nn_idx[0]));
			} 
			//为1时,rnn_idx[0]为当前pts中与k1 i匹配的,registered_idxs[nn_idx[0]]k2的真正序号
			//这里实际上也是让matches保存k1 k2匹配的描述子序号对
			else {
				KeypointMatch match = KeypointMatch(i, registered_idxs[nn_idx[0]]);
				matches.push_back(match);
			}
		}
    }
    // end = clock();
    // printf("Searching tree took %0.3fs\n",
    //        (end - start) / ((double) CLOCKS_PER_SEC));

    int num_matches = (int) matches.size();

    printf("[MatchKeys] Found %d matches\n", num_matches);

    /* Cleanup */
    ann_1_1_char::annDeallocPts(pts);
    ann_1_1_char::annDeallocPt(query);

    delete tree;

    return matches;
}
/* Compute likely matches between the reconstructed points and a set
 * of keypoints and the reconstructed points */
std::vector<KeypointMatch> 
   BundlerApp::MatchPointsToKeys(const std::vector<KeypointWithDesc> &keys, 
                                 double ratio) 
{
	printf("MatchKeysToPoints, error\n");
	exit(EXIT_FAILURE);

    ann_1_1_char::annMaxPtsVisit(200);

    int num_points = (int) keys.size();
    std::vector<KeypointMatch> matches;

    /* Create a new array of points */
    ann_1_1_char::ANNpointArray pts = ann_1_1_char::annAllocPts(num_points, DESCRIPTOR_LENGTH);

    for (int i = 0; i < num_points; i++)
	for (int j = 0; j < DESCRIPTOR_LENGTH; j++)
	    pts[i][j] = keys[i].m_d[j];
    
    clock_t start = clock();
    /* Create a search tree for k2 */
    ann_1_1_char::ANNkd_tree *tree = new ann_1_1_char::ANNkd_tree(pts, num_points, DESCRIPTOR_LENGTH, 4);
    clock_t end = clock();
    
    printf("[MatchPointsToKeys] Building tree took %0.3fs\n", 
	   (end - start) / ((double) CLOCKS_PER_SEC));

    /* Now do the search */
    ann_1_1_char::ANNpoint query = ann_1_1_char::annAllocPt(DESCRIPTOR_LENGTH);
    start = clock();
    for (int i = 0; i < (int) m_point_data.size(); i++) {
	int j;

	for (j = 0; j < DESCRIPTOR_LENGTH; j++) {
	    query[j] = m_point_data[i].m_desc[j];
	}

	ann_1_1_char::ANNidx nn_idx[2];
	ann_1_1_char::ANNdist dist[2];

	tree->annkPriSearch(query, 2, nn_idx, dist, 0.0);

	if (sqrt(((double) dist[0]) / ((double) dist[1])) < ratio) {
	    matches.push_back(KeypointMatch(i, nn_idx[0]));
	}
    }
    end = clock();

    printf("[MatchPointsToKeys] Searching tree took %0.3fs\n",
	   (end - start) / ((double) CLOCKS_PER_SEC));

    int num_matches = (int) matches.size();

    printf("[MatchPointsToKeys] Found %d matches\n", num_matches);
    fflush(stdout);
    
    /* Cleanup */
    ann_1_1_char::annDeallocPts(pts);
    ann_1_1_char::annDeallocPt(query);

    delete tree;

    return matches;
}