Пример #1
0
int main(int argc, char **argv) 
{
    const int dim = 128;

    if (argc != 6 && argc != 7 && argc != 8 && argc != 9 && argc != 10 && 
        argc != 11) {
        printf("Usage: %s <tree.in> <db.in> <query.in> <num_nbrs> "
               "<match-script.out> [leaves_only] [distance_type] [normalize] "
               "[min_feature_scale] [max_keys]\n",
               argv[0]);
        return 1;
    }
    
    char *tree_in = argv[1];
    char *db_in = argv[2];
    char *query_in = argv[3];
    int num_nbrs = atoi(argv[4]);
    char *matches_out = argv[5];
    bool leaves_only = false;
    bool normalize = true;
    double min_feature_scale = 0.0;
    DistanceType distance_type = DistanceMin;
    int max_keys = 0;

    if (argc >= 7) {
        if (atoi(argv[6]) != 0)
            leaves_only = true;
    }

    if (argc >= 8)
        distance_type = (DistanceType) atoi(argv[7]);

    if (argc >= 9)
        if (atoi(argv[8]) == 0)
            normalize = false;

    if (argc >= 10) {
        min_feature_scale = atof(argv[9]);
    }

    if (argc >= 11) {
        max_keys = atoi(argv[10]);
    }

    if (leaves_only) {
        printf("[VocabMatch] Scoring with leaves only\n");
    } else {
        printf("[VocabMatch] Scoring with all nodes\n");
    }

    printf("[VocabMatch] Using tree %s\n", tree_in);
    printf("[VocabMatch] min_feature_scale = %0.3f\n", min_feature_scale);
    printf("[VocabMatch] max_keys = %d\n", max_keys);

    switch (distance_type) {
    case DistanceDot:
        printf("[VocabMatch] Using distance Dot\n");
        break;        
    case DistanceMin:
        printf("[VocabMatch] Using distance Min\n");
        break;
    default:
        printf("[VocabMatch] Using no known distance!\n");
        break;
    }

    /* Read the tree */
    printf("[VocabMatch] Reading tree...\n");
    fflush(stdout);

    clock_t start = clock();
    VocabTree tree;
    tree.Read(tree_in);
    clock_t end = clock();
    printf("[VocabMatch] Read tree in %0.3fs\n", 
           (double) (end - start) / CLOCKS_PER_SEC);

#if 1
    tree.Flatten();
#endif

    tree.SetDistanceType(distance_type);

    if (leaves_only) {
        tree.SetInteriorNodeWeight(atoi(argv[6]) - 1, 0.0);
        // #define CONSTANT_WEIGHTS
#ifdef CONSTANT_WEIGHTS
        tree.SetConstantLeafWeights();
#endif
    }
    
    /* Read the database keyfiles */
    FILE *f = fopen(db_in, "r");
    
    std::vector<std::string> db_files;
    char buf[256];
    while (fgets(buf, 256, f)) {
        /* Remove trailing newline */
        if (buf[strlen(buf) - 1] == '\n')
            buf[strlen(buf) - 1] = 0;

        db_files.push_back(std::string(buf));
    }

    fclose(f);

    /* Read the query keyfiles */
    f = fopen(query_in, "r");
    
    std::vector<std::string> query_files;
    std::vector<int> query_indices;
    while (fgets(buf, 256, f)) {
        /* Remove trailing newline */
        if (buf[strlen(buf) - 1] == '\n')
            buf[strlen(buf) - 1] = 0;

        char keyfile[256];
        int index;
        sscanf(buf, "%d %s", &index, keyfile);

        query_files.push_back(std::string(keyfile));
        query_indices.push_back(index);
    }

    fclose(f);

    /* Populate the database */
    printf("[VocabMatch] Populating database...\n");
    fflush(stdout);

    int num_db_images = db_files.size();
    int num_query_images = query_files.size();

    /* Now score each query keyfile */
    printf("[VocabMatch] Scoring query images...\n");
    fflush(stdout);

    float *scores = new float[num_db_images];
    double *scores_d = new double[num_db_images];
    int *perm = new int[num_db_images];

    FILE *f_match = fopen(matches_out, "w");
    if (f_match == NULL) {
        printf("[VocabMatch] Error opening file %s for writing\n",
               matches_out);
        return 1;
    }

    for (int i = 0; i < num_query_images; i++) {
        int index_i = query_indices[i];

        start = clock();

        /* Clear scores */
        for (int j = 0; j < num_db_images; j++) 
            scores[j] = 0.0;

        unsigned char *keys;
        int num_keys;

        keys = ReadAndFilterKeys(query_files[i].c_str(), dim, 
                                 min_feature_scale, max_keys, num_keys);

        tree.ScoreQueryKeys(num_keys, /*i,*/ true, keys, scores);

        end = clock();
        printf("[VocabMatch] Scored image %s (%d keys) in %0.3fs\n", 
               query_files[i].c_str(), num_keys,
               (double) (end - start) / CLOCKS_PER_SEC);

#if 0
        for (int j = 0; j < num_db_images; j++) {
            /* Normalize scores */
            if (magnitudes[j] > 0.0)
                scores[j] /= magnitudes[j];
            else 
                scores[j] = 0.0;
        }
#endif

        /* Find the top scores */
        for (int j = 0; j < num_db_images; j++) {
            scores_d[j] = (double) scores[j];
        }

        qsort_descending();
        qsort_perm(num_db_images, scores_d, perm);        
        // assert(is_sorted(num_db_images, scores_d));

        int top = MIN(num_nbrs+1, num_db_images);

        for (int j = 0; j < top; j++) {
            if (perm[j] == index_i)
                continue;
            fprintf(f_match, "%d %d %0.5e\n", index_i, perm[j], scores_d[j]);
            fflush(f_match);
        }
        
        fflush(stdout);

        delete [] keys;
    }

    fclose(f_match);

    delete [] scores;
    delete [] scores_d;
    delete [] perm;

    return 0;
}
Пример #2
0
int main(int argc, char **argv) 
{
    const int dim = 128;

    if (argc != 6 && argc != 7 && argc != 8) {
        printf("Usage: %s <tree.in> <db.in> <query.in> <num_nbrs> "
               "<matches.out> [distance_type:1] [normalize:1]\n", argv[0]);
        return 1;
    }
    
    char *tree_in = argv[1];
    char *db_in = argv[2];
    char *query_in = argv[3];
    int num_nbrs = atoi(argv[4]);
    char *matches_out = argv[5];
    DistanceType distance_type = DistanceMin;
    bool normalize = true;

#if 0    
    if (argc >= 7)
        output_html = argv[6];
#endif

    if (argc >= 7)
        distance_type = (DistanceType) atoi(argv[6]);

    if (argc >= 8)
        normalize = (atoi(argv[7]) != 0);

    printf("[VocabMatch] Using tree %s\n", tree_in);

    switch (distance_type) {
    case DistanceDot:
        printf("[VocabMatch] Using distance Dot\n");
        break;        
    case DistanceMin:
        printf("[VocabMatch] Using distance Min\n");
        break;
    default:
        printf("[VocabMatch] Using no known distance!\n");
        break;
    }

    /* Read the tree */
    printf("[VocabMatch] Reading tree...\n");
    fflush(stdout);

    clock_t start = clock();
    VocabTree tree;
    tree.Read(tree_in);

    clock_t end = clock();
    printf("[VocabMatch] Read tree in %0.3fs\n", 
           (double) (end - start) / CLOCKS_PER_SEC);

#if 1
    tree.Flatten();
#endif

    tree.SetDistanceType(distance_type);
    tree.SetInteriorNodeWeight(0, 0.0);
    
    /* Read the database keyfiles */
    FILE *f = fopen(db_in, "r");
    
    std::vector<std::string> db_files;
    char buf[256];
    while (fgets(buf, 256, f)) {
        /* Remove trailing newline */
        if (buf[strlen(buf) - 1] == '\n')
            buf[strlen(buf) - 1] = 0;

        db_files.push_back(std::string(buf));
    }

    fclose(f);

    /* Read the query keyfiles */
    f = fopen(query_in, "r");
    
    std::vector<std::string> query_files;
    while (fgets(buf, 256, f)) {
        /* Remove trailing newline */
        if (buf[strlen(buf) - 1] == '\n')
            buf[strlen(buf) - 1] = 0;

        char keyfile[256];
        sscanf(buf, "%s", keyfile);

        query_files.push_back(std::string(keyfile));
    }

    fclose(f);

    int num_db_images = db_files.size();
    int num_query_images = query_files.size();

    printf("[VocabMatch] Read %d database images\n", num_db_images);

    /* Now score each query keyfile */
    printf("[VocabMatch] Scoring %d query images...\n", num_query_images);
    fflush(stdout);

#if 0
    FILE *f_html = fopen(output_html, "w");
    PrintHTMLHeader(f_html, num_nbrs);
#endif

    float *scores = new float[num_db_images];
    double *scores_d = new double[num_db_images];
    int *perm = new int[num_db_images];

    FILE *f_match = fopen(matches_out, "w");
    if (f_match == NULL) {
        printf("[VocabMatch] Error opening file %s for writing\n",
               matches_out);
        return 1;
    }

    for (int i = 0; i < num_query_images; i++) {
        start = clock();

        /* Clear scores */
        for (int j = 0; j < num_db_images; j++) 
            scores[j] = 0.0;

        int num_keys = 0;
        unsigned char *keys = ReadDescriptorFile(query_files[i].c_str(), 
                                                 dim, num_keys);

        clock_t start_score = clock();
        double mag = tree.ScoreQueryKeys(num_keys, normalize, keys, scores);
        clock_t end_score = end = clock();

        printf("[VocabMatch] Scored image %s in %0.3fs "
               "( %0.3fs total, num_keys = %d, mag = %0.3f )\n", 
               query_files[i].c_str(), 
               (double) (end_score - start_score) / CLOCKS_PER_SEC,
               (double) (end - start) / CLOCKS_PER_SEC, num_keys, mag);

        /* Find the top scores */
        for (int j = 0; j < num_db_images; j++) {
            scores_d[j] = (double) scores[j];
        }

        qsort_descending();
        qsort_perm(num_db_images, scores_d, perm);        

        int top = MIN(num_nbrs, num_db_images);

        for (int j = 0; j < top; j++) {
            // if (perm[j] == index_i)
            //     continue;
            
            fprintf(f_match, "%d %d %0.4f\n", i, perm[j], scores_d[j]);
            //fprintf(f_match, "%d %d %0.4f\n", i, perm[j], mag - scores_d[j]);
        }
        
        fflush(f_match);
        fflush(stdout);

#if 0
        PrintHTMLRow(f_html, query_files[i], scores_d, 
                     perm, num_nbrs, db_files);
#endif

        delete [] keys;
    }

    fclose(f_match);

#if 0
    PrintHTMLFooter(f_html);
    fclose(f_html);
#endif

    delete [] scores;
    delete [] scores_d;
    delete [] perm;

    return 0;
}
Пример #3
0
//Read a database stored as a vocab tree and score a set of query images 
int VocabMatch(char *db_in, char *list_in, char *query_in, int num_nbrs, char *matches_out, DistanceType distance_type = DistanceMin, int normalize = 1)
{
	const int dim = 128;

	printf("[VocabMatch] Using database %s\n", db_in);
	switch (distance_type)
	{
	case DistanceDot:
		printf("[VocabMatch] Using distance Dot\n");
		break;
	case DistanceMin:
		printf("[VocabMatch] Using distance Min\n");
		break;
	default:
		printf("[VocabMatch] Using no known distance!\n");
		break;
	}

	// Read the tree 
	printf("[VocabMatch] Reading database...\n"); fflush(stdout);
	clock_t start = clock();
	VocabTree tree;
	tree.Read(db_in);
	clock_t end = clock();
	printf("[VocabMatch] Read database in %0.3fs\n", (double)(end - start) / CLOCKS_PER_SEC);

	tree.Flatten();
	tree.SetDistanceType(distance_type);
	tree.SetInteriorNodeWeight(0, 0.0);

	// Read the database keyfiles 
	FILE *f = fopen(list_in, "r");
	if (f == NULL)
	{
		printf("Could not open file: %s\n", list_in);
		return 1;
	}

	std::vector<std::string> db_files;
	char buf[256];
	while (fgets(buf, 256, f))
	{
		// Remove trailing newline 
		if (buf[strlen(buf) - 1] == '\n')
			buf[strlen(buf) - 1] = 0;

		db_files.push_back(std::string(buf));
	}
	fclose(f);

	//Read the query keyfiles
	f = fopen(query_in, "r");
	if (f == NULL)
	{
		printf("Could not open file: %s\n", query_in);
		return 1;
	}

	std::vector<std::string> query_files;
	while (fgets(buf, 256, f))
	{
		// Remove trailing newline 
		if (buf[strlen(buf) - 1] == '\n')
			buf[strlen(buf) - 1] = 0;

		char keyfile[256]; sscanf(buf, "%s", keyfile);
		query_files.push_back(std::string(keyfile));
	}
	fclose(f);

	int num_db_images = (int)db_files.size();
	int num_query_images = (int)query_files.size();
	printf("[VocabMatch] Read %d database images\n", num_db_images);

	//Now score each query keyfile
	printf("[VocabMatch] Scoring %d query images...\n", num_query_images); fflush(stdout);

	float *scores = new float[num_db_images];
	double *scores_d = new double[num_db_images];
	int *perm = new int[num_db_images];

	FILE *f_match = fopen(matches_out, "w");
	if (f_match == NULL)
	{
		printf("[VocabMatch] Error opening file %s for writing\n", matches_out);
		return 1;
	}

	bool bnormalize = normalize == 1 ? true : false;
	for (int i = 0; i < num_query_images; i++)
	{
		start = clock();

		for (int j = 0; j < num_db_images; j++)
			scores[j] = 0.0;

		int num_keys;
		unsigned char *keys = ReadKeys(query_files[i].c_str(), dim, num_keys);

		clock_t start_score = clock();
		double mag = tree.ScoreQueryKeys(num_keys, bnormalize, keys, scores);
		clock_t end_score = end = clock();

		printf("[VocabMatch] Scored image %s in %0.3fs ( %0.3fs total, num_keys = %d, mag = %0.3f )\n", query_files[i].c_str(), (double)(end_score - start_score) / CLOCKS_PER_SEC, (double)(end - start) / CLOCKS_PER_SEC, num_keys, mag);

		//Find the top scores 
		for (int j = 0; j < num_db_images; j++)
			scores_d[j] = (double)scores[j];

		qsort_descending();
		qsort_perm(num_db_images, scores_d, perm);

		int top = MIN(num_nbrs, num_db_images);

		for (int j = 0; j < top; j++)
		{
			// if (perm[j] == index_i)
			//     continue;
			fprintf(f_match, "%d %d %0.4f\n", i, perm[j], scores_d[j]);
			//fprintf(f_match, "%d %d %0.4f\n", i, perm[j], mag - scores_d[j]);
		}
		fflush(f_match); fflush(stdout);
		delete[] keys;
	}
	fclose(f_match);

	delete[] scores, delete[] scores_d, delete[] perm;
	return 0;
}