コード例 #1
0
ファイル: main.cpp プロジェクト: minhpvo/VocabTree
// combining several vocab trees into one
int combineVocab(vector<string> &allTreesIn, char *tree_out)
{
	int num_trees = (int)allTreesIn.size();

	VocabTree tree;
	tree.Read(allTreesIn[0].c_str());

	//Start with the second tree, as we just read the first one
	for (int i = 1; i < num_trees; i++)
	{
		printf("[VocabCombine] Adding tree %d [%s]...\n", i, allTreesIn[i].c_str());
		fflush(stdout);

		VocabTree tree_add;
		tree_add.Read(allTreesIn[i].c_str());
		tree.Combine(tree_add);
		tree_add.Clear();
	}

	//Now do the reweighting:
	int total_num_db_images = tree.GetMaxDatabaseImageIndex() + 1;
	printf("Total num_db_images: %d\n", total_num_db_images);
	tree.ComputeTFIDFWeights(total_num_db_images);
	tree.NormalizeDatabase(0, total_num_db_images);
	tree.Write(tree_out);

	//Write vectors to a file 
	tree.WriteDatabaseVectors("vectors_all.txt", 0, total_num_db_images);

	return 0;
}
コード例 #2
0
ファイル: VocabCombine.cpp プロジェクト: Aleem21/VocabTree2
int main(int argc, char **argv) 
{
    if (argc < 3) {
        printf("Usage: %s <tree1.in> <tree2.in> ... <tree.out>\n",
               argv[0]);

        return 1;
    }

    int num_trees = argc - 2;
    
    char *tree_out = argv[argc-1];

    VocabTree tree;
    tree.Read(argv[1]);

    /* Start with the second tree, as we just read the first one */
    for (int i = 1; i < num_trees; i++) {
        printf("[VocabCombine] Adding tree %d [%s]...\n", i, argv[i+1]);
        fflush(stdout);

        VocabTree tree_add;
        tree_add.Read(argv[i+1]);
        tree.Combine(tree_add);
        tree_add.Clear();
    }

    /* Now do the reweighting */
    // if (use_tfidf)
    int total_num_db_images = tree.GetMaxDatabaseImageIndex() + 1;
    printf("Total num_db_images: %d\n", total_num_db_images);
    tree.ComputeTFIDFWeights(total_num_db_images);
    tree.NormalizeDatabase(0, total_num_db_images);
    tree.Write(tree_out);

    /* Write vectors to a file */
    tree.WriteDatabaseVectors("vectors_all.txt", 0, total_num_db_images);

    return 0;
}
コード例 #3
0
int main(int argc, char **argv) 
{
    if (argc < 4 || argc > 8) {
        printf("Usage: %s <list.in> <tree.in> <tree.out> [use_tfidf:1] "
               "[normalize:1] [start_id:0] [distance_type:1]\n", 
               argv[0]);

        return 1;
    }

    double min_feature_scale = 1.4;
    bool use_tfidf = true;
    bool normalize = true;
    
    char *list_in = argv[1];
    char *tree_in = argv[2];
    char *tree_out = argv[3];
    DistanceType distance_type = DistanceMin;
    int start_id = 0;

    if (argc >= 5)
        use_tfidf = atoi(argv[4]);

    if (argc >= 6)
        normalize = atoi(argv[5]);

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

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

    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;
    }

    FILE *f = fopen(list_in, "r");
    
    if (f == NULL) {
        printf("Error opening file %s for reading\n", list_in);
        return 1;
    }

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

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

    printf("[VocabBuildDB] Reading tree %s...\n", tree_in);
    fflush(stdout);

    VocabTree tree;
    tree.Read(tree_in);

#if 1
    tree.Flatten();
#endif

    tree.m_distance_type = distance_type;
    tree.SetInteriorNodeWeight(0.0);

    /* Initialize leaf weights to 1.0 */
    tree.SetConstantLeafWeights();

    const int dim = 128;
    int num_db_images = (int) key_files.size();
    unsigned long count = 0;

    tree.ClearDatabase();

    for (int i = 0; i < num_db_images; i++) {
        int num_keys = 0;
        unsigned char *keys = ReadAndFilterKeys(key_files[i].c_str(), 
                                                dim, min_feature_scale,
                                                0, num_keys);

        printf("[VocabBuildDB] Adding vector %d (%d keys)\n", 
               start_id + i, num_keys);
        tree.AddImageToDatabase(start_id + i, num_keys, keys);

        if (num_keys > 0) 
            delete [] keys;
    }

    printf("[VocabBuildDB] Pushed %lu features\n", count);
    fflush(stdout);

    if (use_tfidf)
        tree.ComputeTFIDFWeights(num_db_images);

    if (normalize) 
        tree.NormalizeDatabase(start_id, num_db_images);

    printf("[VocabBuildDB] Writing tree...\n");
    tree.Write(tree_out);

    // char filename[256];
    // sprintf(filename, "vectors_%03d.txt", start_id);
    // tree.WriteDatabaseVectors(filename, start_id, num_db_images);

    return 0;
}
コード例 #4
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;
}
コード例 #5
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;
}
コード例 #6
0
ファイル: main.cpp プロジェクト: minhpvo/VocabTree
// compare images feature using db
int VocabCompare(int argc, char **argv)
{
	if (argc != 5 && argc != 6)
	{
		printf("Usage: %s <tree.in> <image1.key> <image2.key> <matches.out> [distance_type]\n", argv[0]);
		return 1;
	}

	char *tree_in = argv[1];
	char *image1_in = argv[2];
	char *image2_in = argv[3];
	char *matches_out = argv[4];
	DistanceType distance_type = DistanceMin;

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

	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;
	}

	printf("[VocabBuildDB] Reading tree %s...\n", tree_in); fflush(stdout);
	VocabTree tree;
	tree.Read(tree_in);

	printf("[VocabCompare] Flattening tree...\n");
	tree.Flatten();
	tree.m_distance_type = distance_type;
	tree.SetInteriorNodeWeight(0.0);

	/* Initialize leaf weights to 1.0 */
	tree.SetConstantLeafWeights();

	const int dim = 128;

	tree.ClearDatabase();

	int num_keys_1 = 0, num_keys_2 = 0;
	unsigned char *keys1 = ReadKeys(image1_in, dim, num_keys_1);
	unsigned char *keys2 = ReadKeys(image2_in, dim, num_keys_2);

	unsigned long *ids1 = new unsigned long[num_keys_1];
	unsigned long *ids2 = new unsigned long[num_keys_2];

	printf("[VocabCompare] Adding image 0 (%d keys)\n", num_keys_1);
	tree.AddImageToDatabase(0, num_keys_1, keys1, ids1);
	if (num_keys_1 > 0)
		delete[] keys1;

	printf("[VocabCompare] Adding image 1 (%d keys)\n", num_keys_2);
	tree.AddImageToDatabase(1, num_keys_2, keys2, ids2);
	if (num_keys_2 > 0)
		delete[] keys2;

	// tree.ComputeTFIDFWeights();
	tree.NormalizeDatabase(0, 2);

	//Find collisions among visual word IDs 
	std::multimap<unsigned long, unsigned int> word_map;
	for (unsigned int i = 0; i < (unsigned int)num_keys_1; i++)
	{
		printf("0 %d -> %lu\n", i, ids1[i]);
		std::pair<unsigned long, unsigned int> elem(ids1[i], i);
		word_map.insert(elem);
	}

	//Count number of matches 
	int num_matches = 0;
	for (unsigned int i = 0; i < (unsigned int)num_keys_2; i++)
	{
		unsigned long id = ids2[i];
		printf("1 %d -> %lu\n", i, ids2[i]);

		std::pair<std::multimap<unsigned long, unsigned int>::iterator, std::multimap<unsigned long, unsigned int>::iterator> ret;
		ret = word_map.equal_range(id);

		unsigned int size = 0;
		std::multimap<unsigned long, unsigned int>::iterator iter;
		for (iter = ret.first; iter != ret.second; iter++)
			size++, num_matches++;

		if (size > 0)
			printf("size[%lu] = %d\n", id, size);
	}
	printf("number of matches: %d\n", num_matches); fflush(stdout);

	FILE *f = fopen(matches_out, "w");
	if (f == NULL)
	{
		printf("Error opening file %s for writing\n", matches_out);
		return 1;
	}
	fprintf(f, "%d\n", num_matches);

	/* Write matches */
	for (unsigned int i = 0; i < (unsigned int)num_keys_2; i++)
	{
		unsigned long id = ids2[i];

		std::pair<std::multimap<unsigned long, unsigned int>::iterator, std::multimap<unsigned long, unsigned int>::iterator> ret;
		ret = word_map.equal_range(id);

		std::multimap<unsigned long, unsigned int>::iterator iter;
		for (iter = ret.first; iter != ret.second; iter++)
			fprintf(f, "%d %d\n", iter->second, i);
	}
	fclose(f);

	// printf("[VocabBuildDB] Writing tree...\n");
	// tree.Write(tree_out);

	return 0;
}
コード例 #7
0
ファイル: main.cpp プロジェクト: minhpvo/VocabTree
//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;
}
コード例 #8
0
ファイル: main.cpp プロジェクト: minhpvo/VocabTree
// Building a database with a vocabulary tree
int VocabDB(char *list_in, char *tree_in, char *db_out, int use_tfidf = 1, int normalize = 1, int start_id = 0, DistanceType distance_type = DistanceMin)
{
	double min_feature_scale = 1.4;

	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;
	}

	FILE *f = fopen(list_in, "r");
	if (f == NULL) {
		printf("Error opening file %s for reading\n", list_in);
		return 1;
	}

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

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

	printf("[VocabBuildDB] Reading tree %s...\n", tree_in);
	fflush(stdout);

	VocabTree tree;
	tree.Read(tree_in);
	tree.Flatten();
	tree.m_distance_type = distance_type;
	tree.SetInteriorNodeWeight(0.0);

	//Initialize leaf weights to 1.0 
	tree.SetConstantLeafWeights();

	const int dim = 128;
	int num_db_images = (int)key_files.size();
	unsigned long count = 0;

	tree.ClearDatabase();

	for (int i = 0; i < num_db_images; i++)
	{
		int num_keys = 0;
		unsigned char *keys = ReadAndFilterKeys(key_files[i].c_str(), dim, min_feature_scale, 0, num_keys);

		printf("[VocabBuildDB] Adding vector %d (%d keys)\n", start_id + i, num_keys);
		tree.AddImageToDatabase(start_id + i, num_keys, keys);

		if (num_keys > 0)
			delete[] keys;
	}

	printf("[VocabBuildDB] Pushed %lu features\n", count);
	fflush(stdout);

	if (use_tfidf == 1)
		tree.ComputeTFIDFWeights(num_db_images);

	if (normalize == 1)
		tree.NormalizeDatabase(start_id, num_db_images);

	printf("[VocabBuildDB] Writing database ...\n");
	tree.Write(db_out);

	// char filename[256];
	// sprintf(filename, "vectors_%03d.txt", start_id);
	// tree.WriteDatabaseVectors(filename, start_id, num_db_images);

	return 0;
}