forked from snavely/bundler_sfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBundleTwo.cpp
2026 lines (1596 loc) · 62.7 KB
/
BundleTwo.cpp
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008-2010 Noah Snavely (snavely (at) cs.cornell.edu)
* and the University of Washington
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/* BundleTwo.cpp */
/* Two-frame bundle adjustment */
#include <assert.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <time.h>
#include "BundlerApp.h"
#include "Bundle.h"
#include "BundleAdd.h"
// #include "Epipolar.h"
// #include "Register.h"
#include "Decompose.h"
#include "SifterUtil.h"
#include "TwoFrameModel.h"
#include "sfm.h"
#include "defines.h"
#include "filter.h"
#include "horn.h"
#include "matrix.h"
#include "qsort.h"
#include "triangulate.h"
#include "util.h"
#include "vector.h"
#ifndef WIN32
#include <ext/hash_map>
#else
#include <hash_map>
#endif
/* Stolen from TwoFrameModel.cpp */
static void WriteVector(FILE *f, int n, const double *v)
{
for (int i = 0; i < n; i++) {
fprintf(f, "%0.16e ", v[i]);
}
fprintf(f, "\n");
}
static void WriteCamera(FILE *f, const camera_params_t &camera)
{
WriteVector(f, 9, camera.R);
WriteVector(f, 3, camera.t);
fprintf(f, "%0.16e\n", camera.f);
}
void TwoFrameModel::WriteWithProjections(FILE *f,
const std::vector<TrackData> &tracks,
int i1, int i2,
const ImageData &img1,
const ImageData &img2) const
{
std::vector<v2_t> proj1, proj2;
proj1.resize(m_num_points);
proj2.resize(m_num_points);
fprintf(f, "%d\n", m_num_points);
fprintf(f, "%0.9f\n", m_angle);
for (int i = 0; i < m_num_points; i++) {
int tr;
if (m_tracks == NULL)
tr = -1;
else
tr = m_tracks[i];
fprintf(f, "%d %0.16e %0.16e %0.16e\n", tr,
Vx(m_points[i]), Vy(m_points[i]), Vz(m_points[i]));
int k1 = -1, k2 = -1;
if (tr != -1) {
const ImageKeyVector &v = tracks[tr].m_views;
int num_views = (int) v.size();
for (int j = 0; j < num_views; j++) {
if (v[j].first == i1)
k1 = v[j].second;
else if (v[j].first == i2)
k2 = v[j].second;
}
} else {
k1 = m_keys1[i];
k2 = m_keys2[i];
}
assert(k1 >= 0 && k2 >= 0);
double x1 = img1.m_keys[k1].m_x;
double y1 = img1.m_keys[k1].m_y;
double x2 = img2.m_keys[k2].m_x;
double y2 = img2.m_keys[k2].m_y;
if (img1.m_fisheye)
img1.UndistortPoint(x1, y1, x1, y1);
if (img2.m_fisheye)
img2.UndistortPoint(x2, y2, x2, y2);
fprintf(f, "%0.9e %0.9e %0.9e %0.9e\n", x1, y1, x2, y2);
proj1[i] = v2_new(x1, y1);
proj2[i] = v2_new(x2, y2);
}
WriteCamera(f, m_camera0);
WriteCamera(f, m_camera1);
WriteVector(f, 9, m_C0);
WriteVector(f, 9, m_C1);
#if 0
/* Compute the translation using DLT */
double pts1[30] = {
0.0210, 0.3174, 1.0000,
-0.3350, 0.0522, 1.0000,
-0.1398, 0.3074, 1.0000,
-0.8325, -0.1650, 1.0000,
-1.2247, 0.4144, 1.0000,
-0.4274, 0.6393, 1.0000,
-0.5732, -0.0541, 1.0000,
-0.2511, -0.0100, 1.0000,
-0.0131, 0.4415, 1.0000,
0.1108, 0.1131, 1.0000
};
double pts2[30] = {
0.1993, -0.1316, 1.0000,
15.3933, 1.5673, 1.0000,
0.4956, -0.0401, 1.0000,
0.8608, 0.3763, 1.0000,
0.5506, 0.1301, 1.0000,
0.5927, -0.3441, 1.0000,
2.5845, 0.5888, 1.0000,
-40.0390, -13.0980, 1.0000,
0.2053, -0.0880, 1.0000,
0.0701, 0.5576, 1.0000,
};
double R1[9] = {
0.8440, 0.1616, 0.5113,
0.2310, 0.7509, -0.6187,
-0.4840, 0.6403, 0.5965,
};
double t1[3] = {
-0.3775, -0.2959, -1.4751
};
double R2[9] = {
0.8189, 0.4004, 0.4113,
-0.0417, 0.7561, -0.6532,
-0.5725, 0.5177, 0.6358,
};
double t2[3] = {
-0.2340, 0.1184, 0.3148
};
double R12[9];
matrix_transpose_product2(3, 3, 3, 3, R2, R1, R12);
double A[30];
for (int i = 0; i < 10; i++) {
double *p1 = pts1 + 3 * i;
double *p2 = pts2 + 3 * i;
double p1b[3];
matrix_product(3, 3, 3, 1, R12, p1, p1b);
p1[0] = p1b[0] / p1b[2];
p1[1] = p1b[1] / p1b[2];
double *r = A + i * 3;
r[0] = -p1[1] + p2[1];
r[1] = p1[0] - p2[0];
r[2] = -p1[0] * p2[1] + p1[1] * p2[0];
}
double x[3];
matrix_minimum_unit_norm_solution(10, 3, A, x);
double c1[3], c2[3];
matrix_transpose_product(3, 3, 3, 1, R1, t1, c1);
matrix_scale(3, 1, c1, -1.0, c1);
matrix_transpose_product(3, 3, 3, 1, R2, t2, c2);
matrix_scale(3, 1, c2, -1.0, c2);
double c[3];
matrix_diff(3, 1, 3, 1, c2, c1, c);
double t[3];
matrix_product(3, 3, 3, 1, (double *) R2, c, t);
matrix_scale(3, 1, t, -1.0, t);
double norm = matrix_norm(3, 1, t);
matrix_scale(3, 1, t, 1.0 / norm, t);
double dot;
matrix_product(1, 3, 3, 1, x, t, &dot);
printf("%0.3f %0.3f %0.3f -> %0.3f %0.3f %0.3f [ %0.3f ]\n",
x[0], x[1], x[2], t[0], t[1], t[2], dot);
#endif
#if 0
int num_eqns = m_num_points;
int num_vars = 3;
double *A = new double[num_eqns * num_vars];
for (int i = 0; i < m_num_points; i++) {
v2_t p1 = v2_scale(-1.0 / m_camera0.f, proj1[i]);
v2_t p2 = v2_scale(-1.0 / m_camera1.f, proj2[i]);
double p1a[3] = { Vx(p1), Vy(p1), 1.0 };
double p1b[3];
matrix_product(3, 3, 3, 1, m_camera1.R, p1a, p1b);
// p1 = v2_new(p1b[0] / p1b[2], p1b[1] / p1b[2]);
p1 = v2_new(p1b[0], p1b[1]);
double *r = A + i * num_vars;
r[0] = -Vy(p1) + Vy(p2) * p1b[2];
r[1] = Vx(p1) - Vx(p2) * p1b[2];
r[2] = -Vx(p1) * Vy(p2) + Vy(p1) * Vx(p2);
}
double x[3];
matrix_minimum_unit_norm_solution(num_eqns, num_vars, A, x);
double c[3];
matrix_diff(3, 1, 3, 1, (double *) m_camera1.t, (double *) m_camera0.t, c);
double t[3];
matrix_product(3, 3, 3, 1, (double *) m_camera1.R, c, t);
matrix_scale(3, 1, t, -1.0, t);
double norm = matrix_norm(3, 1, t);
matrix_scale(3, 1, t, 1.0 / norm, t);
double dot;
matrix_product(1, 3, 3, 1, x, t, &dot);
printf("%0.3f %0.3f %0.3f -> %0.3f %0.3f %0.3f [ %0.3f ]\n",
x[0], x[1], x[2], t[0], t[1], t[2], dot);
delete [] A;
#endif
}
#if 0
void WriteModelsProjections(ModelMap &models, int num_images,
const std::vector<TrackData> &track_data,
std::vector<ImageData> &image_data, char *out_file)
{
bool preload_keys = true;
if (num_images > 30000)
preload_keys = false;
if (preload_keys) {
for (int i = 0; i < num_images; i++) {
image_data[i].LoadKeys(false);
}
}
FILE *f = fopen(out_file, "w");
if (f == NULL) {
printf("[WriteModelsProjections] "
"Error opening file %s for reading\n", out_file);
} else {
fprintf(f, "%d\n", num_images);
// FIXME LOOP
for (int i = 0; i < num_images; i++) {
ImageData &img_i = image_data[i];
if (!preload_keys)
image_data[i].LoadKeys(false);
for (int j = i+1; j < num_images; j++) {
if (models.Contains(GetMatchIndex(i, j))) {
if (!preload_keys)
image_data[j].LoadKeys(false);
ImageData &img_j = image_data[j];
fprintf(f, "%d %d\n", i, j);
models.GetModel(GetMatchIndex(i,j)).
WriteWithProjections(f, track_data, i, j,
img_i, img_j);
if (!preload_keys)
image_data[j].UnloadKeys();
}
}
if (!preload_keys)
image_data[i].UnloadKeys();
}
fclose(f);
}
if (!preload_keys) {
for (int i = 0; i < num_images; i++) {
image_data[i].UnloadKeys();
}
}
}
#endif
#if 0
static void FixPEdges(int num_images, ModelMap &models, PEdgeMap &p_edges)
{
const double MIN_ANGLE = 1.5;
for (int i = 0; i < num_images; i++) {
for (int j = i+1; j < num_images; j++) {
int idx = i * num_images + j;
if (models.find(idx) != models.end()) {
if (models[idx].m_angle < MIN_ANGLE) {
printf("[FixPEdges] Angle (%d,%d) too small [%0.3f]\n",
i, j, models[idx].m_angle);
if (models[idx].m_num_points >= 64) {
printf("[FixPEdges] Replacing with p-edge\n");
p_edges[idx] = true;
}
models.erase(idx);
}
}
}
}
}
#endif
static void FixScaffoldEdges(int num_images, ModelMap &models)
{
const double MIN_ANGLE = 1.5;
// FIXME LOOP ?
for (int i = 0; i < num_images; i++) {
for (int j = i+1; j < num_images; j++) {
MatchIndex idx = GetMatchIndex(i,j); // i * num_images + j;
if (models.Contains(idx)) {
TwoFrameModel &m = models.GetModel(idx);
if (m.m_angle < MIN_ANGLE) {
printf("[FixScaffoldEdges] "
"Angle (%d,%d) too small [%0.3f]\n",
i, j, m.m_angle);
#if 0
if (m.m_num_points >= 64) {
printf("[FixScaffoldEdges] "
"Replacing with connector edge\n");
m.TurnOffScaffold();
// p_edges[idx] = true;
} else {
models.erase(idx);
}
#else
models.RemoveModel(idx);
#endif
}
}
}
}
}
#define MATCH_THRESHOLD 28 // 16
static void PermuteMatches(std::vector<KeypointMatch> &matches)
{
int num_matches = (int) matches.size();
int *perm = new int[num_matches];
generate_permutation(num_matches, perm);
std::vector<KeypointMatch> matches_new;
matches_new.resize(num_matches);
for (int i = 0; i < num_matches; i++) {
matches_new[i] = matches[perm[i]];
}
matches = matches_new;
delete [] perm;
}
static void ClearKeys(ImageData &data)
{
/* Clear keys */
std::vector<Keypoint>::iterator iter;
for (iter = data.m_keys.begin(); iter != data.m_keys.end(); iter++) {
iter->m_extra = -1;
}
for (iter = data.m_keys.begin(); iter != data.m_keys.end(); iter++) {
iter->m_extra = -1;
}
}
double BundlerApp::RunSFMNecker(int i1, int i2,
camera_params_t *cameras,
int num_points, v3_t *points, v3_t *colors,
std::vector<ImageKeyVector> &pt_views,
camera_params_t *cameras_new,
v3_t *points_new,
double threshold)
{
memcpy(points_new, points, sizeof(v3_t) * num_points);
memcpy(cameras_new, cameras, sizeof(camera_params_t) * 2);
/* Swap cameras */
camera_params_t tmp = cameras_new[0];
memcpy(cameras_new[0].R, cameras_new[1].R, sizeof(double) * 9);
memcpy(cameras_new[0].t, cameras_new[1].t, sizeof(double) * 3);
memcpy(cameras_new[1].R, tmp.R, sizeof(double) * 9);
memcpy(cameras_new[1].t, tmp.t, sizeof(double) * 3);
for (int i = 0; i < num_points; i++) {
if (pt_views[i].size() == 0)
continue;
int k1 = pt_views[i][0].second;
int k2 = pt_views[i][1].second;
double proj1[2] = { GetKey(i1,k1).m_x, GetKey(i1,k1).m_y };
double proj2[2] = { GetKey(i2,k2).m_x, GetKey(i2,k2).m_y };
if (m_optimize_for_fisheye) {
double x1 = proj1[0];
double y1 = proj1[1];
double x2 = proj2[0];
double y2 = proj2[1];
m_image_data[i1].UndistortPoint(x1, y1, proj1[0], proj1[1]);
m_image_data[i2].UndistortPoint(x2, y2, proj2[0], proj2[1]);
}
v2_t p = v2_new(proj1[0], proj1[1]);
v2_t q = v2_new(proj2[0], proj2[1]);
/* Triangulate the point */
bool in_front = true;
double angle = 0.0;
double error = 0.0;
points_new[i] = Triangulate(p, q, cameras_new[0], cameras_new[1],
error, in_front, angle,
true);
}
int added_order[2] = { i1, i2 };
double error1;
error1 = RunSFM_SBA(num_points, 2, 0, false,
cameras_new, points_new, added_order,
colors, pt_views,
threshold, NULL, NULL, NULL, NULL, true);
return error1;
}
bool BundlerApp::BundleTwoFrame(int i1, int i2, TwoFrameModel *model,
double &angle_out, int &num_pts_out,
bool bundle_from_tracks)
{
const double TERM_THRESH = 1.0e-12;
// assert(!m_estimate_distortion && !m_fixed_focal_length);
assert(!m_fixed_focal_length);
if (!m_image_data[i1].m_has_init_focal ||
!m_image_data[i2].m_has_init_focal) {
printf("[BundleTwoFrame] "
"Error: two frames must have focal length estimates\n");
return false;
}
camera_params_t cameras[2];
/* Load the keys for the images */
if (!m_image_data[i1].m_keys_loaded)
m_image_data[i1].LoadKeys(false, !m_optimize_for_fisheye);
if (!m_image_data[i2].m_keys_loaded)
m_image_data[i2].LoadKeys(false, !m_optimize_for_fisheye);
// #define USE_COLORS
#ifdef USE_COLORS
m_image_data[i1].ReadKeyColors();
m_image_data[i2].ReadKeyColors();
#endif /* USE_COLORS */
if (bundle_from_tracks)
SetMatchesFromTracks(i1, i2);
MatchIndex list_idx;
if (i1 < i2)
list_idx = GetMatchIndex(i1, i2); // i1 * num_images + i2;
else
list_idx = GetMatchIndex(i2, i1); // i2 * num_images + i1;
unsigned int num_matches = m_matches.GetNumMatches(list_idx);
if (num_matches > m_image_data[i1].m_keys.size() - 5 &&
num_matches > m_image_data[i2].m_keys.size() - 5) {
printf("[BundleTwoFrame] Identical images!\n");
angle_out = 0.0;
num_pts_out = (int) num_matches;
return true;
}
if (m_keypoint_border_width > 0) {
RemoveMatchesNearBorder(i1, i2, m_keypoint_border_width);
// int num_matches = (int) m_match_lists[list_idx].size();
int num_matches = m_matches.GetNumMatches(list_idx);
if (num_matches < MATCH_THRESHOLD) {
printf("[BundleTwoFrame] Removed too many matches\n");
return false;
}
}
InitializeCameraParams(m_image_data[i1], cameras[0]);
InitializeCameraParams(m_image_data[i2], cameras[1]);
std::vector<int> tracks;
std::vector<ImageKeyVector> pt_views;
// int num_init_cams = 0;
/* Clear keys */
std::vector<Keypoint>::iterator iter;
for (iter = m_image_data[i1].m_keys.begin();
iter != m_image_data[i1].m_keys.end();
iter++) {
iter->m_extra = -1;
}
for (iter = m_image_data[i2].m_keys.begin();
iter != m_image_data[i2].m_keys.end();
iter++) {
iter->m_extra = -1;
}
/* Put first camera at origin */
cameras[0].R[0] = 1.0; cameras[0].R[1] = 0.0; cameras[0].R[2] = 0.0;
cameras[0].R[3] = 0.0; cameras[0].R[4] = 1.0; cameras[0].R[5] = 0.0;
cameras[0].R[6] = 0.0; cameras[0].R[7] = 0.0; cameras[0].R[8] = 1.0;
/* Initialize the positions of the cameras (using constraints,
* if provided) */
if (m_image_data[i1].m_camera.m_constrained[0])
cameras[0].t[0] = m_image_data[i1].m_camera.m_constraints[0];
else
cameras[0].t[0] = 0.0;
if (m_image_data[i1].m_camera.m_constrained[1])
cameras[0].t[1] = m_image_data[i1].m_camera.m_constraints[1];
else
cameras[0].t[1] = 0.0;
if (m_image_data[i1].m_camera.m_constrained[2])
cameras[0].t[2] = m_image_data[i1].m_camera.m_constraints[2];
else
cameras[0].t[2] = 0.0;
if (m_image_data[i1].m_has_init_focal)
cameras[0].f = m_image_data[i1].m_init_focal;
else
cameras[0].f = m_init_focal_length;
if (m_image_data[i2].m_has_init_focal)
cameras[1].f = m_image_data[i2].m_init_focal;
else
cameras[1].f = m_init_focal_length;
SetCameraConstraints(i1, cameras + 0);
SetCameraConstraints(i2, cameras + 1);
if (m_constrain_focal) {
SetFocalConstraint(m_image_data[i1], cameras + 0);
SetFocalConstraint(m_image_data[i2], cameras + 1);
}
printf("[Sifter::BundleTwoFrame] Estimating relative pose...\n");
fflush(stdout);
bool success = EstimateRelativePose2(i1, i2, cameras[0], cameras[1]);
fflush(stdout);
if (!success)
return false;
// unsigned int num_matches = (int) m_match_lists[list_idx].size();
// unsigned int num_matches = m_matches.GetNumMatches(list_idx);
/* **** Set up the initial 3D points **** */
printf("[BundleTwoFrame] Adding initial matches...\n");
fflush(stdout);
int pt_count = 0;
v3_t *points = new v3_t[num_matches];
#ifdef USE_COLORS
v3_t *colors = new v3_t[num_matches];
#endif
double angle_sum = 0.0;
int num_in_back = 0, num_skipped = 0;
// std::vector<KeypointMatch> matches = m_match_lists[list_idx];
std::vector<KeypointMatch> matches = m_matches.GetMatchList(list_idx);
PermuteMatches(matches);
// #define OUTPUT_POINT_STATUS
#ifdef OUTPUT_POINT_STATUS
m_image_data[i1].LoadImage();
m_image_data[i2].LoadImage();
img_t *pt_img1 = img_scale(m_image_data[i1].m_img, 2);
img_t *pt_img2 = img_scale(m_image_data[i2].m_img, 2);
m_image_data[i1].UnloadImage();
m_image_data[i2].UnloadImage();
#endif
for (unsigned int i = 0; i < num_matches; i++) {
int key_idx1 = matches[i].m_idx1;
int key_idx2 = matches[i].m_idx2;
#if 0
printf(" Adding match %d ==> %d [%d]\n",
key_idx1, key_idx2, pt_count);
#endif
/* Triangulate the point */
double xp1, yp1, xp2, yp2;
double x_proj1, y_proj1, x_proj2, y_proj2;
xp1 = x_proj1 = GetKey(i1,key_idx1).m_x;
yp1 = y_proj1 = GetKey(i1,key_idx1).m_y;
xp2 = x_proj2 = GetKey(i2,key_idx2).m_x;
yp2 = y_proj2 = GetKey(i2,key_idx2).m_y;
if (m_optimize_for_fisheye) {
m_image_data[i1].UndistortPoint(x_proj1, y_proj1,
x_proj1, y_proj1);
m_image_data[i2].UndistortPoint(x_proj2, y_proj2,
x_proj2, y_proj2);
}
double error;
v2_t p = v2_new(x_proj1, y_proj1);
v2_t q = v2_new(x_proj2, y_proj2);
bool in_front = true;
double angle = 0.0;
points[pt_count] = Triangulate(p, q, cameras[0], cameras[1],
error, in_front, angle,
true);
if (m_optimize_for_fisheye) {
/* Project the point */
double tmp[3], tmp2[3];
matrix_diff(3, 1, 3, 1, points[pt_count].p, cameras[0].t, tmp);
matrix_product331(cameras[0].R, tmp, tmp2);
double px1 = -cameras[0].f * tmp2[0] / tmp2[2];
double py1 = -cameras[0].f * tmp2[1] / tmp2[2];
matrix_diff(3, 1, 3, 1, points[pt_count].p, cameras[1].t, tmp);
matrix_product331(cameras[1].R, tmp, tmp2);
double px2 = -cameras[1].f * tmp2[0] / tmp2[2];
double py2 = -cameras[1].f * tmp2[1] / tmp2[2];
m_image_data[i1].DistortPoint(px1, py1, px1, py1);
m_image_data[i2].DistortPoint(px2, py2, px2, py2);
double dx1 = px1 - xp1;
double dy1 = py1 - yp1;
double dx2 = px2 - xp2;
double dy2 = py2 - yp2;
error = 0.5 * (sqrt(dx1 * dx1 + dy1 * dy1) +
sqrt(dx2 * dx2 + dy2 * dy2));
}
#if 0
printf(" tri.error[%d] = %0.3f, %0.3f, %d\n",
i, error, RAD2DEG(angle), in_front ? 1 : 0);
#endif
#ifdef OUTPUT_POINT_STATUS
double x_img1 = 0.5 * (xp1 + 0.5 * m_image_data[i1].GetWidth());
double y_img1 = 0.5 * (yp1 + 0.5 * m_image_data[i1].GetHeight());
double x_img2 = 0.5 * (xp2 + 0.5 * m_image_data[i2].GetWidth());
double y_img2 = 0.5 * (yp2 + 0.5 * m_image_data[i2].GetHeight());
#endif
if (error > 10.0) {
#ifdef OUTPUT_POINT_STATUS
img_draw_pt(pt_img1, iround(x_img1), iround(y_img1), 4,
0xff, 0x0, 0x0);
img_draw_pt(pt_img2, iround(x_img2), iround(y_img2), 4,
0xff, 0x0, 0x0);
#endif
// printf(" skipping point\n");
num_skipped++;
continue;
}
if (!in_front) {
#ifdef OUTPUT_POINT_STATUS
img_draw_pt(pt_img1, iround(x_img1), iround(y_img1), 4,
0x0, 0x0, 0xff);
img_draw_pt(pt_img2, iround(x_img2), iround(y_img2), 4,
0x0, 0x0, 0xff);
#endif
num_in_back++;
continue;
}
#ifdef OUTPUT_POINT_STATUS
img_draw_pt(pt_img1, iround(x_img1), iround(y_img1), 4,
0x0, 0xff, 0x0);
img_draw_pt(pt_img2, iround(x_img2), iround(y_img2), 4,
0x0, 0xff, 0x0);
#endif
angle_sum += angle;
#ifdef USE_COLORS
/* Get the color of the point */
unsigned char r = GetKey(i1,key_idx1).m_r;
unsigned char g = GetKey(i1,key_idx1).m_g;
unsigned char b = GetKey(i1,key_idx1).m_b;
colors[pt_count] = v3_new((double) r, (double) g, (double) b);
#endif
GetKey(i1,key_idx1).m_extra = pt_count;
GetKey(i2,key_idx2).m_extra = pt_count;
if (bundle_from_tracks) {
int track_idx = GetKey(i1,key_idx1).m_track;
m_track_data[track_idx].m_extra = pt_count;
tracks.push_back(track_idx);
}
ImageKeyVector views;
views.push_back(ImageKey(0, key_idx1));
views.push_back(ImageKey(1, key_idx2));
pt_views.push_back(views);
pt_count++;
} /* end loop through matches */
#ifdef OUTPUT_POINT_STATUS
char ptbuf[256];
sprintf(ptbuf, "pt%03d-%03d.bmp", i1, i2);
img_write_bmp_file(pt_img1, ptbuf);
sprintf(ptbuf, "pt%03d-%03d.bmp", i2, i1);
img_write_bmp_file(pt_img2, ptbuf);
img_free(pt_img1);
img_free(pt_img2);
#endif
double angle_avg = angle_sum / pt_count;
printf(" Average angle: %0.3f\n", RAD2DEG(angle_avg));
printf(" In-back pct : %d / %d (%0.3f%%)\n", num_in_back, num_matches,
100.0 * num_in_back / num_matches);
printf(" Skipped pct : %d / %d (%0.3f%%)\n", num_skipped, num_matches,
100.0 * num_skipped / num_matches);
fflush(stdout);
angle_out = RAD2DEG(angle_avg);
num_pts_out = pt_count;
if (RAD2DEG(angle_avg) < 0.5 /*1.5*/) { /* Thresh on triangulation angle */
printf("[BundleTwoFrame] Average tri.angle too small, "
"aborting!\n");
ClearKeys(m_image_data[i1]);
ClearKeys(m_image_data[i2]);
return false;
}
if ((double) num_in_back / num_matches > 0.30 /*0.035*/) {
/* Too many points are in back of the cameras, abort */
printf("[BundleTwoFrame] Too many points [%d / %d / %0.3f] "
"in back of the cameras, aborting!\n",
num_in_back, num_matches, 100.0 * num_in_back / num_matches);
ClearKeys(m_image_data[i1]);
ClearKeys(m_image_data[i2]);
return false;
}
if (pt_count < 20) {
ClearKeys(m_image_data[i1]);
ClearKeys(m_image_data[i2]);
return false;
}
// m_match_lists[list_idx].clear();
m_matches.ClearMatch(list_idx);
matches.clear();
/* Add constraints to camera 0 to fix position and rotation */
cameras[0].constrained[0] = true;
cameras[0].constrained[1] = true;
cameras[0].constrained[2] = true;
cameras[0].constrained[3] = true;
cameras[0].constrained[4] = true;
cameras[0].constrained[5] = true;
cameras[0].constraints[0] = 0.0;
cameras[0].constraints[1] = 0.0;
cameras[0].constraints[2] = 0.0;
cameras[0].constraints[3] = 0.0;
cameras[0].constraints[4] = 0.0;
cameras[0].constraints[5] = 0.0;
cameras[0].weights[0] = 1.0e6;
cameras[0].weights[1] = 1.0e6;
cameras[0].weights[2] = 1.0e6;
cameras[0].weights[3] = 1.0e6;
cameras[0].weights[4] = 1.0e6;
cameras[0].weights[5] = 1.0e6;
/* Constraint radial distortion parameters */
cameras[0].constrained[7] = true;
cameras[0].constrained[8] = true;
cameras[0].constraints[7] = 0.0;
cameras[0].constraints[8] = 0.0;
cameras[0].weights[7] = 1.0e2;
cameras[0].weights[8] = 1.0e2;
cameras[1].constrained[7] = true;
cameras[1].constrained[8] = true;
cameras[1].constraints[7] = 0.0;
cameras[1].constraints[8] = 0.0;
cameras[1].weights[7] = 1.0e2;
cameras[1].weights[8] = 1.0e2;
int added_order[2] = { i1, i2 };
/* ********** Bundle adjust the two views ********** */
// std::vector<ImageKeyVector> pt_views_new = pt_views;
#if 1
#ifdef USE_COLORS
double error0 = RunSFM_SBA(pt_count, 2, 0, false,
cameras, points, added_order,
colors, pt_views,
TERM_THRESH, NULL, NULL, NULL, NULL, true);
#else
double error0 = RunSFM_SBA(pt_count, 2, 0, false,
cameras, points, added_order, NULL, pt_views,
TERM_THRESH, NULL, NULL, NULL, NULL, true);
#endif
#endif
#if 0
camera_params_t cameras_new[2];
v3_t *points_new = new v3_t[pt_count];
double error1 = RunSFMNecker(i1, i2, cameras, pt_count, points, colors,
pt_views_new, cameras_new, points_new,
TERM_THRESH);
double error = MIN(error0, error1);
if (error1 < error0) {
printf(" Switching to reflected solution (%0.3f < %0.3f)\n",
error1, error0);
memcpy(points, points_new, pt_count * sizeof(v3_t));
memcpy(cameras, cameras_new, 2 * sizeof(camera_params_t));
pt_views = pt_views_new;
} else {
printf(" Keeping initial solution (%0.3f < %0.3f)\n",
error0, error1);
}
delete [] points_new;
pt_views_new.clear();
#endif
if (error0 > 0.5) {
printf("[BundleTwoFrame] "
"Error of %0.3f [%d,%d, %d pts] is too high!\n",
error0, i1, i2, pt_count);
#ifdef USE_COLORS
char buf[256];
camera_params_t cameras_tmp[2] =
{ model->m_camera0, model->m_camera1 };
sprintf(buf, "model-%03d-%03d.ply", i1, i2);
DumpPointsToPly(m_output_directory, buf, pt_count, 2,
model->m_points, colors, cameras_tmp, false);
#endif
delete [] points;
ClearKeys(m_image_data[i1]);
ClearKeys(m_image_data[i2]);
return false;
}
/* Remove outliers and points that are outside the 90% distance
* threshold */
double *dists = new double[pt_count];
for (int i = 0; i < pt_count; i++) {
dists[i] = v3_magsq(points[i]);
}
qsort_ascending();
double dist_threshold =
kth_element_copy(pt_count, iround(0.90 * pt_count), dists);
dist_threshold = MAX(10000.0 /*500.0*/, dist_threshold);
if (pt_count < 30)
dist_threshold = DBL_MAX;
/* Compute the angle between the rays used to triangulate the
* point */
double *angles = new double[pt_count];
for (int i = 0; i < pt_count; i++) {
if ((int) pt_views[i].size() > 0) {
double *pos = points[i].p;
double ray1[3], ray2[3];
matrix_diff(3, 1, 3, 1, pos, cameras[0].t, ray1);
matrix_diff(3, 1, 3, 1, pos, cameras[1].t, ray2);
double dot;
matrix_product(1, 3, 3, 1, ray1, ray2, &dot);
double norm = matrix_norm(3, 1, ray1) * matrix_norm(3, 1, ray2);
dot /= norm;
double angle = acos(CLAMP(dot, -1.0 + 1.0e-8, 1.0 - 1.0e-8));
angles[i] = RAD2DEG(angle);
} else {
/* Hack to make sure outliers aren't considered */
angles[i] = 30.0;
}
}
qsort_descending();
double angle_threshold =
kth_element_copy(pt_count, iround(0.90 * pt_count), angles);
/* Angle threshold should be at most 1.0 -- scratch that, 0.15 */
angle_threshold = MIN(0.15 /*0.5*/, angle_threshold);
if (pt_count < 30)
angle_threshold = 0.0;
printf(" Using angle threshold: %0.3f\n", angle_threshold);