/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk

« back to all changes in this revision

Viewing changes to dict_hw/src/dict_hw_cli.cpp

  • Committer: Suren A. Chilingaryan
  • Date: 2010-04-26 03:58:12 UTC
  • Revision ID: csa@dside.dyndns.org-20100426035812-st93op9yj551wi3b
Multipass mode and re-initialization fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include "helpers.h"
23
23
 
 
24
#define DATA_CLEANUP \
 
25
    if (fy) fclose(fy); \
 
26
    if (fx) fclose(fx); \
 
27
    free(res); \
 
28
    dictDestroyContext(ps); \
 
29
    free(images);
 
30
 
 
31
 
24
32
int matlab_mode = 0;
25
33
 
26
34
typedef enum {
28
36
    OPT_SINGLE_MODE = 251,
29
37
    OPT_NO_PRELOAD = 252,
30
38
    OPT_MULTIPASS = 253,
 
39
    OPT_CORRSIZE = 's',
 
40
    OPT_PRECISION = 'p',
31
41
    OPT_MATLAB = 'm',
32
42
    OPT_HELP = 'h'
33
43
} Options;
34
44
 
35
45
static struct option long_options[] = {
36
 
    {"matlab",                  no_argument, 0, OPT_MATLAB },
37
 
    {"default-fft",             no_argument, 0, OPT_DEFAULT_FFT },
38
 
    {"no-threads",              no_argument, 0, OPT_SINGLE_MODE },
39
 
    {"no-preload",              no_argument, 0, OPT_NO_PRELOAD },
40
 
    {"multipass",               no_argument, 0, OPT_MULTIPASS },
41
 
    {"help",                    no_argument, 0, OPT_HELP },
 
46
    {"matlab",                  no_argument,            0, OPT_MATLAB },
 
47
    {"corrsize",                required_argument,      0, OPT_CORRSIZE },      
 
48
    {"precision",               required_argument,      0, OPT_PRECISION },
 
49
    {"default-fft",             no_argument,            0, OPT_DEFAULT_FFT },
 
50
    {"no-threads",              no_argument,            0, OPT_SINGLE_MODE },
 
51
    {"no-preload",              no_argument,            0, OPT_NO_PRELOAD },
 
52
    {"multipass",               no_argument,            0, OPT_MULTIPASS },
 
53
    {"help",                    no_argument,            0, OPT_HELP },
42
54
    { 0, 0, 0, 0 }
43
55
};
44
56
 
47
59
" %s [options] <directory> [maximages]\n"
48
60
"  Options:\n"
49
61
"       --matlab        - Matlab mode (transposed data ordering)\n"
 
62
"       --corrsize <s>  - Size of correlation area in pixels (15 by default)\n"
 
63
"       --precision <p> - Subpixel precision (1000 by default)\n"
50
64
"       --default-fft   - Do not optimize FFT size\n"
51
65
"       --no-threads    - Disables threading, results in a single GPU mode\n"
52
66
"       --no-preload    - Disables image preloading in multi-threaded mode\n"
113
127
    return i;
114
128
}
115
129
 
116
 
int writefile(char *name, float *x, int img, int ncp) {
 
130
int writefile(int matlab_mode, char *name, float *x, int img, int ncp) {
117
131
    FILE *f = fopen(name, "w");
118
132
    if (!f) {
119
133
        printf("Can't write file %s\n", name);
122
136
    }
123
137
    g_free(name);
124
138
 
125
 
    for (int j = 0; j < ncp ; j++) {    
126
 
        for (int i = 0; i < img ; i++) {
127
 
            fprintf(f, "% 9.7le\t", x[i*ncp + j]);
128
 
        }
129
 
        fprintf(f,"\n");
 
139
    if (matlab_mode) {
 
140
        for (int j = 0; j < ncp ; j++) {    
 
141
            for (int i = 0; i < img ; i++) {
 
142
                fprintf(f, "% 9.7le\t", x[i*ncp + j]);
 
143
            }
 
144
            fprintf(f,"\n");
 
145
        }
 
146
    } else {
 
147
        for (int j = 0; j < img ; j++) {    
 
148
            for (int i = 0; i < ncp ; i++) {
 
149
                fprintf(f, "% 9.7le\t", x[j*ncp + i]);
 
150
            }
 
151
            fprintf(f,"\n");
 
152
        }
130
153
    }
131
154
    
132
155
    fclose(f);
158
181
    gettimeofday(&tv1, NULL);
159
182
#endif /* DICT_HW_MEASURE_TIMINGS */
160
183
 
 
184
    int corrsize = 15;
 
185
    int precision = 1000;
 
186
 
161
187
    int default_fft = 0;
162
188
    int single_mode = 0;
163
189
    int preload = 1;
164
190
 
165
 
    int multipass;
 
191
    int multipass = 0;
166
192
    
167
193
    int option_index = 0;
168
194
    unsigned char c;
173
199
            case OPT_MATLAB:
174
200
                matlab_mode = 1;
175
201
            break;
 
202
            case OPT_CORRSIZE:
 
203
                corrsize = atoi(optarg);
 
204
                if (corrsize < 1) {
 
205
                    printf("Invalid size of correlation are is specified, value \"%s\" is invalid", optarg);
 
206
                    exit(-1);
 
207
                }
 
208
            break;
 
209
            case OPT_PRECISION:
 
210
                precision = atoi(optarg);
 
211
                if (corrsize < 1) {
 
212
                    printf("Invalid subpixel precision is specified, value \"%s\" is invalid", optarg);
 
213
                    exit(-1);
 
214
                }
 
215
            break;
176
216
            case OPT_DEFAULT_FFT:
177
217
                default_fft = 1;
178
218
            break;
235
275
 
236
276
    GRegex *regex = g_regex_new("(tiff?|bmp|jpe?g|png)", G_REGEX_CASELESS, (GRegexMatchFlags)0, NULL);
237
277
    if (!regex) {
238
 
        printf("Internal error. Failed to compiler regular expression\n");
 
278
        printf("Internal error. Failed to compile regular expression\n");
239
279
        exit(-1);
240
280
    }
241
281
    
280
320
    if (default_fft) flags |= DICT_FLAGS_FIXED_FFT_SIZE;
281
321
    if (single_mode) flags |= DICT_FLAGS_SINGLE_THREAD;
282
322
 
283
 
    err = dictSetup(ps, ncp, 15, 1000, flags);
284
 
    
285
 
    
 
323
 
 
324
    size_t ncp_block;
 
325
    
 
326
    if (multipass) {
 
327
        ncp_block = dictGetMaxCacheablePoints(corrsize, precision, flags);
 
328
        if (ncp_block >= ncp) multipass = 0;
 
329
    }
 
330
 
 
331
    err = dictSetup(ps, multipass?ncp_block:ncp, corrsize, precision, flags);
286
332
    if (err) {
 
333
        dictDestroyContext(ps);
 
334
        free(images);
287
335
        printf("Setup is failed, error code %i\n", err);
288
336
        exit(-1);
289
337
    }
290
338
 
291
339
    float *res;
292
340
    int step;
293
 
    
294
 
    if (matlab_mode) {
 
341
 
 
342
    FILE *fx = NULL, *fy = NULL;
 
343
 
 
344
    if ((matlab_mode)||(multipass)) {
295
345
        res = (float*) malloc(2 * ncp * (maximg - 1) * sizeof(float));
296
346
        step = ncp * (maximg - 1);
 
347
        
 
348
        if (!res) {
 
349
            dictDestroyContext(ps);
 
350
            free(images);
 
351
            printf("Can't allocate result memory\n");
 
352
            exit(-1);
 
353
        }
297
354
    } else {
298
355
        res = (float*) malloc(2 * ncp * sizeof(float));
299
356
        step = ncp;
300
357
        dictSetPointsBuffer(ps, res, res + ncp);
301
 
    }
302
 
 
303
 
    dictSetTemplatePoints(ps, x, y);
304
 
 
305
 
    name = g_strdup_printf("%s/images/%s", argv[optind], images[0]);
306
 
    if (!g_file_test(name, G_FILE_TEST_IS_REGULAR)) {
307
 
        g_free(name);
308
 
        printf("%s is not a regular file\n", images[0]);
309
 
        exit(-1);
310
 
    }
311
 
    err = dictLoadTemplateImageFile(ps, name);
312
 
    g_free(name);
313
 
    if (err) {
314
 
        printf("failed to load template file %s\n", images[0]);
315
 
        exit(-1);
316
 
    }
317
 
    
318
 
 
319
 
    dictSetCurrentPoints(ps, x, y);
320
 
 
321
 
#ifdef DICT_HW_MEASURE_TIMINGS
322
 
    gettimeofday(&tv2, NULL);
323
 
    time[0] = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
324
 
#endif /* DICT_HW_MEASURE_TIMINGS */
325
 
 
326
 
 
327
 
#ifdef DICT_HW_MEASURE_TIMINGS
328
 
    gettimeofday(&tv3, NULL);
329
 
#endif /* DICT_HW_MEASURE_TIMINGS */
330
 
 
331
 
    FILE *fx, *fy;
332
 
    if (!matlab_mode) {
 
358
 
333
359
        name = g_strdup_printf("%s/data/validx.dat", argv[optind]);
334
360
        fx = fopen(name, "w");
335
361
        g_free(name);
337
363
        fy = fopen(name, "w");
338
364
        g_free(name);
339
365
        
340
 
        if ((!fx)||(!fy)) {
 
366
        if ((!res)||(!fx)||(!fy)) {
 
367
            if (fy) fclose(fy);
 
368
            if (fx) fclose(fx);
 
369
            if (res) free(res);
 
370
            dictDestroyContext(ps);
 
371
            free(images);
341
372
            printf("Can't create output files...\n");
342
373
            exit(-1);
343
374
        }
344
375
    }
345
376
 
346
377
#ifdef DICT_HW_MEASURE_TIMINGS
347
 
    gettimeofday(&tv2, NULL);
348
 
    time[1] += (tv2.tv_sec - tv3.tv_sec) * 1000000 + (tv2.tv_usec - tv3.tv_usec);
349
 
#endif /* DICT_HW_MEASURE_TIMINGS */
350
 
 
351
 
 
352
 
    gchar *next = NULL;
353
 
 
354
 
    name = g_strdup_printf("%s/images/%s", argv[optind], images[1]);
355
 
    if (!g_file_test(name, G_FILE_TEST_IS_REGULAR)) {
356
 
        g_free(name);
357
 
        printf("%s is not a regular file\n", images[1]);
358
 
        exit(-1);
359
 
    }
360
 
    
361
 
    for (i = 1; i < maximg; i++, name = next) {
362
 
#ifndef DICT_HW_MEASURE_TIMINGS
363
 
        printf("%s\n", images[i]);
364
 
#endif /* !DICT_HW_MEASURE_TIMINGS */
365
 
 
366
 
 
367
 
        if ((i + 1) < maximg) {
368
 
            next = g_strdup_printf("%s/images/%s", argv[optind], images[i + 1]);
369
 
            if (!g_file_test(next, G_FILE_TEST_IS_REGULAR)) {
370
 
                g_free(next);
371
 
                g_free(name);
372
 
                printf("%s is not a regular file\n", images[i + 1]);
373
 
                exit(-1);
374
 
            }
375
 
            
376
 
            if (preload) {
377
 
                dictPreloadImageFile(ps, next);
378
 
            }
379
 
        }
380
 
        
381
 
#ifdef DICT_HW_MEASURE_TIMINGS
382
 
        gettimeofday(&tv3, NULL);
383
 
#endif /* DICT_HW_MEASURE_TIMINGS */
384
 
        err = dictLoadImageFile(ps, name);
385
 
#ifdef DICT_HW_MEASURE_TIMINGS
386
378
        gettimeofday(&tv2, NULL);
387
 
        time[5] += (tv2.tv_sec - tv3.tv_sec) * 1000000 + (tv2.tv_usec - tv3.tv_usec);
 
379
        time[0] = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
388
380
#endif /* DICT_HW_MEASURE_TIMINGS */
389
 
        
 
381
 
 
382
    int icp;
 
383
    int real_ncp = ncp;
 
384
    
 
385
    for (icp = 0, ncp = multipass?ncp_block:ncp; icp < real_ncp; icp += ncp, ncp = min2(ncp_block, real_ncp - icp)) {
 
386
#ifdef DICT_HW_VERBOSE_OUTPUT
 
387
        printf("%i + %i (of %i)\n", icp, ncp, real_ncp);
 
388
#endif /* DICT_HW_VERBOSE_OUTPUT */
 
389
        if (icp) {
 
390
            dictSetActualNumberOfPoints(ps, ncp);
 
391
            dictClean(ps);
 
392
            //dictSetup(ps, ncp, corrsize, precision, flags);
 
393
        }
 
394
 
 
395
 
 
396
        dictSetTemplatePoints(ps, x + icp, y + icp);
 
397
 
 
398
        name = g_strdup_printf("%s/images/%s", argv[optind], images[0]);
 
399
        if (!g_file_test(name, G_FILE_TEST_IS_REGULAR)) {
 
400
            g_free(name);
 
401
            printf("%s is not a regular file\n", images[0]);
 
402
            DATA_CLEANUP
 
403
            exit(-1);
 
404
        }
 
405
    
 
406
        err = dictLoadTemplateImageFile(ps, name);
390
407
        g_free(name);
391
 
 
392
408
        if (err) {
393
 
            printf("failed to load image %s\n", images[0]);
394
 
            exit(-1);
395
 
        }
 
409
            printf("failed to load template file %s\n", images[0]);
 
410
            DATA_CLEANUP
 
411
            exit(-1);
 
412
        }
 
413
    
 
414
        dictSetCurrentPoints(ps, x + icp, y + icp);
 
415
 
 
416
 
 
417
        gchar *next = NULL;
 
418
 
 
419
        name = g_strdup_printf("%s/images/%s", argv[optind], images[1]);
 
420
        if (!g_file_test(name, G_FILE_TEST_IS_REGULAR)) {
 
421
            g_free(name);
 
422
            printf("%s is not a regular file\n", images[1]);
 
423
            DATA_CLEANUP
 
424
            exit(-1);
 
425
        }
 
426
    
 
427
        for (i = 1; i < maximg; i++, name = next) {
 
428
#ifdef DICT_HW_VERBOSE_OUTPUT
 
429
# ifndef DICT_HW_MEASURE_TIMINGS
 
430
            printf("%s\n", images[i]);
 
431
# endif /* !DICT_HW_MEASURE_TIMINGS */
 
432
#endif /* DICT_HW_VERBOSE_OUTPUT */
 
433
 
 
434
            if ((i + 1) < maximg) {
 
435
                next = g_strdup_printf("%s/images/%s", argv[optind], images[i + 1]);
 
436
                if (!g_file_test(next, G_FILE_TEST_IS_REGULAR)) {
 
437
                    g_free(next);
 
438
                    g_free(name);
 
439
                    printf("%s is not a regular file\n", images[i + 1]);
 
440
                    DATA_CLEANUP
 
441
                    exit(-1);
 
442
                }
 
443
            
 
444
                if (preload) {
 
445
                    dictPreloadImageFile(ps, next);
 
446
                }
 
447
            }
396
448
        
397
 
 
398
 
        if (matlab_mode) {
399
 
            float *out = res + ncp * (i - 1);
400
 
            dictGetCurrentPoints(ps, out, out + step);
401
 
        } else {
402
 
            dictCompute(ps);
403
 
 
404
449
#ifdef DICT_HW_MEASURE_TIMINGS
405
450
            gettimeofday(&tv3, NULL);
406
451
#endif /* DICT_HW_MEASURE_TIMINGS */
407
 
 
408
 
            for (int j = 0; j < ncp; j++) {
409
 
                fprintf(fx, "% 9.7le\t", res[j]);
410
 
            }
411
 
            fprintf(fx,"\n");
412
 
            
413
 
            float *out = res + step;
414
 
            for (int j = 0; j < ncp; j++) {
415
 
                fprintf(fy, "% 9.7le\t", out[j]);
416
 
            }
417
 
            fprintf(fy,"\n");
418
 
 
 
452
            err = dictLoadImageFile(ps, name);
419
453
#ifdef DICT_HW_MEASURE_TIMINGS
420
454
            gettimeofday(&tv2, NULL);
421
 
            time[1] += (tv2.tv_sec - tv3.tv_sec) * 1000000 + (tv2.tv_usec - tv3.tv_usec);
422
 
#endif /* DICT_HW_MEASURE_TIMINGS */
 
455
            time[5] += (tv2.tv_sec - tv3.tv_sec) * 1000000 + (tv2.tv_usec - tv3.tv_usec);
 
456
#endif /* DICT_HW_MEASURE_TIMINGS */
 
457
        
 
458
            g_free(name);
 
459
 
 
460
            if (err) {
 
461
                printf("failed to load image %s\n", images[0]);
 
462
                DATA_CLEANUP
 
463
                exit(-1);
 
464
            }
 
465
        
 
466
 
 
467
            if ((matlab_mode)||(multipass)) {
 
468
                float *out = res + real_ncp * (i - 1) + icp;
 
469
                dictGetCurrentPoints(ps, out, out + step);
 
470
            } else {
 
471
                dictCompute(ps);
 
472
 
 
473
#ifdef DICT_HW_MEASURE_TIMINGS
 
474
                gettimeofday(&tv3, NULL);
 
475
#endif /* DICT_HW_MEASURE_TIMINGS */
 
476
 
 
477
                for (int j = 0; j < ncp; j++) {
 
478
                    fprintf(fx, "% 9.7le\t", res[j]);
 
479
                }
 
480
                fprintf(fx,"\n");
 
481
            
 
482
                float *out = res + step;
 
483
                for (int j = 0; j < ncp; j++) {
 
484
                    fprintf(fy, "% 9.7le\t", out[j]);
 
485
                }
 
486
                fprintf(fy,"\n");
 
487
 
 
488
#ifdef DICT_HW_MEASURE_TIMINGS
 
489
                gettimeofday(&tv2, NULL);
 
490
                time[1] += (tv2.tv_sec - tv3.tv_sec) * 1000000 + (tv2.tv_usec - tv3.tv_usec);
 
491
#endif /* DICT_HW_MEASURE_TIMINGS */
 
492
            }
423
493
        }
424
494
    }
425
 
 
 
495
    
426
496
#ifdef DICT_HW_MEASURE_TIMINGS
427
497
    gettimeofday(&tv3, NULL);
428
498
#endif /* DICT_HW_MEASURE_TIMINGS */
429
499
    
430
 
    if (matlab_mode) {
 
500
    if ((matlab_mode)||(multipass)) {
431
501
        name = g_strdup_printf("%s/data/validx.dat", argv[optind]);
432
 
        writefile(name, res, maximg - 1, ncp);
 
502
        writefile(matlab_mode, name, res, maximg - 1, real_ncp);
433
503
 
434
504
        name = g_strdup_printf("%s/data/validy.dat", argv[optind]);
435
 
        writefile(name, res+step, maximg - 1, ncp);
 
505
        writefile(matlab_mode, name, res+step, maximg - 1, real_ncp);
436
506
    } else {
437
507
        fclose(fx);
438
508
        fclose(fy);