コード例 #1
0
ファイル: query_mvptree_dct.cpp プロジェクト: aurora/phash
int main(int argc, char **argv){
    if (argc < 3){
	printf("not enough input args\n");
        printf("usage: %s directory dbname [radius] [knearest] [threshold]\n", argv[0]);
	return -1;
    }

    const char *dir_name = argv[1];/* name of files in directory of query images */
    const char *filename = argv[2];/* name of file to save db */

    MVPFile mvpfile;
    mvpfile.filename = strdup(filename);
    mvpfile.hashdist = distancefunc;
    mvpfile.hash_type = UINT64ARRAY;

    int nbfiles = 0;
    printf("using db %s\n", filename);
    printf("using dir %s for query files\n", dir_name);
    char **files = ph_readfilenames(dir_name,nbfiles);
    if (!files){
	printf("unable to read files from directory\n");
	return -2;
    }

    printf("nb query files = %d\n", nbfiles);

    DP *query = ph_malloc_datapoint(mvpfile.hash_type);
    if (query == NULL){
        printf("mem alloc error\n");
        return -3;
    }

    float radius = 30.0f;
    float threshold = 15.0f;
    int knearest = 20;
    if (argc >= 4){
	radius = atof(argv[3]);
    }
    if (argc >= 5){
	knearest = atoi(argv[4]);
    }
    if (argc >= 6){
        threshold = atof(argv[5]);
    }
    printf("radius = %f\n", radius);
    printf("knearest = %d\n", knearest);
    printf("threshold = %f\n", threshold);

    DP **results = (DP**)malloc(knearest*sizeof(DP*));
    if (results == NULL){
        return -3;
    }
    ulong64 tmphash = 0x0000000000000000;
    int nbfound = 0, count = 0, sum_calcs = 0;
    for (int i=0;i<nbfiles;i++){

        if (ph_dct_imagehash(files[i],tmphash) < 0){
	    printf("unable to get hash\n");
            continue;
	}
	printf("query[%d]: %s %llx\n", i, files[i], tmphash);
        query->id = files[i];
        query->hash = &tmphash;
        query->hash_length = 1;

	nb_calcs = 0;
	nbfound = 0;
	MVPRetCode retcode = ph_query_mvptree(&mvpfile,query,knearest,radius,threshold,results,nbfound);
	if (retcode != PH_SUCCESS && retcode != PH_ERRCAP){
	    printf("could not complete query, %d\n",retcode);
	    continue;
	}
        count++;
	sum_calcs += nb_calcs;
	
	printf(" %d files found\n", nbfound);
	for (int j=0;j<nbfound;j++){
	    float d = distancefunc(query, results[j]);
	    printf(" %d  %s distance = %f\n", j, results[j]->id, d);
	}
	printf("nb distance calcs: %d\n", nb_calcs);
	for (int j=0;j<nbfound;j++){
            free(results[j]->id);
            results[j]->id = NULL;
            free(results[j]->hash);
            results[j]->id = NULL;
	    ph_free_datapoint(results[j]);
	}

    }
 
   float ave_calcs = (float)sum_calcs/(float)count;      
   printf("ave calcs/query: %f\n", ave_calcs);
    

   for (int i=0;i<nbfiles;i++){
       free(files[i]);
   }
   free(files);

   ph_free_datapoint(query);
   free(results);
   free(mvpfile.filename);

    return 0;
}
コード例 #2
0
ファイル: add_mvptree_dct.cpp プロジェクト: HeeJaeYo/pHash
int main(int argc, char **argv){
    if (argc < 3){
	printf("not enough input args\n");
        printf("usage: %s directory filename\n", argv[0]);
	return -1;
    }

    const char *dir_name = argv[1];/* name of dir to retrieve image files */
    const char *filename = argv[2];/* name of file to save db */

    MVPFile mvpfile;
    mvpfile.filename = strdup(filename);
    mvpfile.hashdist = distancefunc;
    mvpfile.hash_type = UINT64ARRAY;

    int nbfiles = 0;
    printf("dir name: %s\n", dir_name);
    char **files = ph_readfilenames(dir_name,nbfiles);
    if (!files){
	printf("mem alloc error\n");
	return -2;
    }
    printf("nbfiles = %d\n", nbfiles);
    DP **hashlist = (DP**)malloc(nbfiles*sizeof(DP*));
    if (!hashlist){
	printf("mem alloc error\n");
	return -3;
    }

    int count = 0;
    ulong64 tmphash = 0;
    for (int i=0;i<nbfiles;i++){
        hashlist[count] = ph_malloc_datapoint(mvpfile.hash_type);
	if (hashlist[count] == NULL){
	    printf("mem alloc error\n");
	    return -4;
	}
	hashlist[count]->hash = malloc(sizeof(ulong64));
	if (hashlist[count]->hash == NULL){
	    printf("mem alloc error\n");
	    return -5;
	}
        printf("file[%d] = %s\n", i, files[i]);        
        if (ph_dct_imagehash(files[i],tmphash) < 0){
            printf("unable to get hash\n");
            free(hashlist[count]->hash);
            ph_free_datapoint(hashlist[count]);
            continue;
	}
        hashlist[count]->id = files[i];
        *((ulong64*)hashlist[count]->hash) = tmphash;
	hashlist[count]->hash_length = 1;
        count++;
    }

    printf("add files to file %s\n", filename);
    int nbsaved;
    MVPRetCode ret = ph_add_mvptree(&mvpfile, hashlist, count,nbsaved);
    printf("number saved %d out of %d, ret code %d\n", nbsaved,count,ret);


    for (int i=0;i<nbfiles;i++){
	free(files[i]);
    }
    free(files);

    for (int i=0;i<nbfiles;i++){
	free(hashlist[i]->hash);
        ph_free_datapoint(hashlist[i]);
    }
    free(hashlist);

    return 0;
}