-
Notifications
You must be signed in to change notification settings - Fork 2
/
orsa_main.c
752 lines (602 loc) · 14.2 KB
/
orsa_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
#include "header.h"
#include "proto.h"
static int compare_match(
const void *p1,
const void *p2
)
/*
It's a lexicographical comparison
Compare x1, then y1, then x2, and then y2
*/
{
/*
Compare x1
*/
if ( ((match_struct *)p1)->x1 < ((match_struct *)p2)->x1 )
return -1;
if ( ((match_struct *)p1)->x1 > ((match_struct *)p2)->x1 )
return +1;
/*
If here, same x1
*/
/*
Compare y1
*/
if ( ((match_struct *)p1)->y1 < ((match_struct *)p2)->y1 )
return -1;
if ( ((match_struct *)p1)->y1 > ((match_struct *)p2)->y1 )
return +1;
/*
If here, same x1 and same y1
*/
/*
Compare x2
*/
if ( ((match_struct *)p1)->x2 < ((match_struct *)p2)->x2 )
return -1;
if ( ((match_struct *)p1)->x2 > ((match_struct *)p2)->x2 )
return +1;
/*
If here, same x1, same y1, and same x2
*/
/*
Compare y2
*/
if ( ((match_struct *)p1)->y2 < ((match_struct *)p2)->y2 )
return -1;
if ( ((match_struct *)p1)->y2 > ((match_struct *)p2)->y2 )
return +1;
/*
If here, same x1, same y1, same x2, and same y2
*/
return 0;
}
int orsa_main(
int width,
int height,
char *filename_match,
char *filename_good_match,
char *filename_F,
int ntrials,
unsigned long seed
)
{
FILE *fp;
int match_nbr;
match_struct *match_arr;
int match_ind;
double x1;
double y1;
double x2;
double y2;
int match2_nbr;
match_struct *match2_arr;
int dup_flag;
int prev_match_ind;
double prev_x1;
double prev_y1;
double prev_x2;
double prev_y2;
double dup_tol;
double N[3*3];
double norm;
double normalized_x1;
double normalized_y1;
double normalized_x2;
double normalized_y2;
int ind;
double logalpha0;
double loge0;
double *logcn;
double *logc7;
int random7[7];
int sample7[7];
double F_arr[3*9];
int iter;
int F_nbr;
int F_ind;
int i;
double F[3*3];
double best_nfa;
int best_k;
int *inliers_arr;
int inliers_nbr;
int match2_ind;
double error_max;
error_struct *error_arr;
double min_nfa;
double bestF[3*3];
double FN[3*3];
double Nt[3*3];
double denormalized_F[3*3];
double denormalized_error_max;
match_struct *backup_match_arr;
int vec_index_nbr;
int *vec_index_arr;
int max_iter;
int optim;
int better;
int optimized_orsa= 0;
int feat1_ind;
int feat2_ind;
int prev_feat1_ind;
int prev_feat2_ind;
int j;
/*
Read the file containing the matches
Put the matches in match_arr
*/
fp= fopen(filename_match,"r");
match_nbr= 0;
match_arr= 0;
while ( fscanf(fp,"%d %lg %lg %d %lg %lg",
&feat1_ind,&x1,&y1,&feat2_ind,&x2,&y2) == 6 ) {
if ( match_nbr == 0 ) {
match_arr= (match_struct *)calloc(match_nbr+1,sizeof(match_struct));
}
else {
match_arr= (match_struct *)realloc(match_arr,(match_nbr+1)*sizeof(match_struct));
}
match_arr[match_nbr].feat1_ind= feat1_ind;
match_arr[match_nbr].x1= x1;
match_arr[match_nbr].y1= y1;
match_arr[match_nbr].feat2_ind= feat2_ind;
match_arr[match_nbr].x2= x2;
match_arr[match_nbr].y2= y2;
match_nbr++;
}
fclose(fp);
/*
Initialize seed for random number generator
*/
srand(seed);
/*
Sort the matches lexicographically using library function qsort()
*/
qsort(
match_arr,
match_nbr,
sizeof(match_struct),
compare_match
);
/*
Print the matches after the sort
to make sure it's correctly sorted
*/
/*
for ( match_ind= 0 ; match_ind< match_nbr ; match_ind++ ) {
feat1_ind= match_arr[match_ind].feat1_ind;
x1= match_arr[match_ind].x1;
y1= match_arr[match_ind].y1;
feat2_ind= match_arr[match_ind].feat2_ind;
x2= match_arr[match_ind].x2;
y2= match_arr[match_ind].y2;
fprintf(stdout,"feat1_ind= %d x1= %g y1= %g feat2_ind= %d x2= %g y2= %g\n",
feat1_ind,x1,y1,feat2_ind,x2,y2);
}
*/
/*
Process the matches and get rid of duplicate matches
Put the matches in match2_arr
*/
match2_nbr= 0;
match2_arr= 0;
for ( match_ind= 0 ; match_ind< match_nbr ; match_ind++ ) {
feat1_ind= match_arr[match_ind].feat1_ind;
x1= match_arr[match_ind].x1;
y1= match_arr[match_ind].y1;
feat2_ind= match_arr[match_ind].feat2_ind;
x2= match_arr[match_ind].x2;
y2= match_arr[match_ind].y2;
/*
Since the match array has been sorted,
duplicate matches are adjacent in the match array
*/
/*
See if this match is a duplicate
by looking at previous match
*/
dup_flag= 0;
prev_match_ind= match_ind-1;
if ( prev_match_ind >= 0 ) {
prev_feat1_ind= match_arr[prev_match_ind].feat1_ind;
prev_x1= match_arr[prev_match_ind].x1;
prev_y1= match_arr[prev_match_ind].y1;
prev_feat2_ind= match_arr[prev_match_ind].feat2_ind;
prev_x2= match_arr[prev_match_ind].x2;
prev_y2= match_arr[prev_match_ind].y2;
/*
if ( prev_x1 == x1 &&
prev_y1 == y1 &&
prev_x2 == x2 &&
prev_y2 == y2 ) {
dup_flag= 1;
}
*/
dup_tol= 1.0e-12;
if ( fabs(x1-prev_x1) < dup_tol*(double)width &&
fabs(y1-prev_y1) < dup_tol*(double)height &&
fabs(x2-prev_x2) < dup_tol*(double)width &&
fabs(y2-prev_y2) < dup_tol*(double)height ) {
dup_flag= 1;
}
}
if ( dup_flag == 1 )
continue;
/*
If here,
not a duplicate
*/
if ( match2_nbr == 0 ) {
match2_arr= (match_struct *)calloc(match2_nbr+1,sizeof(match_struct));
}
else {
match2_arr= (match_struct *)realloc(match2_arr,(match2_nbr+1)*sizeof(match_struct));
}
match2_arr[match2_nbr].feat1_ind= feat1_ind;
match2_arr[match2_nbr].x1= x1;
match2_arr[match2_nbr].y1= y1;
match2_arr[match2_nbr].feat2_ind= feat2_ind;
match2_arr[match2_nbr].x2= x2;
match2_arr[match2_nbr].y2= y2;
match2_nbr++;
}
/*
Replace match_arr by match2_arr
*/
if ( match_nbr > 0 )
free(match_arr);
match_nbr= match2_nbr;
match_arr= match2_arr;
fprintf(stdout," Number of matches (after removal of duplicates)= %d\n",match_nbr);
/*
Check that there are enough matches
*/
if ( match_nbr <= 7 ) {
fprintf(stdout," More than 7 matches are needed!\n");
return 1;
}
/*
Backup the matches before normalization
*/
backup_match_arr= (match_struct *)calloc(match_nbr,sizeof(match_struct));
for ( match_ind= 0 ; match_ind< match_nbr ; match_ind++ ) {
feat1_ind= match_arr[match_ind].feat1_ind;
x1= match_arr[match_ind].x1;
y1= match_arr[match_ind].y1;
feat2_ind= match_arr[match_ind].feat2_ind;
x2= match_arr[match_ind].x2;
y2= match_arr[match_ind].y2;
backup_match_arr[match_ind].feat1_ind= feat1_ind;
backup_match_arr[match_ind].x1= x1;
backup_match_arr[match_ind].y1= y1;
backup_match_arr[match_ind].feat2_ind= feat2_ind;
backup_match_arr[match_ind].x2= x2;
backup_match_arr[match_ind].y2= y2;
}
/*
Normalize the coordinates of the matches
*/
norm= 1 / sqrt( (double)width * (double)height );
for ( match_ind= 0 ; match_ind< match_nbr ; match_ind++ ) {
feat1_ind= match_arr[match_ind].feat1_ind;
x1= match_arr[match_ind].x1;
y1= match_arr[match_ind].y1;
feat2_ind= match_arr[match_ind].feat2_ind;
x2= match_arr[match_ind].x2;
y2= match_arr[match_ind].y2;
normalized_x1= ( x1 - 0.5 * (double)width ) * norm;
normalized_y1= ( y1 - 0.5 * (double)height ) * norm;
normalized_x2= ( x2 - 0.5 * (double)width ) * norm;
normalized_y2= ( y2 - 0.5 * (double)height ) * norm;
match_arr[match_ind].x1= normalized_x1;
match_arr[match_ind].y1= normalized_y1;
match_arr[match_ind].x2= normalized_x2;
match_arr[match_ind].y2= normalized_y2;
}
/*
Compute normalization matrix N
*/
N[0*3+0]= norm;
N[0*3+1]= 0;
N[0*3+2]= -0.5 * (double)width * norm;
N[1*3+0]= 0;
N[1*3+1]= norm;
N[1*3+2]= -0.5 * (double)height * norm;
N[2*3+0]= 0;
N[2*3+1]= 0;
N[2*3+2]= 1;
/*
fprintf(stdout,"N=");
for ( ind= 0 ; ind< 3*3 ; ind++ )
fprintf(stdout," %g",N[ind]);
fprintf(stdout,"\n");
*/
/*
Compute log proba of random data term to have error at most 1 pixel
*/
logalpha0= log10(2)+
0.5*log10( ((double)width*(double)width + (double)height*(double)height) /
((double)width * (double)height) );
/*
Pre-compute log probas
*/
loge0= log10( 3 * ((double)match_nbr-7) );
logcn= (double *)calloc(match_nbr+1,sizeof(double));
for ( ind= 0 ; ind<= match_nbr ; ind++ ) {
logcn[ind]= orsa_log_combi(ind,match_nbr);
}
/*
fprintf(stdout,"logcn=");
for ( ind= 0 ; ind< match_nbr+1 ; ind++ )
fprintf(stdout," %g",logcn[ind]);
fprintf(stdout,"\n");
*/
logc7= (double *)calloc(match_nbr+1,sizeof(double));
for ( ind= 0 ; ind<= match_nbr ; ind++ ) {
logc7[ind]= orsa_log_combi(7,ind);
}
/*
fprintf(stdout,"logc7=");
for ( ind= 0 ; ind< match_nbr+1 ; ind++ )
fprintf(stdout," %g",logc7[ind]);
fprintf(stdout,"\n");
*/
vec_index_nbr= match_nbr;
vec_index_arr= (int *)calloc(vec_index_nbr,sizeof(int));
for ( match_ind= 0 ; match_ind< match_nbr ; match_ind++ )
vec_index_arr[match_ind]= match_ind;
if ( optimized_orsa == 1 ) {
/*
Reduce the number of trials by 10%
We'll add those back when we have found a meaningful model
*/
max_iter = ntrials - ntrials/10;
}
else {
max_iter = ntrials;
}
iter= 0;
min_nfa= 1.0e32;
optim= 0;
inliers_arr= 0;
inliers_nbr= 0;
/*
Allocate memory for errors
*/
error_arr= (error_struct *)calloc(match_nbr,sizeof(error_struct));
START:
iter++;
/*
Pick 7 matches at random
*/
orsa_pick_7_random_matches(
random7,
vec_index_nbr
);
for ( ind= 0 ; ind< 7 ; ind++ )
sample7[ind]= vec_index_arr[ random7[ind] ];
/*
Get the fundamental matrices
Either 1 fundamental matrix is returned or 3
*/
F_nbr= orsa_epipolar(
match_arr,
match_nbr,
sample7,
F_arr
);
/*
Loop on 1 fundamental matrix or 3
*/
/*
for ( F_ind= 0 ; F_ind< F_nbr ; F_ind++ ) {
*/
for ( F_ind= F_nbr-1 ; F_ind>= 0 ; F_ind-- ) {
for ( i= 0 ; i< 9 ; i++ )
F[i]= F_arr[F_ind*9+i];
/*
fprintf(stdout,"F=");
for ( ind= 0 ; ind< 9 ; ind++ )
fprintf(stdout," %g",F[ind]);
fprintf(stdout,"\n");
*/
/*
Compute the errors for all matches and sort them
*/
orsa_compute_sort_errors(
match_arr,
match_nbr,
F,
error_arr
);
/*
Compute the best nfa (number of false alarms)
*/
orsa_best_number_false_alarms(
error_arr,
match_nbr,
logalpha0,
loge0,
logcn,
logc7,
&best_nfa,
&best_k
);
/*
Initialize to
haven't found a better model
*/
better= 0;
if ( best_nfa < min_nfa ) {
/*
Have found a better model
*/
better= 1;
min_nfa= best_nfa;
fprintf(stdout," iter= %d log(nfa)= %g\n",iter,min_nfa);
/*
Free inliers_arr
*/
if ( inliers_nbr > 0 )
free(inliers_arr);
/*
Update the inliers
*/
inliers_nbr= best_k+1;
inliers_arr= (int *)calloc(inliers_nbr,sizeof(int));
for ( match2_ind= 0 ; match2_ind<= best_k ; match2_ind++ ) {
match_ind= error_arr[match2_ind].match_ind;
inliers_arr[match2_ind]= match_ind;
}
error_max= error_arr[best_k].error;
/*
Update optimum fundamental matrix
*/
for ( i= 0 ; i< 9 ; i++ )
bestF[i]= F[i];
}
if ( optimized_orsa == 1 ) {
if ( (better == 1 && min_nfa < 0) ||
(iter == max_iter && !optim) ) {
/*
We either have found a meaningful model or
we have gone through 90% of trials
*/
if ( !optim ) {
/*
Add back the 10% iterations we took out
*/
optim= 1;
max_iter= ntrials;
}
/*
From now on,
we are gonna pick samples from the inliers
*/
if ( vec_index_nbr > 0 )
free(vec_index_arr);
vec_index_nbr= inliers_nbr;
vec_index_arr= (int *)calloc(vec_index_nbr,sizeof(int));
for ( match2_ind= 0 ; match2_ind< vec_index_nbr ; match2_ind++ )
vec_index_arr[match2_ind]= inliers_arr[match2_ind];;
}
}
} /* loop on the fundamental matrices */
/*
Let's see if we are done
*/
if ( iter < max_iter )
goto START;
error_max= sqrt(error_max);
/*
Denormalize the max error
*/
denormalized_error_max= error_max/norm;
fprintf(stdout," Number of inliers = %d\n",inliers_nbr);
fprintf(stdout," Max error = %g\n",denormalized_error_max);
/*
Denormalize the best fundamental matrix
F = Nt * F * N
*/
for ( i= 0 ; i< 9 ; i++ )
F[i]= bestF[i];
math_matrix_matrix_product(
F,
3,
3,
N,
3,
3,
FN
);
math_matrix_transpose(
N,
3,
3,
Nt
);
math_matrix_matrix_product(
Nt,
3,
3,
FN,
3,
3,
denormalized_F
);
fprintf(stdout," Best F = [");
fprintf(stdout," %g %g %g;",
denormalized_F[0*3+0],denormalized_F[0*3+1],denormalized_F[0*3+2]);
fprintf(stdout," %g %g %g;",
denormalized_F[1*3+0],denormalized_F[1*3+1],denormalized_F[1*3+2]);
fprintf(stdout," %g %g %g ]\n",
denormalized_F[2*3+0],denormalized_F[2*3+1],denormalized_F[2*3+2]);
/*
Write the denormalized F
*/
fp= fopen(filename_F,"w");
for ( i= 0 ; i< 3 ; i++ ) {
for ( j= 0 ; j< 3 ; j++ ) {
fprintf(fp,"%g ",denormalized_F[i*3+j]);
}
}
fclose(fp);
/*
Write the good matches
*/
fp= fopen(filename_good_match,"w");
for ( match2_ind= 0 ; match2_ind< inliers_nbr ; match2_ind++ ) {
match_ind= inliers_arr[match2_ind];
feat1_ind= backup_match_arr[match_ind].feat1_ind;
x1= backup_match_arr[match_ind].x1;
y1= backup_match_arr[match_ind].y1;
feat2_ind= backup_match_arr[match_ind].feat2_ind;
x2= backup_match_arr[match_ind].x2;
y2= backup_match_arr[match_ind].y2;
fprintf(fp,"%d ",feat1_ind);
fprintf(fp,"%g ",x1);
fprintf(fp,"%g ",y1);
fprintf(fp,"%d ",feat2_ind);
fprintf(fp,"%g ",x2);
fprintf(fp,"%g ",y2);
fprintf(fp,"\n");
}
fclose(fp);
/*
Free match_arr
*/
if ( match_nbr > 0 )
free(match_arr);
/*
Free backup_match_arr
*/
if ( match_nbr > 0 )
free(backup_match_arr);
/*
Free logcn
*/
if ( match_nbr+1 > 0 )
free(logcn);
/*
Free logc7
*/
if ( match_nbr+1 > 0 )
free(logc7);
/*
Free vec_index_arr
*/
if ( vec_index_nbr > 0 )
free(vec_index_arr);
/*
Free error_arr
*/
if ( match_nbr > 0 )
free(error_arr);
/*
Free inliers_arr
*/
if ( inliers_nbr > 0 )
free(inliers_arr);
return 0;
}