Esempio n. 1
0
struct sift_keypoint_std *sift_read_keyslocation_from_file(char *filename, int *n)
{
    struct sift_parameters* p = sift_assign_default_parameters();
    int n_ori = p->n_ori;   // 8
    int n_hist = p->n_hist; // 4
    int l = n_hist*n_hist*n_ori;
    int n_bins = p->n_bins;

    // read keypoints locations from a file and
    // save them into a sift_keypoints structure
    struct sift_keypoints* keys = sift_malloc_keypoints();
    int flag = 0; // read coordinates 
    sift_read_keypoints(keys, filename, n_hist, n_ori, n_bins, flag);

    // translate the sift_keypoints structure into a flat list
    *n = keys->size;
    struct sift_keypoint_std* k = xmalloc((*n)*sizeof(struct sift_keypoint_std));
    for(int i = 0; i < keys->size; i++){
        k[i].x = keys->list[i]->x;
        k[i].y = keys->list[i]->y;
        k[i].scale = keys->list[i]->sigma;
        k[i].orientation = keys->list[i]->theta;  // 0
        for(int j=0; j<l; j++){
            k[i].descriptor[j] = keys->list[i]->descr[j]; // 0
        }
    }
    sift_free_keypoints(keys);

    return k;
}
Esempio n. 2
0
/** @brief Extracts oriented keypoints (without description
 *
 *
 */
struct sift_keypoint_std* sift_compute_points(const float* x, int w, int h, int *n)
{

    /** assign default parameters **/
    struct sift_parameters* p = sift_assign_default_parameters();

    /** Memory dynamic allocation */
    // WARNING 5 lists of keypoints containing intermediary states of the algorithm
    struct sift_keypoints **kk = xmalloc(5*sizeof(struct sift_keypoints*));
    for(int i = 0; i < 5; i++){
        kk[i] = sift_malloc_keypoints();
    }
    // WARNING 4 scalespace structure containing the DoG and Gaussian scalespace and the gradient
    struct sift_scalespace **ss = xmalloc(2*sizeof(struct sift_scalespace*));

    /** Algorithm */
    struct sift_keypoints* keys = sift_anatomy_without_description(x, w, h, p, ss, kk);

    /* Copy to a list of keypoints */
    *n = keys->size;
    struct sift_keypoint_std* k = xmalloc((*n)*sizeof(*k));
    for(int i = 0; i < keys->size; i++){
        k[i].x = keys->list[i]->x;
        k[i].y = keys->list[i]->y;
        k[i].scale = keys->list[i]->sigma;
        k[i].orientation = keys->list[i]->theta;
        for(int j = 0; j < 128; j++){
            k[i].descriptor[j] = 0;
        }
    }

    /* memory deallocation */
    xfree(p);
    sift_free_keypoints(keys);
    for(int i = 0; i < 5; i++){
        sift_free_keypoints(kk[i]);
    }
    xfree(kk);
    for(int i = 0; i < 2; i++){
        sift_free_scalespace(ss[i]);
    }
    xfree(ss);

    return k;
}
Esempio n. 3
0
static struct sift_keypoints* sift_translate_standard_into_anatomy(
        const struct sift_keypoint_std* k,
        int n)
{
    // load the default parameters are required
    struct sift_parameters* p = sift_assign_default_parameters();
    float sigma_min = p->sigma_min;  // 0.8
    float delta_min = p->delta_min;  // 0.5
    int n_spo = p->n_spo;   // 3
    int n_ori = p->n_ori;   // 8
    int n_hist = p->n_hist; // 4
    int n_bins = p->n_bins; // 36

    struct sift_keypoints* keys = sift_malloc_keypoints();
    for (int i = 0; i < n; i++) {
        struct keypoint* key = sift_malloc_keypoint(n_ori, n_hist, n_bins);
        /* reading the extremum continuous coordinates */
        key->x = k[i].x;
        key->y = k[i].y;
        key->sigma = k[i].scale;
        key->theta = k[i].orientation;
        /*  inferring the discrete coordinates in the scale-space grid */
        // We look for the pair of integer (o,s) such that nspo*o+s is the nearest
        // to alpha = nspo * log( k[i].scale / sigma_min) /M_LN2
        // with the constraint s=1,2,..,nspo.
        int o, s;
        int a = (int) (round(n_spo * log( k[i].scale / sigma_min) / M_LN2));
        o = (a - 1) / n_spo;
        if (o > -1) {
            s = (a - 1) % n_spo + 1;
        }
        else {
            o = 0;
            s = 0;
        }
        key->o = o;
        key->s = s;
        key->i = (int)( key->x / ( delta_min * exp( key->o * M_LN2)) + 0.5 );
        key->j = (int)( key->y / ( delta_min * exp( key->o * M_LN2)) + 0.5 );
        sift_add_keypoint_to_list(key,keys);
    }
    return keys;
}
Esempio n. 4
0
void sift_find_ori_and_fill_descriptors(const float *x, int w, int h, struct sift_keypoint_std *k, int n)
{
    struct sift_keypoints* keys = sift_translate_standard_into_anatomy(k, n);

    /** assign default parameters **/
    struct sift_parameters* p = sift_assign_default_parameters();

    /** algorithm */
    sift_anatomy_orientation_and_description(x, w, h, p, keys);

    /* Copy back to the input flat list of keypoints */
    for(int i = 0; i < keys->size; i++){
        k[i].x = keys->list[i]->x;
        k[i].y = keys->list[i]->y;
        k[i].scale = keys->list[i]->sigma;
        k[i].orientation = keys->list[i]->theta;
        for(int j=0; j<128; j++){
            k[i].descriptor[j] = (unsigned char)(keys->list[i]->descr[j]);
        }
    }
}
int main(int argc, char **argv)
{

    if((argc != 3)&&(argc != 5)){
        print_usage();
        return EXIT_FAILURE;
    }

    // Loading image
    int w, h;
    _myfloat* im = read_image_to_gray(argv[2], &w, &h);

    // load standard method parameter
    struct sift_parameters* p = sift_assign_default_parameters();
    bool gauss_flag = true;
    if (argc  == 5){
        p->lambda_descr = atof(argv[argc-2]);
        gauss_flag = atoi(argv[argc-1]);
    }

    // load keypoints (ellipse)
    struct sift_keypoints *l = sift_malloc_keypoints();
    sift_read_ellipses(l, argv[1], p->n_hist, p->n_ori, p->n_bins, 0);
    fprintf(stderr," after read");
    estimate_scalespace_index(l, p);
    fprintf(stderr," after estimate scalespace index");

    // scalespace, gradient, orientation and description
    ellipse_sift_anatomy_orientation_and_description(im, w, h, p, l, gauss_flag);
    fprintf(stderr," after ellipse index");

    // printing ellipses
    fprintf_ellipses(stdout, l, 1);

    // memory deallocation
    sift_free_keypoints(l);
    free(im);

    return EXIT_SUCCESS;
}
Esempio n. 6
0
int main(int c, char *v[])
{
    // process input arguments
    if (c < 2) {
        print_help(v);
    	return 1;
    }

    // optional arguments
    const char *output_file = pick_option(&c, &v, "o", "stdout");
    bool binary = (bool) pick_option(&c, &v, "b", NULL);
    bool verbose = (bool) pick_option(&c, &v, "-verbose", NULL);
    int max_nb_pts = atoi(pick_option(&c, &v, "-max-nb-pts", "INT_MAX"));
    float thresh_dog = atof(pick_option(&c, &v, "-thresh-dog", "0.0133"));
    int ss_noct = atoi(pick_option(&c, &v, "-scale-space-noct", "8"));
    int ss_nspo = atoi(pick_option(&c, &v, "-scale-space-nspo", "3"));

    // initialise time
    struct timespec ts; portable_gettime(&ts);


    // define the rectangular region of interest (roi)
    int x, y, w, h;
    if (c == 2) {
        x = 0;
        y = 0;
    } else if (c == 6) {
        x = atoi(v[2]);
        y = atoi(v[3]);
        w = atoi(v[4]);
        h = atoi(v[5]);
    } else {
        print_help(v);
        return 1;
    }

    // read the roi in the input image
    GDALDatasetH  hDataset;
    GDALAllRegister();
    hDataset = GDALOpen( v[1], GA_ReadOnly );
    if( hDataset == NULL )
    {
        fprintf(stderr, "ERROR : can't open %s\n", v[1]);
    }
       
    GDALRasterBandH hBand;
    hBand = GDALGetRasterBand( hDataset, 1 );
    float *roi;
    roi = (float *) CPLMalloc(sizeof(float)*w*h);
    GDALRasterIO( hBand, GF_Read, x, y, w, h, 
              roi, w, h, GDT_Float32, 
              0, 0 );
    GDALClose(hBand);
    GDALClose(hDataset);
    
    
    if (verbose) print_elapsed_time(&ts, "read ROI", 35);

    // prepare sift parameters
    struct sift_parameters* p = sift_assign_default_parameters();
    p->C_DoG = thresh_dog;
    p->n_oct = ss_noct;
    p->n_spo = ss_nspo;

    // compute sift keypoints
    struct sift_scalespace **ss = (struct sift_scalespace**) malloc(4 * sizeof(struct sift_scalespace*));
    struct sift_keypoints **kk = (struct sift_keypoints**) malloc(6 * sizeof(struct sift_keypoints*));
    for (int i = 0; i < 6; i++)
        kk[i] = sift_malloc_keypoints();
    struct sift_keypoints* kpts = sift_anatomy(roi, w, h, p, ss, kk);
    if (verbose) print_elapsed_time(&ts, "run SIFT", 35);

    // add (x, y) offset to keypoints coordinates
    for (int i = 0; i < kpts->size; i++) {
        kpts->list[i]->x += y;  // in Ives' conventions x is the row index
        kpts->list[i]->y += x;
    }
    if (verbose) print_elapsed_time(&ts, "add offset", 35);

    // write to standard output
    FILE *f = fopen(output_file, "w");
    fprintf_keypoints(f, kpts, max_nb_pts, binary, 1);
    fclose(f);
    if (verbose) print_elapsed_time(&ts, "write output", 35);

    // cleanup
    CPLFree(roi);
    sift_free_keypoints(kpts);
    for (int i = 0; i < 6; i++)
        sift_free_keypoints(kk[i]);
    free(kk);
    for (int i = 0; i < 4; i++)
        sift_free_scalespace(ss[i]);
    free(ss);
    free(p);
    return 0;
}