Ejemplo n.º 1
0
int main(int argc, char **argv)
{
    // Setting default parameters
    int n_hist = 4;
    int n_ori = 8;
    int n_bins = 36;
    int meth_flag = 1;
    float thresh = 0.6;
    int verb_flag = 0;
    char label[256];
    strcpy(label, "extra");

    // Parsing command line
    int res = parse_options(argc, argv, &n_bins, &n_hist, &n_ori,
                            &meth_flag, &thresh, &verb_flag, label);
    if (res == EXIT_FAILURE)
        return EXIT_FAILURE;

    // Memory allocation
    struct sift_keypoints* k1 = sift_malloc_keypoints();
    struct sift_keypoints* k2 = sift_malloc_keypoints();
    struct sift_keypoints* out_k1 = sift_malloc_keypoints();
    struct sift_keypoints* out_k2A = sift_malloc_keypoints();
    struct sift_keypoints* out_k2B = sift_malloc_keypoints();

    // Read input keypoint ASCII files
    int readflag = verb_flag + 1;
    sift_read_keypoints(k1, argv[1], n_hist, n_ori, n_bins, readflag);
    sift_read_keypoints(k2, argv[2], n_hist, n_ori, n_bins, readflag);


    debug("size read keys / k1 %i / k2 %i /", k1->size, k2->size);

    // Matching
    matching(k1, k2, out_k1, out_k2A, out_k2B, thresh, meth_flag);

    // Print
    print_pairs(out_k1, out_k2A);
    char name[FILENAME_MAX];
    if(verb_flag == 1){
        save_pairs_extra("OUTmatches.txt", out_k1, out_k2A, out_k2B);
        sprintf(name, "%s_im0.txt", label);
        sift_save_keypoints(out_k1, name, 1);
        sprintf(name, "%s_im1.txt", label);
        sift_save_keypoints(out_k2A, name, 1);
    }

    // Free memory
    sift_free_keypoints(k1);
    sift_free_keypoints(k2);
    sift_free_keypoints(out_k1);
    sift_free_keypoints(out_k2A);
    sift_free_keypoints(out_k2B);

    return EXIT_SUCCESS;
}
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
0
struct sift_keypoints* sift_anatomy(const float* x, int w, int h, const struct sift_parameters* p,
                                    struct sift_scalespace* ss[4],
                                    struct sift_keypoints* kk[6])
{
    // WARNING
    //  ss[2]: pointers to two scalespace structures for lates use (The Gaussian
    //         scale-space and the DoG scale-space).
    //  kk[6]: pointers to six keypoint lists structure for later use.
    
    struct sift_keypoints* k = sift_malloc_keypoints();
   
    // The number of octaves.
    int n_oct = number_of_octaves(w, h, p);
    
    // adapt the threshold to the scalespace discretization
    float thresh = convert_threshold(p);
    
    /** MEMORY ALLOCATION **/
    /** scale-space structure */
    struct sift_scalespace* s   = sift_malloc_scalespace_lowe(n_oct,p->n_spo,w,h,p->delta_min,p->sigma_min);
    struct sift_scalespace* d   = sift_malloc_scalespace_dog_from_scalespace(s);
    struct sift_scalespace* sx  = sift_malloc_scalespace_from_model(s);
    struct sift_scalespace* sy  = sift_malloc_scalespace_from_model(s);
    /** list-of-keypoints (Already allocated) */
    struct sift_keypoints* kA   = kk[0];  /* 3D (discrete) extrema   */
    struct sift_keypoints* kB   = kk[1];  /* passing the threshold on DoG  */
    struct sift_keypoints* kC   = kk[2];  /* interpolated 3D extrema (continuous) */
    struct sift_keypoints* kD   = kk[3];  /* passing the threshold on DoG  */
    struct sift_keypoints* kE   = kk[4];  /* passing OnEdge filter */
    struct sift_keypoints* kF   = kk[5];  /* keypoints whose distance to image borders are sufficient */

    /** KEYPOINT DETECTION ***************************************************/
    scalespace_compute(s, x, w, h, p->sigma_in);  /* Builds Lowe's scale-space */
    scalespace_compute_dog(s,d);

    keypoints_find_3d_discrete_extrema(d, kA, p->n_ori, p->n_hist, p->n_bins);
    keypoints_discard_with_low_response(kA, kB, 0.8*thresh);
    keypoints_interpolate_position(d, kB, kC, p->itermax);
    keypoints_discard_with_low_response(kC, kD, thresh); 
    keypoints_compute_edge_response(d,kD); 
    keypoints_discard_on_edge(kD, kE, (p->C_edge+1)*(p->C_edge+1)/p->C_edge);
    keypoints_discard_near_the_border(kE, kF, w, h, 1.0);


    /** KEYPOINT DESCRIPTION *************************************************/
    scalespace_compute_gradient(s,sx,sy); /* Pre-computes gradient scale-space */
    keypoints_attribute_orientations(sx, sy, kF, k, p->n_bins,p->lambda_ori,p->t);
    keypoints_attribute_descriptors(sx, sy, k, p->n_hist,p->n_ori,p->lambda_descr);

    /** scalespace structures*/
    ss[0] = s;
    ss[1] = d;
    ss[2] = sx;
    ss[3] = sy;

    return k;
}
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
0
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;
}
Ejemplo n.º 7
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;
}